Class: GRX::Data::TensorDataset

Inherits:
Dataset
  • Object
show all
Defined in:
lib/grx/data.rb

Overview

================================================================

TensorDataset — Dataset wrapping parallel tensors (e.g. X and Y)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*tensors) ⇒ TensorDataset

Returns a new instance of TensorDataset.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/grx/data.rb', line 24

def initialize(*tensors)
  raise ArgumentError, "Must provide at least one tensor" if tensors.empty?
  first_dim = tensors.first.shape[0]
  unless tensors.all? { |t| t.shape[0] == first_dim }
    raise ArgumentError, "All tensors must have the same size in batch dimension (dimension 0)"
  end
  @tensors = tensors
  @size = first_dim
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



22
23
24
# File 'lib/grx/data.rb', line 22

def size
  @size
end

#tensorsObject (readonly)

Returns the value of attribute tensors.



22
23
24
# File 'lib/grx/data.rb', line 22

def tensors
  @tensors
end

Instance Method Details

#[](index) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/grx/data.rb', line 34

def [](index)
  @tensors.map do |t|
    cols = t.numel / @size
    offset = index * cols
    data = t.to_a.slice(offset, cols)
    new_shape = t.shape.size == 1 ? [1] : [1] + t.shape[1..]
    Tensor.create(data, new_shape)
  end
end