Class: Torch::Tensor
- Inherits:
-
Object
- Object
- Torch::Tensor
- Includes:
- Comparable, Enumerable, Inspector
- Defined in:
- lib/torch/tensor.rb
Direct Known Subclasses
Constant Summary
Constants included from Inspector
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
TODO better compare?.
-
#[](*indexes) ⇒ Object
based on python_variable_indexing.cpp and pytorch.org/cppdocs/notes/tensor_indexing.html.
-
#[]=(*indexes, value) ⇒ Object
based on python_variable_indexing.cpp and pytorch.org/cppdocs/notes/tensor_indexing.html.
- #cpu ⇒ Object
- #cuda ⇒ Object
- #dtype ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object
-
#imag ⇒ Object
not a method in native_functions.yaml attribute in Python rather than method.
- #item ⇒ Object
- #layout ⇒ Object
-
#length ⇒ Object
mirror Python len().
-
#new ⇒ Object
unsure if this is correct.
-
#numo ⇒ Object
TODO read directly from memory.
-
#random!(*args) ⇒ Object
parser can’t handle overlap, so need to handle manually.
-
#real ⇒ Object
not a method in native_functions.yaml attribute in Python rather than method.
- #requires_grad=(requires_grad) ⇒ Object
- #size(dim = nil) ⇒ Object
-
#stft(*args) ⇒ Object
center option.
- #stride(dim = nil) ⇒ Object
- #to(device = nil, dtype: nil, non_blocking: false, copy: false) ⇒ Object
-
#to_a ⇒ Object
TODO make more performant.
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #type(dtype) ⇒ Object
Methods included from Inspector
Class Method Details
.new(*args) ⇒ Object
25 26 27 |
# File 'lib/torch/tensor.rb', line 25 def self.new(*args) FloatTensor.new(*args) end |
Instance Method Details
#<=>(other) ⇒ Object
TODO better compare?
153 154 155 |
# File 'lib/torch/tensor.rb', line 153 def <=>(other) item <=> other end |
#[](*indexes) ⇒ Object
based on python_variable_indexing.cpp and pytorch.org/cppdocs/notes/tensor_indexing.html
159 160 161 |
# File 'lib/torch/tensor.rb', line 159 def [](*indexes) _index(indexes) end |
#[]=(*indexes, value) ⇒ Object
based on python_variable_indexing.cpp and pytorch.org/cppdocs/notes/tensor_indexing.html
165 166 167 168 169 |
# File 'lib/torch/tensor.rb', line 165 def []=(*indexes, value) raise ArgumentError, "Tensor does not support deleting items" if value.nil? value = Torch.tensor(value, dtype: dtype) unless value.is_a?(Tensor) _index_put_custom(indexes, value) end |
#cpu ⇒ Object
80 81 82 |
# File 'lib/torch/tensor.rb', line 80 def cpu to("cpu") end |
#cuda ⇒ Object
84 85 86 |
# File 'lib/torch/tensor.rb', line 84 def cuda to("cuda") end |
#dtype ⇒ Object
29 30 31 32 33 |
# File 'lib/torch/tensor.rb', line 29 def dtype dtype = ENUM_TO_DTYPE[_dtype] raise Error, "Unknown type: #{_dtype}" unless dtype dtype end |
#dup ⇒ Object
182 183 184 185 186 |
# File 'lib/torch/tensor.rb', line 182 def dup Torch.no_grad do clone end end |
#each ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/torch/tensor.rb', line 43 def each return enum_for(:each) unless block_given? size(0).times do |i| yield self[i] end end |
#imag ⇒ Object
not a method in native_functions.yaml attribute in Python rather than method
190 191 192 |
# File 'lib/torch/tensor.rb', line 190 def imag Torch.imag(self) end |
#item ⇒ Object
110 111 112 113 114 115 |
# File 'lib/torch/tensor.rb', line 110 def item if numel != 1 raise Error, "only one element tensors can be converted to Ruby scalars" end to_a.first end |
#layout ⇒ Object
35 36 37 |
# File 'lib/torch/tensor.rb', line 35 def layout _layout.downcase.to_sym end |
#length ⇒ Object
mirror Python len()
105 106 107 |
# File 'lib/torch/tensor.rb', line 105 def length size(0) end |
#new ⇒ Object
unsure if this is correct
126 127 128 |
# File 'lib/torch/tensor.rb', line 126 def new Torch.empty(0, dtype: dtype) end |
#numo ⇒ Object
TODO read directly from memory
131 132 133 134 135 |
# File 'lib/torch/tensor.rb', line 131 def numo cls = Torch._dtype_to_numo[dtype] raise Error, "Cannot convert #{dtype} to Numo" unless cls cls.from_string(_data_str).reshape(*shape) end |
#random!(*args) ⇒ Object
parser can’t handle overlap, so need to handle manually
172 173 174 175 |
# File 'lib/torch/tensor.rb', line 172 def random!(*args) return _random!(0, *args) if args.size == 1 _random!(*args) end |
#real ⇒ Object
not a method in native_functions.yaml attribute in Python rather than method
196 197 198 |
# File 'lib/torch/tensor.rb', line 196 def real Torch.real(self) end |
#requires_grad=(requires_grad) ⇒ Object
137 138 139 |
# File 'lib/torch/tensor.rb', line 137 def requires_grad=(requires_grad) _requires_grad!(requires_grad) end |
#size(dim = nil) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/torch/tensor.rb', line 88 def size(dim = nil) if dim _size(dim) else shape end end |
#stft(*args) ⇒ Object
center option
178 179 180 |
# File 'lib/torch/tensor.rb', line 178 def stft(*args) Torch.stft(*args) end |
#stride(dim = nil) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/torch/tensor.rb', line 96 def stride(dim = nil) if dim _stride(dim) else _strides end end |
#to(device = nil, dtype: nil, non_blocking: false, copy: false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/torch/tensor.rb', line 64 def to(device = nil, dtype: nil, non_blocking: false, copy: false) if device.is_a?(Symbol) && !dtype dtype = device device = nil end device ||= self.device device = Device.new(device) if device.is_a?(String) dtype ||= self.dtype enum = DTYPE_TO_ENUM[dtype] raise Error, "Unknown type: #{dtype}" unless enum _to(device, enum, non_blocking, copy) end |
#to_a ⇒ Object
TODO make more performant
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/torch/tensor.rb', line 52 def to_a arr = _flat_data if shape.empty? arr else shape[1..-1].reverse.each do |dim| arr = arr.each_slice(dim) end arr.to_a end end |
#to_f ⇒ Object
121 122 123 |
# File 'lib/torch/tensor.rb', line 121 def to_f item.to_f end |
#to_i ⇒ Object
117 118 119 |
# File 'lib/torch/tensor.rb', line 117 def to_i item.to_i end |
#to_s ⇒ Object
39 40 41 |
# File 'lib/torch/tensor.rb', line 39 def to_s inspect end |
#type(dtype) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/torch/tensor.rb', line 141 def type(dtype) if dtype.is_a?(Class) raise Error, "Invalid type: #{dtype}" unless TENSOR_TYPE_CLASSES.include?(dtype) dtype.new(self) else enum = DTYPE_TO_ENUM[dtype] raise Error, "Invalid type: #{dtype}" unless enum _type(enum) end end |