Struct engram::linalg::Tensor

source ·
pub struct Tensor {
    pub rows: usize,
    pub cols: usize,
    pub data: Tensor2D,
    pub grad: Option<Tensor2D>,
}
Expand description

A tensor of floating point values.

Fields§

§rows: usize

The number of rows in the tensor.

§cols: usize

The number of columns in the tensor.

§data: Tensor2D

The data in the tensor, represented as a two-dimensional vector.

§grad: Option<Tensor2D>

Gradient of the tensor.

Implementations§

source§

impl Tensor

source

pub fn shape(&self) -> (usize, usize)

Returns the shape of the tensor as a tuple of (rows, columns).

§Examples
let t1 = Tensor::zeros(2, 3);
let t2 = Tensor::zeros(3, 2);
t1.shape();
t2.shape();
source

pub fn size(&self) -> usize

Returns the number of elements in the tensor.

§Examples
let t1 = Tensor::zeros(2, 3);
let t2 = Tensor::zeros(5, 4);
t1.size();
t2.size();
source

pub fn first(&self) -> f64

Returns the first element in the tensor.

§Examples
let t = tensor![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
t.first();
source§

impl Tensor

source

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

Performs matrix multiplication between two tensors.

§Examples
let a = tensor![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
let b = tensor![[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]];
let c = a.matmul(&b);
source

pub fn mapv(&self, function: &dyn Fn(f64) -> f64) -> Tensor

Applies a function to each element in the tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.mapv(&|x| x * 2.0);
source

pub fn mapv_mut(&mut self, function: &dyn Fn(f64) -> f64)

Applies a function to each element in the tensor in-place.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.mapv_mut(&|x| x * 2.0);
source

pub fn square(&self) -> Tensor

Returns the square of each element in the tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.square();
source

pub fn square_mut(&mut self)

Squares each element in the tensor in-place.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.square_mut();
source

pub fn sqrt(&self) -> Tensor

Returns the square root of each element in the tensor.

§Examples
let a = tensor![[1.0, 4.0], [9.0, 16.0]];
let b = a.sqrt();
source

pub fn sqrt_mut(&mut self)

Takes the square root of each element in the tensor in-place.

§Examples
let mut a = tensor![[1.0, 4.0], [9.0, 16.0]];
a.sqrt_mut();
source

pub fn pow(&self, exponent: f64) -> Tensor

Returns each element in the tensor raised to the given exponent.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.pow(2.0);
source

pub fn pow_mut(&mut self, exponent: f64)

Raises each element in the tensor to the given exponent in-place.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.pow_mut(2.0);
source

pub fn ln(&self) -> Tensor

Returns each element in the tensor applied with the natural logarithm.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.ln();
source

pub fn ln_mut(&mut self)

Applies the natural logarithm to each element in the tensor in-place.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.ln_mut();
source

pub fn log2(&self) -> Tensor

Returns each element in the tensor applied with the base 2 logarithm.

§Examples
let a = tensor![[1.0, 2.0], [4.0, 8.0]];
let b = a.log2();
source

pub fn log2_mut(&mut self)

Applies the base 2 logarithm to each element in the tensor in-place.

§Examples
let mut a = tensor![[1.0, 2.0], [4.0, 8.0]];
a.log2_mut();
source

pub fn abs(&self) -> Tensor

Returns a tensor with the absolute value of each element in the tensor.

§Examples
let a = tensor![[-1.0, 2.0], [-3.0, 4.0]];
let b = a.abs();
source

pub fn dot(&self, other: &Tensor) -> f64

Computes the dot product between two tensors.

§Examples
let a = tensor![[1.0], [2.0], [3.0]];
let b = tensor![[4.0], [5.0], [6.0]];
let c = a.dot(&b);
source

pub fn sum(&self) -> f64

Returns the sum of all elements in the tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.sum();
source

pub fn sum_axis(&self, axis: u8) -> Tensor

Returns the sum of all elements in the tensor along the given axis.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
let sum_cols = a.sum_axis(0);
let sum_rows = a.sum_axis(1);
source

pub fn mean(&self) -> f64

Returns the mean of all elements in the tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.mean();
source

pub fn norm(&self, p: f64) -> f64

Returns the p-norm of the tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.norm(2.0);
source

pub fn activate(&self, activation: &Activation) -> Tensor

Returns a new tensor with each element in the tensor activated by the given activation function.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.activate(&Activation::Sigmoid);
source

pub fn activate_mut(&mut self, activation: &Activation)

Mutates the tensor in-place with each element in the tensor activated by the given activation function.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.activate_mut(&Activation::Sigmoid);
source

pub fn grad(&mut self, activation: &Activation) -> Tensor

Returns a new tensor with each element in the tensor activated by the derivative of the given activation function.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.grad(&Activation::Sigmoid);
source§

impl Tensor

source

pub fn new(rows: usize, cols: usize, value: f64) -> Tensor

Creates a new Tensor with the specified number of rows, columns, and value.

§Examples
let t1 = Tensor::new(2, 3, 0.0);
let t2 = Tensor::new(3, 2, 0.0);
source

pub fn new_like(other: &Tensor, value: f64) -> Tensor

Creates a new Tensor with the same shape as the provided Tensor, filled with the specified value.

§Examples
let t1 = Tensor::new(2, 3, 0.0);
let t2 = Tensor::new_like(&t1, 0.0);
source

pub fn zeros(rows: usize, cols: usize) -> Tensor

source

pub fn zeros_like(other: &Tensor) -> Tensor

source

pub fn ones(rows: usize, cols: usize) -> Tensor

source

pub fn ones_like(other: &Tensor) -> Tensor

source

pub fn identity(size: usize) -> Tensor

Creates a new Tensor with the specified size, initialized to the identity matrix.

§Examples
let t = Tensor::identity(3);
source

pub fn initialize(rows: usize, cols: usize, initializer: &Initializer) -> Tensor

Creates a new Tensor with the specified number of rows and columns, initialized using the provided initializer.

§Examples
let t = Tensor::initialize(2, 3, &Initializer::Xavier);
source§

impl Tensor

source

pub fn iter_rows(&self) -> impl Iterator<Item = &Tensor1D>

Returns an iterator over the rows of the tensor.

§Examples
let mut tensor = tensor![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
let mut iter = tensor.iter_rows();
source

pub fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Tensor1D>

Returns a mutable iterator over the rows of the tensor.

§Examples
let mut tensor = tensor![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
let mut iter = tensor.iter_rows_mut();
source§

impl Tensor

source

pub fn reshape(&self, rows: usize, cols: usize) -> Tensor

Reshapes the tensor to a new shape.

§Examples
let a = tensor![[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]];
let b = tensor![[1.0, 2.0], [4.0, 5.0], [7.0, 8.0], [3.0, 6.0]];
let c = a.reshape(2, 3);
let d = b.reshape(2, 4);
source

pub fn resize(&self, rows: usize, cols: usize) -> Tensor

Returns a resized version of the tensor with the given rows and cols.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
let b = a.resize(2, 2);
let c = b.resize(2, 3);
source

pub fn resize_mut(&mut self, rows: usize, cols: usize)

Returns a resized version of the tensor with the given rows and cols inplace.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
let b = a.resize(2, 2);
let c = b.resize(2, 3);
source

pub fn flatten(&self) -> Tensor1D

Returns a flattened version of the tensor data.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = a.flatten();
source

pub fn resize_to(&self, other: &Tensor) -> Tensor

Returns a resized version of the tensor with the same shape as the given tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
let b = tensor![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];
let c = a.resize_to(&b);
let d = b.resize_to(&a);
source

pub fn clip(&self, min: f64, max: f64) -> Tensor

Returns a clipped version of the tensor with values between the given min and max.

§Examples
let a = tensor![[-1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
let b = a.clip(0.0, 4.0);
source

pub fn slice(&self, start: usize, end: usize) -> Tensor

Returns a slice of the tensor.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
let b = a.slice(1, 3);
source§

impl Tensor

source

pub fn add<T: Rhs>(&self, other: T) -> Tensor

Performs self + rhs.

source

pub fn sub<T: Rhs>(&self, other: T) -> Tensor

Performs self - rhs.

source

pub fn mul<T: Rhs>(&self, other: T) -> Tensor

Performs self * rhs.

source

pub fn div<T: Rhs>(&self, other: T) -> Tensor

Performs self / rhs.

source

pub fn broadcast_and_apply<F>(&self, other: &Tensor, op: F) -> Tensor
where F: Fn(f64, f64) -> f64,

Broadcasts two tensors and applies a function element-wise.

§Examples
let t1 = tensor![[1.0], [2.0]];
let t2 = tensor![3.0, 4.0];
let t3 = t1.broadcast_and_apply(&t2, |a, b| a + b);
source

pub fn broadcast_and_apply_mut<F>(&mut self, other: &Tensor, op: F)
where F: Fn(f64, f64) -> f64,

Broadcasts two tensors and applies a function element-wise in-place.

§Examples
let mut t1 = tensor![[1.0], [2.0]];
let t2 = tensor![3.0, 4.0];
t1.broadcast_and_apply_mut(&t2, |a, b| a + b);
source§

impl Tensor

source

pub fn sparse_matmul(&self, other: &Tensor) -> Tensor

Performs sparse matrix multiplication between two tensors.

§Examples
let a = tensor![[1.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 2.0]];
let b = tensor![[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]];
let c = a.sparse_matmul(&b);
source

pub fn sparse(&self) -> HashMap<(usize, usize), f64>

Returns a vector of the indices of all non-zero elements in the tensor.

§Examples
let a = tensor![[0.0, 2.0], [3.0, 0.0]];
let b = a.sparse();
let expected = HashMap::from([((0, 1), 2.0), ((1, 0), 3.0)]);
source§

impl Tensor

source

pub fn set_grad(&mut self, grad: Vec<Vec<f64>>)

Sets the gradient of the tensor.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.set_grad(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
source

pub fn zero_grad(&mut self)

Zeros out the gradient of the tensor.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.zero_grad();
source§

impl Tensor

source

pub fn transpose(&self) -> Tensor

Returns the transpose of the tensor.

§Examples
let a = tensor![[1.0, 2.0, 3.0], [3.0, 4.0, 6.0]];
let b = a.transpose();
source

pub fn transpose_mut(&mut self)

Transposes the tensor in-place.

§Examples
let mut a = tensor![[1.0, 2.0], [3.0, 4.0]];
a.transpose_mut();
source

pub fn broadcast(&self, other: &Tensor) -> Tensor

Broadcasts the tensor to another tensor’s shape.

§Examples
let a = tensor![[1.0, 2.0], [3.0, 4.0]];
let b = tensor![[1.0, 2.0, 8.0], [3.0, 4.0, 9.0]];
let c = a.broadcast(&b);
source§

impl Tensor

source

pub fn is_symmetric(&self) -> bool

Returns true if the tensor is symmetric, i.e. a square matrix with equal elements across the diagonal.

§Examples
let t = tensor![[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [3.0, 2.0, 1]];
assert!(t.is_symmetric());
source

pub fn is_positive_definite(&self) -> bool

Returns true if the tensor is positive definite, i.e. symmetric and all eigenvalues are positive.

§Examples
let t = tensor![[4.0, 12.0, -16.0], [12.0, 37.0, -43.0], [-16.0, -43.0, 98.0]];
assert!(t.is_positive_definite());

Trait Implementations§

source§

impl Add<&Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Tensor) -> Tensor

Performs the + operation. Read more
source§

impl Add<&Tensor> for Tensor

§

type Output = Tensor

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Tensor) -> Tensor

Performs the + operation. Read more
source§

impl Add<Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the + operator.
source§

fn add(self, rhs: Tensor) -> Tensor

Performs the + operation. Read more
source§

impl Add<f64> for &Tensor

§

type Output = Tensor

The resulting type after applying the + operator.
source§

fn add(self, rhs: f64) -> Tensor

Performs the + operation. Read more
source§

impl Add<f64> for Tensor

§

type Output = Tensor

The resulting type after applying the + operator.
source§

fn add(self, rhs: f64) -> Tensor

Performs the + operation. Read more
source§

impl Add for Tensor

§

type Output = Tensor

The resulting type after applying the + operator.
source§

fn add(self, rhs: Tensor) -> Tensor

Performs the + operation. Read more
source§

impl AddAssign<f64> for Tensor

source§

fn add_assign(&mut self, rhs: f64)

Performs the += operation. Read more
source§

impl AddAssign for Tensor

source§

fn add_assign(&mut self, rhs: Tensor)

Performs the += operation. Read more
source§

impl Clone for Tensor

source§

fn clone(&self) -> Tensor

Returns a copy 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 Tensor

source§

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

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

impl Display for Tensor

source§

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

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

impl Div<&Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the / operator.
source§

fn div(self, rhs: &Tensor) -> Tensor

Performs the / operation. Read more
source§

impl Div<&Tensor> for Tensor

§

type Output = Tensor

The resulting type after applying the / operator.
source§

fn div(self, rhs: &Tensor) -> Tensor

Performs the / operation. Read more
source§

impl Div<Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the / operator.
source§

fn div(self, rhs: Tensor) -> Tensor

Performs the / operation. Read more
source§

impl Div<f64> for &Tensor

§

type Output = Tensor

The resulting type after applying the / operator.
source§

fn div(self, rhs: f64) -> Tensor

Performs the / operation. Read more
source§

impl Div<f64> for Tensor

§

type Output = Tensor

The resulting type after applying the / operator.
source§

fn div(self, rhs: f64) -> Tensor

Performs the / operation. Read more
source§

impl Div for Tensor

§

type Output = Tensor

The resulting type after applying the / operator.
source§

fn div(self, rhs: Tensor) -> Tensor

Performs the / operation. Read more
source§

impl DivAssign<f64> for Tensor

source§

fn div_assign(&mut self, rhs: f64)

Performs the /= operation. Read more
source§

impl DivAssign for Tensor

source§

fn div_assign(&mut self, rhs: Tensor)

Performs the /= operation. Read more
source§

impl From<&Vec<Vec<f64>>> for Tensor

source§

fn from(data: &Tensor2D) -> Self

Converts to this type from the input type.
source§

impl From<&Vec<f64>> for Tensor

source§

fn from(data: &Tensor1D) -> Self

Converts to this type from the input type.
source§

impl From<Vec<Vec<f64>>> for Tensor

source§

fn from(data: Tensor2D) -> Self

Converts to this type from the input type.
source§

impl From<Vec<f64>> for Tensor

source§

fn from(data: Tensor1D) -> Self

Converts to this type from the input type.
source§

impl FromIterator<Vec<f64>> for Tensor

source§

fn from_iter<I: IntoIterator<Item = Tensor1D>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl Mul<&Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Tensor) -> Tensor

Performs the * operation. Read more
source§

impl Mul<&Tensor> for Tensor

§

type Output = Tensor

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Tensor) -> Tensor

Performs the * operation. Read more
source§

impl Mul<Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Tensor) -> Tensor

Performs the * operation. Read more
source§

impl Mul<f64> for &Tensor

§

type Output = Tensor

The resulting type after applying the * operator.
source§

fn mul(self, rhs: f64) -> Tensor

Performs the * operation. Read more
source§

impl Mul<f64> for Tensor

§

type Output = Tensor

The resulting type after applying the * operator.
source§

fn mul(self, rhs: f64) -> Tensor

Performs the * operation. Read more
source§

impl Mul for Tensor

§

type Output = Tensor

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Tensor) -> Tensor

Performs the * operation. Read more
source§

impl MulAssign<f64> for Tensor

source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
source§

impl MulAssign for Tensor

source§

fn mul_assign(&mut self, rhs: Tensor)

Performs the *= operation. Read more
source§

impl PartialEq for Tensor

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sub<&Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Tensor) -> Tensor

Performs the - operation. Read more
source§

impl Sub<&Tensor> for Tensor

§

type Output = Tensor

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Tensor) -> Tensor

Performs the - operation. Read more
source§

impl Sub<Tensor> for &Tensor

§

type Output = Tensor

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Tensor) -> Tensor

Performs the - operation. Read more
source§

impl Sub<f64> for &Tensor

§

type Output = Tensor

The resulting type after applying the - operator.
source§

fn sub(self, rhs: f64) -> Tensor

Performs the - operation. Read more
source§

impl Sub<f64> for Tensor

§

type Output = Tensor

The resulting type after applying the - operator.
source§

fn sub(self, rhs: f64) -> Tensor

Performs the - operation. Read more
source§

impl Sub for Tensor

§

type Output = Tensor

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Tensor) -> Tensor

Performs the - operation. Read more
source§

impl SubAssign<f64> for Tensor

source§

fn sub_assign(&mut self, rhs: f64)

Performs the -= operation. Read more
source§

impl SubAssign for Tensor

source§

fn sub_assign(&mut self, rhs: Tensor)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Tensor

§

impl RefUnwindSafe for Tensor

§

impl Send for Tensor

§

impl Sync for Tensor

§

impl Unpin for Tensor

§

impl UnwindSafe for Tensor

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> 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> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.
§

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

§

fn vzip(self) -> V