Module: Torch

Defined in:
lib/torch.rb,
lib/torch/tensor.rb,
lib/torch/nn/init.rb,
lib/torch/nn/relu.rb,
lib/torch/version.rb,
lib/torch/inspector.rb,
lib/torch/nn/conv2d.rb,
lib/torch/nn/linear.rb,
lib/torch/nn/module.rb,
lib/torch/optim/sgd.rb,
lib/torch/nn/mse_loss.rb,
lib/torch/nn/parameter.rb,
lib/torch/nn/functional.rb,
lib/torch/nn/sequential.rb,
lib/torch/optim/optimizer.rb,
lib/torch/utils/data/data_loader.rb,
lib/torch/utils/data/tensor_dataset.rb

Defined Under Namespace

Modules: Inspector, NN, Optim, Utils Classes: Error, Tensor

Constant Summary collapse

DTYPE_TO_ENUM =
{
  uint8: 0,
  int8: 1,
  short: 2,
  int16: 2,
  int: 3,
  int32: 3,
  long: 4,
  int64: 4,
  half: 5,
  float16: 5,
  float: 6,
  float32: 6,
  double: 7,
  float64: 7,
  # complex_half: 8,
  # complex_float: 9,
  # complex_double: 10,
  bool: 11,
  # qint8: 12,
  # quint8: 13,
  # qint32: 14,
  # bfloat16: 15
}
ENUM_TO_DTYPE =

qint8: 12, quint8: 13, qint32: 14, bfloat16: 15

DTYPE_TO_ENUM.map(&:reverse).to_h
VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

._dtype_to_numoObject

private use method for cases when Numo not available or available after Torch loaded



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/torch.rb', line 88

def _dtype_to_numo
  {
    uint8: Numo::UInt8,
    int8: Numo::Int8,
    int16: Numo::Int16,
    int32: Numo::Int32,
    int64: Numo::Int64,
    float32: Numo::SFloat,
    float64: Numo::DFloat
  }
end

.arange(start, finish = nil, step = 1, **options) ⇒ Object

— begin tensor creation: pytorch.org/cppdocs/notes/tensor_creation.html



102
103
104
105
106
107
108
109
# File 'lib/torch.rb', line 102

def arange(start, finish = nil, step = 1, **options)
  # ruby doesn't support start = 0, finish, step = 1, ...
  if finish.nil?
    finish = start
    start = 0
  end
  _arange(start, finish, step, tensor_options(**options))
end

.argmax(input, dim = nil, keepdim: false) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/torch.rb', line 251

def argmax(input, dim = nil, keepdim: false)
  if dim
    _argmax_dim(input, dim, keepdim)
  else
    _argmax(input)
  end
end

.dot(input, tensor) ⇒ Object



291
292
293
# File 'lib/torch.rb', line 291

def dot(input, tensor)
  _dot(input, tensor)
end

.empty(*size, **options) ⇒ Object



111
112
113
# File 'lib/torch.rb', line 111

def empty(*size, **options)
  _empty(tensor_size(size), tensor_options(**options))
end

.empty_like(input, **options) ⇒ Object



182
183
184
# File 'lib/torch.rb', line 182

def empty_like(input, **options)
  empty(input.size, like_options(input, options))
end

.eq(input, other) ⇒ Object



259
260
261
# File 'lib/torch.rb', line 259

def eq(input, other)
  _eq(input, other)
end

.exp(input) ⇒ Object



279
280
281
# File 'lib/torch.rb', line 279

def exp(input)
  _exp(input)
end

.eye(n, m = nil, **options) ⇒ Object



115
116
117
# File 'lib/torch.rb', line 115

def eye(n, m = nil, **options)
  _eye(n, m || n, tensor_options(**options))
end

.from_numo(ndarray) ⇒ Object

TODO don’t copy

Raises:



79
80
81
82
83
# File 'lib/torch.rb', line 79

def from_numo(ndarray)
  dtype = _dtype_to_numo.find { |k, v| ndarray.is_a?(v) }
  raise Error, "Cannot convert #{ndarray.class.name} to tensor" unless dtype
  tensor(ndarray.to_a, dtype: dtype[0])
end

.full(size, fill_value, **options) ⇒ Object



119
120
121
# File 'lib/torch.rb', line 119

def full(size, fill_value, **options)
  _full(size, fill_value, tensor_options(**options))
end

.full_like(input, fill_value, **options) ⇒ Object



186
187
188
# File 'lib/torch.rb', line 186

def full_like(input, fill_value, **options)
  full(input.size, fill_value, like_options(input, options))
end

.linspace(start, finish, steps = 100, **options) ⇒ Object



123
124
125
# File 'lib/torch.rb', line 123

def linspace(start, finish, steps = 100, **options)
  _linspace(start, finish, steps, tensor_options(**options))
end

.log(input) ⇒ Object



283
284
285
# File 'lib/torch.rb', line 283

def log(input)
  _log(input)
end

.logspace(start, finish, steps = 100, base = 10.0, **options) ⇒ Object



127
128
129
# File 'lib/torch.rb', line 127

