class

ML::Tensor

Inherits Reference / Object

Tensor: shape + strides + data buffer Immutable shape, mutable data

Constructors

arange(start : Float32, stop : Float32, step : Float32 = 1.0_f32, device : Device = Tensor.default_device) : Tensor

Arange

Source
eye(n : Int32, device : Device = Tensor.default_device) : Tensor

Identity matrix

Source
from_array(data : Array(Float32), shape : Shape) : Tensor

Create from existing data (CPU)

Source
from_array(data : Array(Array(Float32))) : Tensor

Create from nested array (infers shape)

Source
full(*dims : Int32, value : Float32, device : Device = Tensor.default_device) : Tensor
Source
linspace(start : Float32, stop : Float32, count : Int32, device : Device = Tensor.default_device) : Tensor

Linspace

Source
new(shape : Shape, dtype : DType = DType::F32, device : Device = Tensor.default_device)

Create empty tensor on GPU

Source
new(*dims : Int32, dtype : DType = DType::F32, device : Device = Tensor.default_device) : Tensor

Create from shape tuple

Source
ones(*dims : Int32, device : Device = Tensor.default_device) : Tensor
Source
rand(*dims : Int32, device : Device = Tensor.default_device) : Tensor
Source
randn(*dims : Int32, device : Device = Tensor.default_device) : Tensor
Source
zeros(*dims : Int32, device : Device = Tensor.default_device) : Tensor

Factory methods

Source

Class methods

default_device

Default device (compile-time selectable)

Source

Instance methods

[](indices : Array(Int32)) : Float32

Element access (for debugging, copies to CPU if needed)

Source
[](*indices : Int32) : Float32
Source
[]=(indices : Array(Int32), value : Float32) : Float32
Source
[]=(*indices_and_value) : Float32
Source
buffer
Source
buffer_handle

Get underlying buffer handle for kernel dispatch

Source
clone

Clone (deep copy)

Source
contiguous

Make contiguous copy

Source
contiguous?
Source
cpu_data
Source
data_ptr

Get raw data pointer (CPU only)

Source
device
Source
dtype
Source
fill!(value : Float32) : self

Fill with value

Source
flatten(start_dim : Int32 = 0, end_dim : Int32 = -1) : Tensor

Flatten

Source
inspect(io : IO) : Nil

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Source
ndim
Source
numel

Properties

Source
on_cpu?
Source
on_gpu?
Source
reshape(new_shape : Shape) : Tensor

Reshape (returns view if contiguous, copy otherwise)

Source
reshape(*dims : Int32) : Tensor
Source
safe_cpu_data

Safe CPU data access - converts from GPU if needed, handles empty tensors Returns empty array for empty tensors instead of crashing

Source
shape
Source
squeeze(dim : Int32 | Nil = nil) : Tensor

Squeeze / Unsqueeze

Source
strides
Source
to_a

Data access

Source
to_cpu
Source
to_cpu!
Source
to_flat_array
Source
to_gpu

Device transfer

Source
to_gpu!
Source
to_s(io : IO) : Nil

String representation

Source
transpose

Transpose (swap last two dims)

Source
unsqueeze(dim : Int32) : Tensor
Source

Nested types