t15836_2.nim 629 B

12345678910111213141516171819202122
  1. import std/sugar
  2. type Tensor[T] = object
  3. x: T
  4. proc numerical_gradient*[T](input: T, f: (proc(x: T): T), h = T(1e-5)): T {.inline.} =
  5. result = default(T)
  6. proc numerical_gradient*[T](input: Tensor[T], f: (proc(x: Tensor[T]): T), h = T(1e-5)): Tensor[T] {.noinit.} =
  7. result = default(Tensor[T])
  8. proc conv2d*[T](input: Tensor[T]): Tensor[T] {.inline.} =
  9. result = default(Tensor[T])
  10. proc sum*[T](arg: Tensor[T]): T = default(T)
  11. proc sum*[T](arg: Tensor[T], axis: int): Tensor[T] {.noinit.} = default(Tensor[T])
  12. let dinput = Tensor[int](x: 1)
  13. let target_grad_input = dinput.numerical_gradient(
  14. x => conv2d(x).sum())