class

ML::Autograd::Variable

Inherits Reference / Object

Variable wraps a Tensor and tracks gradients

Constructors

from_tensor(tensor : Tensor, requires_grad : Bool = false) : Variable

Create from existing data

Source
new(data : Tensor, requires_grad : Bool = false)

Create variable from tensor

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

Factory methods that mirror Tensor

Source

Instance methods

*(other : Variable) : Variable

Multiplication

Source
*(scalar : Float32) : Variable
Source
+(other : Variable) : Variable

Addition

Source
+(scalar : Float32) : Variable
Source
-(other : Variable) : Variable

Subtraction

Source
/(other : Variable) : Variable

Division

Source
backward(grad_output : Tensor | Nil = nil) : Nil

Backward pass - compute gradients

Source
clone

Clone with gradient tracking

Source
data
Source
detach

Detach from computation graph (returns new variable with same data, no grad tracking)

Source
grad
Source
grad=(grad : Tensor | Nil)
Source
grad_fn
Source
grad_fn=(grad_fn : GradFn | Nil)
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
is_leaf?
Source
item

Item (for scalar tensors)

Source
matmul(other : Variable) : Variable

Matrix multiplication

Source
mean

Mean reduction

Source
ndim
Source
numel
Source
relu

ReLU activation

Source
requires_grad=(value : Bool)

Allow toggling grad tracking (useful for inference)

Source
requires_grad?
Source
reshape(*dims : Int32) : Variable

Reshape

Source
shape

Shape delegation

Source
sigmoid

Sigmoid activation

Source
sum

Sum reduction

Source
t

Transpose

Source
to_s(io : IO) : Nil

String representation

Source
transpose

Transpose (swap last two dims)

Source
zero_grad!

Zero gradient

Source