Module: Torch
- Defined in:
- lib/torch/inspector.rb,
lib/torch.rb,
lib/torch/hub.rb,
lib/torch/nn/gru.rb,
lib/torch/nn/rnn.rb,
lib/torch/tensor.rb,
lib/torch/nn/fold.rb,
lib/torch/nn/init.rb,
lib/torch/nn/loss.rb,
lib/torch/nn/lstm.rb,
lib/torch/nn/relu.rb,
lib/torch/nn/tanh.rb,
lib/torch/version.rb,
lib/torch/nn/prelu.rb,
lib/torch/nn/utils.rb,
lib/torch/nn/conv1d.rb,
lib/torch/nn/conv2d.rb,
lib/torch/nn/conv3d.rb,
lib/torch/nn/convnd.rb,
lib/torch/nn/linear.rb,
lib/torch/nn/module.rb,
lib/torch/nn/unfold.rb,
lib/torch/optim/sgd.rb,
lib/torch/nn/dropout.rb,
lib/torch/nn/l1_loss.rb,
lib/torch/nn/sigmoid.rb,
lib/torch/nn/softmax.rb,
lib/torch/nn/softmin.rb,
lib/torch/optim/adam.rb,
lib/torch/optim/asgd.rb,
lib/torch/utils/data.rb,
lib/torch/nn/bce_loss.rb,
lib/torch/nn/bilinear.rb,
lib/torch/nn/ctc_loss.rb,
lib/torch/nn/identity.rb,
lib/torch/nn/mse_loss.rb,
lib/torch/nn/nll_loss.rb,
lib/torch/nn/rnn_base.rb,
lib/torch/nn/softplus.rb,
lib/torch/nn/softsign.rb,
lib/torch/optim/adamw.rb,
lib/torch/optim/rprop.rb,
lib/torch/nn/dropout2d.rb,
lib/torch/nn/dropout3d.rb,
lib/torch/nn/dropoutnd.rb,
lib/torch/nn/embedding.rb,
lib/torch/nn/lp_pool1d.rb,
lib/torch/nn/lp_pool2d.rb,
lib/torch/nn/lp_poolnd.rb,
lib/torch/nn/parameter.rb,
lib/torch/nn/softmax2d.rb,
lib/torch/optim/adamax.rb,
lib/torch/native/parser.rb,
lib/torch/nn/avg_pool1d.rb,
lib/torch/nn/avg_pool2d.rb,
lib/torch/nn/avg_pool3d.rb,
lib/torch/nn/avg_poolnd.rb,
lib/torch/nn/batch_norm.rb,
lib/torch/nn/functional.rb,
lib/torch/nn/group_norm.rb,
lib/torch/nn/hardshrink.rb,
lib/torch/nn/layer_norm.rb,
lib/torch/nn/leaky_relu.rb,
lib/torch/nn/max_pool1d.rb,
lib/torch/nn/max_pool2d.rb,
lib/torch/nn/max_pool3d.rb,
lib/torch/nn/max_poolnd.rb,
lib/torch/nn/sequential.rb,
lib/torch/nn/softshrink.rb,
lib/torch/nn/tanhshrink.rb,
lib/torch/nn/zero_pad2d.rb,
lib/torch/optim/adagrad.rb,
lib/torch/optim/rmsprop.rb,
lib/torch/nn/kl_div_loss.rb,
lib/torch/nn/log_sigmoid.rb,
lib/torch/nn/log_softmax.rb,
lib/torch/optim/adadelta.rb,
lib/torch/native/function.rb,
lib/torch/nn/batch_norm1d.rb,
lib/torch/nn/batch_norm2d.rb,
lib/torch/nn/batch_norm3d.rb,
lib/torch/nn/max_unpool1d.rb,
lib/torch/nn/max_unpool2d.rb,
lib/torch/nn/max_unpool3d.rb,
lib/torch/nn/max_unpoolnd.rb,
lib/torch/optim/optimizer.rb,
lib/torch/native/generator.rb,
lib/torch/nn/alpha_dropout.rb,
lib/torch/nn/embedding_bag.rb,
lib/torch/nn/instance_norm.rb,
lib/torch/nn/weighted_loss.rb,
lib/torch/native/dispatcher.rb,
lib/torch/nn/constant_pad1d.rb,
lib/torch/nn/constant_pad2d.rb,
lib/torch/nn/constant_pad3d.rb,
lib/torch/nn/constant_padnd.rb,
lib/torch/nn/smooth_l1_loss.rb,
lib/torch/utils/data/subset.rb,
lib/torch/nn/instance_norm1d.rb,
lib/torch/nn/instance_norm2d.rb,
lib/torch/nn/instance_norm3d.rb,
lib/torch/utils/data/dataset.rb,
lib/torch/nn/poisson_nll_loss.rb,
lib/torch/nn/reflection_pad1d.rb,
lib/torch/nn/reflection_pad2d.rb,
lib/torch/nn/reflection_padnd.rb,
lib/torch/nn/soft_margin_loss.rb,
lib/torch/nn/cosine_similarity.rb,
lib/torch/nn/multi_margin_loss.rb,
lib/torch/nn/pairwise_distance.rb,
lib/torch/nn/replication_pad1d.rb,
lib/torch/nn/replication_pad2d.rb,
lib/torch/nn/replication_pad3d.rb,
lib/torch/nn/replication_padnd.rb,
lib/torch/nn/cross_entropy_loss.rb,
lib/torch/nn/adaptive_avg_pool1d.rb,
lib/torch/nn/adaptive_avg_pool2d.rb,
lib/torch/nn/adaptive_avg_pool3d.rb,
lib/torch/nn/adaptive_avg_poolnd.rb,
lib/torch/nn/adaptive_max_pool1d.rb,
lib/torch/nn/adaptive_max_pool2d.rb,
lib/torch/nn/adaptive_max_pool3d.rb,
lib/torch/nn/adaptive_max_poolnd.rb,
lib/torch/nn/local_response_norm.rb,
lib/torch/nn/margin_ranking_loss.rb,
lib/torch/nn/triplet_margin_loss.rb,
lib/torch/utils/data/data_loader.rb,
lib/torch/nn/bce_with_logits_loss.rb,
lib/torch/nn/hinge_embedding_loss.rb,
lib/torch/nn/cosine_embedding_loss.rb,
lib/torch/nn/feature_alpha_dropout.rb,
lib/torch/utils/data/tensor_dataset.rb,
lib/torch/nn/multi_label_margin_loss.rb,
lib/torch/optim/lr_scheduler/step_lr.rb,
lib/torch/optim/lr_scheduler/lambda_lr.rb,
lib/torch/nn/multi_label_soft_margin_loss.rb,
lib/torch/optim/lr_scheduler/lr_scheduler.rb,
lib/torch/optim/lr_scheduler/multi_step_lr.rb,
lib/torch/optim/lr_scheduler/exponential_lr.rb,
lib/torch/optim/lr_scheduler/multiplicative_lr.rb,
lib/torch/optim/lr_scheduler/cosine_annealing_lr.rb
Overview
We use a generic interface for methods (*args, **options) and this class to determine the C++ method to call
This is needed since LibTorch uses function overloading, which isn’t available in Ruby or Python
PyTorch uses this approach, but the parser/dispatcher is written in C++
We could generate Ruby methods directly, but an advantage of this approach is arguments and keyword arguments can be used interchangably like in Python, making it easier to port code
Defined Under Namespace
Modules: Hub, Inspector, NN, Native, Optim, Utils Classes: Error, NotImplementedYet, 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 =
DTYPE_TO_ENUM.map(&:reverse).to_h
- DTYPE_TO_CLASS =
{ float32: "FloatTensor", float64: "DoubleTensor", float16: "HalfTensor", uint8: "ByteTensor", int8: "CharTensor", int16: "ShortTensor", int32: "IntTensor", int64: "LongTensor", bool: "BoolTensor" }
- VERSION =
"0.3.6"
Class Method Summary collapse
-
._dtype_to_numo ⇒ Object
private use method for cases when Numo not available or available after Torch loaded.
- ._make_tensor_class(dtype, cuda = false) ⇒ Object
-
.arange(start, finish = nil, step = 1, **options) ⇒ Object
— begin tensor creation: pytorch.org/cppdocs/notes/tensor_creation.html —.
- .clamp(tensor, min, max) ⇒ Object
- .device(str) ⇒ Object
- .empty(*size, **options) ⇒ Object
- .empty_like(input, **options) ⇒ Object
- .enable_grad ⇒ Object
- .eye(n, m = nil, **options) ⇒ Object
- .from_numo(ndarray) ⇒ Object
- .full(size, fill_value, **options) ⇒ Object
- .full_like(input, fill_value, **options) ⇒ Object
- .linspace(start, finish, steps = 100, **options) ⇒ Object
- .load(f) ⇒ Object
- .logspace(start, finish, steps = 100, base = 10.0, **options) ⇒ Object
- .no_grad ⇒ Object
- .ones(*size, **options) ⇒ Object
-
.ones_like(input, **options) ⇒ Object
— begin like —.
- .rand(*size, **options) ⇒ Object
- .rand_like(input, **options) ⇒ Object
- .randint(low = 0, high, size, **options) ⇒ Object
- .randint_like(input, low, high = nil, **options) ⇒ Object
- .randn(*size, **options) ⇒ Object
- .randn_like(input, **options) ⇒ Object
- .randperm(n, **options) ⇒ Object
- .save(obj, f) ⇒ Object
- .stft(input, n_fft, hop_length: nil, win_length: nil, window: nil, center: true, pad_mode: "reflect", normalized: false, onesided: true) ⇒ Object
- .tensor(data, **options) ⇒ Object
- .tensor?(obj) ⇒ Boolean
- .zeros(*size, **options) ⇒ Object
- .zeros_like(input, **options) ⇒ Object
Class Method Details
._dtype_to_numo ⇒ Object
private use method for cases when Numo not available or available after Torch loaded
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/torch.rb', line 294 def _dtype_to_numo raise Error, "Numo not found" unless defined?(Numo::NArray) { uint8: Numo::UInt8, int8: Numo::Int8, int16: Numo::Int16, int32: Numo::Int32, int64: Numo::Int64, float32: Numo::SFloat, float64: Numo::DFloat } end |
._make_tensor_class(dtype, cuda = false) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/torch.rb', line 227 def self._make_tensor_class(dtype, cuda = false) cls = Class.new device = cuda ? "cuda" : "cpu" cls.define_singleton_method("new") do |*args| if args.size == 1 && args.first.is_a?(Tensor) args.first.send(dtype).to(device) elsif args.size == 1 && args.first.is_a?(Array) Torch.tensor(args.first, dtype: dtype, device: device) else Torch.empty(*args, dtype: dtype, device: device) end end cls end |
.arange(start, finish = nil, step = 1, **options) ⇒ Object
— begin tensor creation: pytorch.org/cppdocs/notes/tensor_creation.html —
342 343 344 345 346 347 348 349 |
# File 'lib/torch.rb', line 342 def arange(start, finish = nil, step = 1, **) # ruby doesn't support start = 0, finish, step = 1, ... if finish.nil? finish = start start = 0 end _arange(start, finish, step, (**)) end |
.clamp(tensor, min, max) ⇒ Object
475 476 477 478 |
# File 'lib/torch.rb', line 475 def clamp(tensor, min, max) tensor = _clamp_min(tensor, min) _clamp_max(tensor, max) end |
.device(str) ⇒ Object
328 329 330 |
# File 'lib/torch.rb', line 328 def device(str) Device.new(str) end |
.empty(*size, **options) ⇒ Object
351 352 353 |
# File 'lib/torch.rb', line 351 def empty(*size, **) _empty(tensor_size(size), (**)) end |
.empty_like(input, **options) ⇒ Object
435 436 437 |
# File 'lib/torch.rb', line 435 def empty_like(input, **) empty(input.size, **(input, )) end |
.enable_grad ⇒ Object
318 319 320 321 322 323 324 325 326 |
# File 'lib/torch.rb', line 318 def enable_grad previous_value = grad_enabled? begin _set_grad_enabled(true) yield ensure _set_grad_enabled(previous_value) end end |
.eye(n, m = nil, **options) ⇒ Object
355 356 357 |
# File 'lib/torch.rb', line 355 def eye(n, m = nil, **) _eye(n, m || n, (**)) end |
.from_numo(ndarray) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/torch.rb', line 277 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 = (device: "cpu", dtype: dtype[0]) # TODO pass pointer to array instead of creating string str = ndarray.to_string tensor = _from_blob(str, ndarray.shape, ) # from_blob does not own the data, so we need to keep # a reference to it for duration of tensor # can remove when passing pointer directly tensor.instance_variable_set("@_numo_str", str) tensor end |
.full(size, fill_value, **options) ⇒ Object
359 360 361 |
# File 'lib/torch.rb', line 359 def full(size, fill_value, **) _full(size, fill_value, (**)) end |
.full_like(input, fill_value, **options) ⇒ Object
439 440 441 |
# File 'lib/torch.rb', line 439 def full_like(input, fill_value, **) full(input.size, fill_value, **(input, )) end |
.linspace(start, finish, steps = 100, **options) ⇒ Object
363 364 365 |
# File 'lib/torch.rb', line 363 def linspace(start, finish, steps = 100, **) _linspace(start, finish, steps, (**)) end |
.load(f) ⇒ Object
336 337 338 |
# File 'lib/torch.rb', line 336 def load(f) to_ruby(_load(File.binread(f))) end |
.logspace(start, finish, steps = 100, base = 10.0, **options) ⇒ Object
367 368 369 |
# File 'lib/torch.rb', line 367 def logspace(start, finish, steps = 100, base = 10.0, **) _logspace(start, finish, steps, base, (**)) end |
.no_grad ⇒ Object
308 309 310 311 312 313 314 315 316 |
# File 'lib/torch.rb', line 308 def no_grad previous_value = grad_enabled? begin _set_grad_enabled(false) yield ensure _set_grad_enabled(previous_value) end end |
.ones(*size, **options) ⇒ Object
371 372 373 |
# File 'lib/torch.rb', line 371 def ones(*size, **) _ones(tensor_size(size), (**)) end |
.ones_like(input, **options) ⇒ Object
— begin like —
431 432 433 |
# File 'lib/torch.rb', line 431 def ones_like(input, **) ones(input.size, **(input, )) end |
.rand(*size, **options) ⇒ Object
375 376 377 |
# File 'lib/torch.rb', line 375 def rand(*size, **) _rand(tensor_size(size), (**)) end |
.rand_like(input, **options) ⇒ Object
443 444 445 |
# File 'lib/torch.rb', line 443 def rand_like(input, **) rand(input.size, **(input, )) end |
.randint(low = 0, high, size, **options) ⇒ Object
379 380 381 |
# File 'lib/torch.rb', line 379 def randint(low = 0, high, size, **) _randint(low, high, size, (**)) end |
.randint_like(input, low, high = nil, **options) ⇒ Object
447 448 449 450 451 452 453 454 |
# File 'lib/torch.rb', line 447 def randint_like(input, low, high = nil, **) # ruby doesn't support input, low = 0, high, ... if high.nil? high = low low = 0 end randint(low, high, input.size, **(input, )) end |
.randn(*size, **options) ⇒ Object
383 384 385 |
# File 'lib/torch.rb', line 383 def randn(*size, **) _randn(tensor_size(size), (**)) end |
.randn_like(input, **options) ⇒ Object
456 457 458 |
# File 'lib/torch.rb', line 456 def randn_like(input, **) randn(input.size, **(input, )) end |
.randperm(n, **options) ⇒ Object
387 388 389 390 391 392 393 |
# File 'lib/torch.rb', line 387 def randperm(n, **) # dtype hack in Python # https://github.com/pytorch/pytorch/blob/v1.6.0/tools/autograd/gen_python_functions.py#L1307-L1311 [:dtype] ||= :int64 _randperm(n, (**)) end |
.save(obj, f) ⇒ Object
332 333 334 |
# File 'lib/torch.rb', line 332 def save(obj, f) File.binwrite(f, _save(to_ivalue(obj))) end |
.stft(input, n_fft, hop_length: nil, win_length: nil, window: nil, center: true, pad_mode: "reflect", normalized: false, onesided: true) ⇒ Object
464 465 466 467 468 469 470 471 472 473 |
# File 'lib/torch.rb', line 464 def stft(input, n_fft, hop_length: nil, win_length: nil, window: nil, center: true, pad_mode: "reflect", normalized: false, onesided: true) if center signal_dim = input.dim extended_shape = [1] * (3 - signal_dim) + input.size pad = n_fft.div(2).to_i input = NN::F.pad(input.view(extended_shape), [pad, pad], mode: pad_mode) input = input.view(input.shape[-signal_dim..-1]) end _stft(input, n_fft, hop_length, win_length, window, normalized, onesided) end |
.tensor(data, **options) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/torch.rb', line 399 def tensor(data, **) if [:dtype].nil? && defined?(Numo::NArray) && data.is_a?(Numo::NArray) numo_to_dtype = _dtype_to_numo.map(&:reverse).to_h [:dtype] = numo_to_dtype[data.class] end 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 [:dtype].nil? if data.all? { |v| v.is_a?(Integer) } [:dtype] = :int64 elsif data.all? { |v| v == true || v == false } [:dtype] = :bool end end _tensor(data, size, (**)) end |
.tensor?(obj) ⇒ Boolean
273 274 275 |
# File 'lib/torch.rb', line 273 def tensor?(obj) obj.is_a?(Tensor) end |
.zeros(*size, **options) ⇒ Object
395 396 397 |
# File 'lib/torch.rb', line 395 def zeros(*size, **) _zeros(tensor_size(size), (**)) end |
.zeros_like(input, **options) ⇒ Object
460 461 462 |
# File 'lib/torch.rb', line 460 def zeros_like(input, **) zeros(input.size, **(input, )) end |