def logspace(start, finish, steps = 100, base = 10.0, **options)
  _logspace(start, finish, steps, base, tensor_options(**options))
end

.matmul(input, other) ⇒ Object



295
296
297
# File 'lib/torch.rb', line 295

def matmul(input, other)
  _matmul(input, other)
end

.max(input) ⇒ Object



275
276
277
# File 'lib/torch.rb', line 275

def max(input)
  _max(input)
end

.mean(input, dim = nil, keepdim: false) ⇒ Object

TODO support out



234
235
236
237
238
239
240
# File 'lib/torch.rb', line 234

def mean(input, dim = nil, keepdim: false)
  if dim
    _mean_dim(input, dim, keepdim)
  else
    _mean(input)
  end
end

.min(input) ⇒ Object



271
272
273
# File 'lib/torch.rb', line 271

def min(input)
  _min(input)
end

.neg(input) ⇒ Object



219
220
221
# File 'lib/torch.rb', line 219

def neg(input)
  _neg(input)
end

.no_gradObject



223
224
225
226
227
228
229
230
231
# File 'lib/torch.rb', line 223

def no_grad
  previous_value = grad_enabled?
  begin
    _set_grad_enabled(false)
    yield
  ensure
    _set_grad_enabled(previous_value)
  end
end

.norm(input) ⇒ Object



263
264
265
# File 'lib/torch.rb', line 263

def norm(input)
  _norm(input)
end

.ones(*size, **options) ⇒ Object



131
132
133
# File 'lib/torch.rb', line 131

def ones(*size, **options)
  _ones(tensor_size(size), tensor_options(**options))
end

.ones_like(input, **options) ⇒ Object

— begin like —



178
179
180
# File 'lib/torch.rb', line 178

def ones_like(input, **options)
  ones(input.size, like_options(input, options))
end

.pow(input, exponent) ⇒ Object



267
268
269
# File 'lib/torch.rb', line 267

def pow(input, exponent)
  _pow(input, exponent)
end

.rand(*size, **options) ⇒ Object



135
136
137
# File 'lib/torch.rb', line 135

def rand(*size, **options)
  _rand(tensor_size(size), tensor_options(**options))
end

.rand_like(input, **options) ⇒ Object



190
191
192
# File 'lib/torch.rb', line 190

def rand_like(input, **options)
  rand(input.size, like_options(input, options))
end

.randint(low = 0, high, size, **options) ⇒ Object



139
140
141
# File 'lib/torch.rb', line 139

def randint(low = 0, high, size, **options)
  _randint(low, high, size, tensor_options(**options))
end

.randint_like(input, low, high = nil, **options) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/torch.rb', line 194

def randint_like(input, low, high = nil, **options)
  # ruby doesn't support input, low = 0, high, ...
  if high.nil?
    high = low
    low = 0
  end
  rand(input.size, like_options(input, options))
end

.randn(*size, **options) ⇒ Object



143
144
145
# File 'lib/torch.rb', line 143

def randn(*size, **options)
  _randn(tensor_size(size), tensor_options(**options))
end

.randn_like(input, **options) ⇒ Object



203
204
205
# File 'lib/torch.rb', line 203

def randn_like(input, **options)
  randn(input.size, like_options(input, options))
end

.randperm(n, **options) ⇒ Object



147
148
149
# File 'lib/torch.rb', line 147

def randperm(n, **options)
  _randperm(n, tensor_options(**options))
end

.reshape(input, shape) ⇒ Object



299
300
301
# File 'lib/torch.rb', line 299

def reshape(input, shape)
  _reshape(input, shape)
end

.sum(input, dim = nil, keepdim: false) ⇒ Object

TODO support dtype



243
244
245
246
247
248
249
# File 'lib/torch.rb', line 243

def sum(input, dim = nil, keepdim: false)
  if dim
    _sum_dim(input, dim, keepdim)
  else
    _sum(input)
  end
end

.tensor(data, **options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/torch.rb', line 155

def tensor(data, **options)
  size = []
  if data.respond_to?(:to_a)
    data = data.to_a
    d = data
    while d.is_a?(Array)
      size << d.size
      d = d.first
    end
    data = data.flatten
  else
    data = [data].compact
  end

  if options[:dtype].nil? && data.all? { |v| v.is_a?(Integer) }
    options[:dtype] = :int64
  end

  _tensor(data, size, tensor_options(**options))
end

.tensor?(obj) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/torch.rb', line 74

def tensor?(obj)
  obj.is_a?(Tensor)
end

.unsqueeze(input, dim) ⇒ Object



287
288
289
# File 'lib/torch.rb', line 287

def unsqueeze(input, dim)
  _unsqueeze(input, dim)
end

.zeros(*size, **options) ⇒ Object



151
152
153
# File 'lib/torch.rb', line 151

def zeros(*size, **options)
  _zeros(tensor_size(size), tensor_options(**options))
end

.zeros_like(input, **options) ⇒ Object



207
208
209
# File 'lib/torch.rb', line 207

def zeros_like(input, **options)
  zeros(input.size, like_options(input, options))
end