Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. # Seq of tensors to tensor
  2. proc toTensor*[T](s: openarray[Tensor[T]]): Tensor[T] =
  3. s.map(proc(t: Tensor[T]): Tensor[T] = t.unsafeUnsqueeze(0)).concat(0)
  4.  
  5. # Make universal does not work with apply
  6. proc abs*[T](t: Tensor[T]): Tensor[T] =
  7. t.map(proc(x: T):T = abs(x))
  8.  
  9. # Seq to reshaped tensor, no copy
  10. proc unsafeToTensorReshape[T](data: seq[T], shape: openarray[int]): Tensor[T] {.noSideEffect.} =
  11. result.shape = @shape
  12. result.strides = shape_to_strides(result.shape)
  13. result.offset = 0
  14. shallowCopy(result.data, data)
  15.  
  16. # This is not the full implementation
  17. template unsafeAt[T](t: Tensor[T], x: int): Tensor[T] =
  18. t.unsafeView(x, _, _).unsafeReshape([t.shape[1], t.shape[2]])
  19.  
  20. # unsafeSqueeze on axis
  21. proc unsafeSqueeze*[T](t: Tensor[T], axis: int): Tensor[T] {.noSideEffect,inline.} =
  22. var shape = t.shape
  23. assert shape[axis] == 1
  24. shape.delete(axis)
  25. t.unsafeReshape(shape)
  26.  
  27. # unsqueeze on axis
  28. proc unsafeUnsqueeze*(t: Tensor, axis: int): Tensor =
  29. var shape = t.shape
  30. shape.insert(1, axis)
  31. t.reshape(shape)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement