pub struct Variable { /* private fields */ }Expand description
A tensor with automatic differentiation support.
Variable wraps a Tensor and tracks operations performed on it to enable
automatic gradient computation. When requires_grad is true, all operations
are recorded in a computational graph.
Implementations§
Source§impl Variable
impl Variable
Sourcepub fn new(data: Tensor<f32>, requires_grad: bool) -> Variable
pub fn new(data: Tensor<f32>, requires_grad: bool) -> Variable
Creates a new variable from a tensor.
§Arguments
data- The tensor datarequires_grad- Whether to track gradients for this variable
Sourcepub fn from_tensor(data: Tensor<f32>) -> Variable
pub fn from_tensor(data: Tensor<f32>) -> Variable
Creates a variable that doesn’t require gradients.
Sourcepub fn requires_grad(&self) -> bool
pub fn requires_grad(&self) -> bool
Returns whether this variable requires gradients.
Sourcepub fn grad(&self) -> Option<Tensor<f32>>
pub fn grad(&self) -> Option<Tensor<f32>>
Returns the gradient of this variable.
Only available for leaf variables after backward() has been called.
Sourcepub fn accumulate_grad(&self, grad: &Tensor<f32>)
pub fn accumulate_grad(&self, grad: &Tensor<f32>)
Accumulates gradient (adds to existing gradient).
Sourcepub fn detach(&self) -> Variable
pub fn detach(&self) -> Variable
Detaches this variable from the computation graph.
Returns a new variable with the same data but no gradient history.
Sourcepub fn requires_grad_(self, requires_grad: bool) -> Variable
pub fn requires_grad_(self, requires_grad: bool) -> Variable
Returns a new variable with requires_grad set.
Sourcepub fn backward(&self)
pub fn backward(&self)
Computes gradients via backpropagation.
This should only be called on scalar (single-element) tensors, typically the loss value.
Sourcepub fn binary_cross_entropy(&self, target: &Variable) -> Variable
pub fn binary_cross_entropy(&self, target: &Variable) -> Variable
Binary Cross Entropy loss (expects sigmoid output).
Sourcepub fn slice(&self, ranges: &[Range<usize>]) -> Variable
pub fn slice(&self, ranges: &[Range<usize>]) -> Variable
Slices the variable along specified ranges.
Sourcepub fn narrow(&self, dim: usize, start: usize, length: usize) -> Variable
pub fn narrow(&self, dim: usize, start: usize, length: usize) -> Variable
Narrows the variable along a dimension.
Returns a view of the tensor containing elements from start to start + length
along the specified dimension. This operation preserves gradients for backpropagation.
Sourcepub fn expand(&self, shape: &[usize]) -> Variable
pub fn expand(&self, shape: &[usize]) -> Variable
Expands the variable to a new shape (broadcast).
Sourcepub fn mul_scalar(&self, scalar: f32) -> Variable
pub fn mul_scalar(&self, scalar: f32) -> Variable
Multiplies by a scalar.
Sourcepub fn add_scalar(&self, scalar: f32) -> Variable
pub fn add_scalar(&self, scalar: f32) -> Variable
Adds a scalar.
Sourcepub fn sub_scalar(&self, scalar: f32) -> Variable
pub fn sub_scalar(&self, scalar: f32) -> Variable
Subtracts a scalar.
Sourcepub fn div_scalar(&self, scalar: f32) -> Variable
pub fn div_scalar(&self, scalar: f32) -> Variable
Divides by a scalar.
Sourcepub fn log_softmax(&self, dim: i32) -> Variable
pub fn log_softmax(&self, dim: i32) -> Variable
Log softmax along specified dimension.
Sourcepub fn mean_dim(&self, dim: i32, keepdim: bool) -> Variable
pub fn mean_dim(&self, dim: i32, keepdim: bool) -> Variable
Mean along a dimension, optionally keeping the dimension.
Sourcepub fn var_dim(&self, dim: i32, keepdim: bool) -> Variable
pub fn var_dim(&self, dim: i32, keepdim: bool) -> Variable
Variance along a dimension, optionally keeping the dimension.
Sourcepub fn from_tensor_with_grad(data: Tensor<f32>, requires_grad: bool) -> Variable
pub fn from_tensor_with_grad(data: Tensor<f32>, requires_grad: bool) -> Variable
Creates a Variable from a tensor and requires_grad flag (for weight access). This is typically used internally by Parameter types.
Sourcepub fn add(&self, other: &Variable) -> Variable
pub fn add(&self, other: &Variable) -> Variable
Adds another variable (alias for add_var for method chaining).
Sourcepub fn sub(&self, other: &Variable) -> Variable
pub fn sub(&self, other: &Variable) -> Variable
Subtracts another variable (alias for sub_var for method chaining).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Variable
impl !RefUnwindSafe for Variable
impl Send for Variable
impl Sync for Variable
impl Unpin for Variable
impl !UnwindSafe for Variable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more