Variable wraps a Tensor and tracks gradients
Constructors
from_tensor(tensor :
Tensor, requires_grad :
Bool = false) :
Variable Create from existing data
Sourcenew(data :
Tensor, requires_grad :
Bool = false)
Create variable from tensor
Sourceones(*dims :
Int32, requires_grad :
Bool = false, device :
Tensor::Device =
Tensor.default_device) :
Variable Sourcerand(*dims :
Int32, requires_grad :
Bool = false, device :
Tensor::Device =
Tensor.default_device) :
Variable Sourcerandn(*dims :
Int32, requires_grad :
Bool = false, device :
Tensor::Device =
Tensor.default_device) :
Variable Sourcezeros(*dims :
Int32, requires_grad :
Bool = false, device :
Tensor::Device =
Tensor.default_device) :
Variable Factory methods that mirror Tensor
SourceInstance methods
*(other : Variable) : Variable
Source+(other : Variable) : Variable
Source-(other : Variable) : Variable
Source/(other : Variable) : Variable
Sourcebackward(grad_output : Tensor | Nil = nil) : Nil
Backward pass - compute gradients
Sourceclone
Clone with gradient tracking
Sourcedetach
Detach from computation graph (returns new variable with same data, no grad tracking)
Sourcegrad=(grad : Tensor | Nil)
Sourcegrad_fn=(grad_fn : GradFn | Nil)
SourceAppends 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>
Sourceitem
Item (for scalar tensors)
Sourcematmul(other : Variable) : Variable
Sourcerequires_grad=(value :
Bool)
Allow toggling grad tracking (useful for inference)
Sourcetranspose
Transpose (swap last two dims)
Source