Variable

Struct Variable 

Source
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

Source

pub fn new(data: Tensor<f32>, requires_grad: bool) -> Variable

Creates a new variable from a tensor.

§Arguments
  • data - The tensor data
  • requires_grad - Whether to track gradients for this variable
Source

pub fn from_tensor(data: Tensor<f32>) -> Variable

Creates a variable that doesn’t require gradients.

Source

pub fn data(&self) -> Tensor<f32>

Returns a reference to the underlying tensor data.

Source

pub fn shape(&self) -> Vec<usize>

Returns the shape of the tensor.

Source

pub fn ndim(&self) -> usize

Returns the number of dimensions.

Source

pub fn numel(&self) -> usize

Returns the total number of elements.

Source

pub fn requires_grad(&self) -> bool

Returns whether this variable requires gradients.

Source

pub fn is_leaf(&self) -> bool

Returns whether this is a leaf variable.

Source

pub fn grad(&self) -> Option<Tensor<f32>>

Returns the gradient of this variable.

Only available for leaf variables after backward() has been called.

Source

pub fn grad_fn(&self) -> Option<&GradFn>

Returns the gradient function.

Source

pub fn set_grad(&self, grad: Tensor<f32>)

Sets the gradient (used during backward pass).

Source

pub fn accumulate_grad(&self, grad: &Tensor<f32>)

Accumulates gradient (adds to existing gradient).

Source

pub fn zero_grad(&self)

Clears the gradient.

Source

pub fn detach(&self) -> Variable

Detaches this variable from the computation graph.

Returns a new variable with the same data but no gradient history.

Source

pub fn requires_grad_(self, requires_grad: bool) -> Variable

Returns a new variable with requires_grad set.

Source

pub fn backward(&self)

Computes gradients via backpropagation.

This should only be called on scalar (single-element) tensors, typically the loss value.

Source

pub fn add_var(&self, other: &Variable) -> Variable

Element-wise addition.

Source

pub fn sub_var(&self, other: &Variable) -> Variable

Element-wise subtraction.

Source

pub fn mul_var(&self, other: &Variable) -> Variable

Element-wise multiplication.

Source

pub fn div_var(&self, other: &Variable) -> Variable

Element-wise division.

Source

pub fn neg_var(&self) -> Variable

Negation.

Source

pub fn matmul(&self, other: &Variable) -> Variable

Matrix multiplication.

Source

pub fn pow(&self, exponent: f32) -> Variable

Power operation.

Source

pub fn relu(&self) -> Variable

ReLU activation.

Source

pub fn sigmoid(&self) -> Variable

Sigmoid activation.

Source

pub fn tanh(&self) -> Variable

Tanh activation.

Source

pub fn sum(&self) -> Variable

Sum all elements.

Source

pub fn mean(&self) -> Variable

Mean of all elements.

Source

pub fn mse_loss(&self, target: &Variable) -> Variable

Mean Squared Error loss.

Source

pub fn binary_cross_entropy(&self, target: &Variable) -> Variable

Binary Cross Entropy loss (expects sigmoid output).

Source

pub fn reshape(&self, shape: &[usize]) -> Variable

Reshapes the variable to a new shape.

Source

pub fn transpose(&self, dim0: usize, dim1: usize) -> Variable

Transposes two dimensions.

Source

pub fn slice(&self, ranges: &[Range<usize>]) -> Variable

Slices the variable along specified ranges.

Source

pub fn expand(&self, shape: &[usize]) -> Variable

Expands the variable to a new shape (broadcast).

Source

pub fn mul_scalar(&self, scalar: f32) -> Variable

Multiplies by a scalar.

Source

pub fn add_scalar(&self, scalar: f32) -> Variable

Adds a scalar.

Source

pub fn sub_scalar(&self, scalar: f32) -> Variable

Subtracts a scalar.

Source

pub fn div_scalar(&self, scalar: f32) -> Variable

Divides by a scalar.

Source

pub fn gelu(&self) -> Variable

GELU activation function (Gaussian Error Linear Unit).

Source

pub fn silu(&self) -> Variable

SiLU/Swish activation function (x * sigmoid(x)).

Source

pub fn sqrt(&self) -> Variable

Square root.

Source

pub fn softmax(&self, dim: i32) -> Variable

Softmax along specified dimension.

Source

pub fn log_softmax(&self, dim: i32) -> Variable

Log softmax along specified dimension.

Source

pub fn mean_dim(&self, dim: i32, keepdim: bool) -> Variable

Mean along a dimension, optionally keeping the dimension.

Source

pub fn var_dim(&self, dim: i32, keepdim: bool) -> Variable

Variance along a dimension, optionally keeping the dimension.

Source

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.

Source

pub fn clone_var(&self) -> Variable

Clones the variable (alias for Clone trait).

Source

pub fn add(&self, other: &Variable) -> Variable

Adds another variable (alias for add_var for method chaining).

Source

pub fn sub(&self, other: &Variable) -> Variable

Subtracts another variable (alias for sub_var for method chaining).

Source

pub fn mul(&self, other: &Variable) -> Variable

Multiplies by another variable (alias for mul_var for method chaining).

Source

pub fn div(&self, other: &Variable) -> Variable

Divides by another variable (alias for div_var for method chaining).

Trait Implementations§

Source§

impl Add for &Variable

Source§

type Output = Variable

The resulting type after applying the + operator.
Source§

fn add(self, other: &Variable) -> Variable

Performs the + operation. Read more
Source§

impl Clone for Variable

Source§

fn clone(&self) -> Variable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Variable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Div for &Variable

Source§

type Output = Variable

The resulting type after applying the / operator.
Source§

fn div(self, other: &Variable) -> Variable

Performs the / operation. Read more
Source§

impl Mul for &Variable

Source§

type Output = Variable

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Variable) -> Variable

Performs the * operation. Read more
Source§

impl Neg for &Variable

Source§

type Output = Variable

The resulting type after applying the - operator.
Source§

fn neg(self) -> Variable

Performs the unary - operation. Read more
Source§

impl Sub for &Variable

Source§

type Output = Variable

The resulting type after applying the - operator.
Source§

fn sub(self, other: &Variable) -> Variable

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V