Class: Arrow::Tensor

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/tensor.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_tensor, data_type: nil, shape: nil, dimension_names: nil) ⇒ Tensor #initialize(data_type, data, shape, strides, dimension_names) ⇒ Tensor

Creates a new Arrow::Tensor.

Overloads:

  • #initialize(raw_tensor, data_type: nil, shape: nil, dimension_names: nil) ⇒ Tensor

    Examples:

    Create a tensor from Ruby’s Array

    raw_tensor = [
      [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
      ],
      [
        [9, 10, 11, 12],
        [13, 14, 15, 16],
      ],
      [
        [17, 18, 19, 20],
        [21, 22, 23, 24],
      ],
    ]
    Arrow::Tensor.new(raw_tensor)

    Parameters:

    • raw_tensor (::Array<Numeric>)

      The tensor represented as a raw ‘Array` (not `Arrow::Array`) and `Numeric`s. You can pass a nested `Array` for a multi-dimensional tensor.

    • data_type (Arrow::DataType, String, Symbol, ::Array<String>, ::Array<Symbol>, Hash, nil) (defaults to: nil)

      The element data type of the tensor.

      If you specify ‘nil`, data type is guessed from `raw_tensor`.

      See DataType.resolve for how to specify data type.

    • shape (::Array<Integer>, nil) (defaults to: nil)

      The array of dimension sizes.

      If you specify ‘nil`, shape is guessed from `raw_tensor`.

    • dimension_names (::Array<String>, ::Array<Symbol>, nil) (defaults to: nil)

      The array of the dimension names.

      If you specify ‘nil`, all dimensions have empty names.

    Since:

    • 10.0.0

  • #initialize(data_type, data, shape, strides, dimension_names) ⇒ Tensor

    Examples:

    Create a table from Arrow::Buffer

    raw_data = [
      1, 2,
      3, 4,
    
      5, 6,
      7, 8,
    
      9, 10,
      11, 12,
    ]
    data = Arrow::Buffer.new(raw_data.pack("c*").freeze)
    shape = [3, 2, 2]
    strides = []
    names = ["a", "b", "c"]
    Arrow::Tensor.new(:int8, data, shape, strides, names)

    Parameters:

    • data_type (Arrow::DataType, String, Symbol, ::Array<String>, ::Array<Symbol>, Hash)

      The element data type of the tensor.

      See DataType.resolve how to specify data type.

    • data (Arrow::Buffer, String)

      The data of the tensor.

    • shape (::Array<Integer>)

      The array of dimension sizes.

    • strides (::Array<Integer>, nil)

      The array of strides which is the number of bytes between two adjacent elements in each dimension.

      If you specify ‘nil` or an empty `Array`, strides are guessed from `data_type` and `data`.

    • dimension_names (::Array<String>, ::Array<Symbol>, nil)

      The array of the dimension names.

      If you specify ‘nil`, all dimensions doesn’t have their names.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/arrow/tensor.rb', line 105

def initialize(*args,
               data_type: nil,
               data: nil,
               shape: nil,
               strides: nil,
               dimension_names: nil)
  n_args = args.size
  case n_args
  when 1
    converter = RawTensorConverter.new(args[0],
                                       data_type: data_type,
                                       shape: shape,
                                       strides: strides,
                                       dimension_names: dimension_names)
    data_type = converter.data_type
    data = converter.data
    shape = converter.shape
    strides = converter.strides
    dimension_names = converter.dimension_names
  when 0, 2..5
    data_type = args[0] || data_type
    data = args[1] || data
    shape = args[2] || shape
    strides = args[3] || strides
    dimension_names = args[4] || dimension_names
    if data_type.nil?
      raise ArgumentError, "data_type: is missing: #{data.inspect}"
    end
  else
    message = "wrong number of arguments (given #{n_args}, expected 0..5)"
    raise ArgumentError, message
  end
  initialize_raw(DataType.resolve(data_type),
                 data,
                 shape,
                 strides,
                 dimension_names)
end

Instance Method Details

#dimension_namesObject



144
145
146
147
148
# File 'lib/arrow/tensor.rb', line 144

def dimension_names
  n_dimensions.times.collect do |i|
    get_dimension_name(i)
  end
end

#initialize_rawObject



22
# File 'lib/arrow/tensor.rb', line 22

alias_method :initialize_raw, :initialize

#to_arrowObject



150
151
152
# File 'lib/arrow/tensor.rb', line 150

def to_arrow
  self
end

#to_arrow_arrayObject



154
155
156
157
158
159
160
161
162
# File 'lib/arrow/tensor.rb', line 154

def to_arrow_array
  if n_dimensions != 1
    raise RangeError, "must be 1 dimensional tensor: #{shape.inspect}"
  end
  value_data_type.array_class.new(size,
                                  buffer,
                                  nil,
                                  0)
end

#to_arrow_chunked_arrayObject



164
165
166
# File 'lib/arrow/tensor.rb', line 164

def to_arrow_chunked_array
  ChunkedArray.new([to_arrow_array])
end