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
impl Tensor
sourcepub fn shape(&self) -> (usize, usize)
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§impl Tensor
impl Tensor
sourcepub fn matmul(&self, other: &Tensor) -> Tensor
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);
sourcepub fn mapv(&self, function: &dyn Fn(f64) -> f64) -> Tensor
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);
sourcepub fn mapv_mut(&mut self, function: &dyn Fn(f64) -> f64)
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);
sourcepub fn square(&self) -> Tensor
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();
sourcepub fn square_mut(&mut self)
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();
sourcepub fn sqrt(&self) -> Tensor
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();
sourcepub fn sqrt_mut(&mut self)
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();
sourcepub fn pow(&self, exponent: f64) -> Tensor
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);
sourcepub fn pow_mut(&mut self, exponent: f64)
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);
sourcepub fn ln(&self) -> Tensor
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();
sourcepub fn ln_mut(&mut self)
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();
sourcepub fn log2(&self) -> Tensor
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();
sourcepub fn log2_mut(&mut self)
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();
sourcepub fn abs(&self) -> Tensor
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();
sourcepub fn dot(&self, other: &Tensor) -> f64
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);
sourcepub fn sum(&self) -> f64
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();
sourcepub fn sum_axis(&self, axis: u8) -> Tensor
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);
sourcepub fn mean(&self) -> f64
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();
sourcepub fn norm(&self, p: f64) -> f64
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);
sourcepub fn activate(&self, activation: &Activation) -> Tensor
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);
sourcepub fn activate_mut(&mut self, activation: &Activation)
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);
sourcepub fn grad(&mut self, activation: &Activation) -> Tensor
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
impl Tensor
sourcepub fn new(rows: usize, cols: usize, value: f64) -> Tensor
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);
sourcepub fn new_like(other: &Tensor, value: f64) -> Tensor
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);
pub fn zeros(rows: usize, cols: usize) -> Tensor
pub fn zeros_like(other: &Tensor) -> Tensor
pub fn ones(rows: usize, cols: usize) -> Tensor
pub fn ones_like(other: &Tensor) -> Tensor
sourcepub fn identity(size: usize) -> Tensor
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);
sourcepub fn initialize(rows: usize, cols: usize, initializer: &Initializer) -> Tensor
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
impl Tensor
sourcepub fn iter_rows(&self) -> impl Iterator<Item = &Tensor1D>
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();
sourcepub fn iter_rows_mut(&mut self) -> impl Iterator<Item = &mut Tensor1D>
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
impl Tensor
sourcepub fn reshape(&self, rows: usize, cols: usize) -> Tensor
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);
sourcepub fn resize(&self, rows: usize, cols: usize) -> Tensor
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);
sourcepub fn resize_mut(&mut self, rows: usize, cols: usize)
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);
sourcepub fn flatten(&self) -> Tensor1D
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();
sourcepub fn resize_to(&self, other: &Tensor) -> Tensor
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§impl Tensor
impl Tensor
sourcepub fn broadcast_and_apply<F>(&self, other: &Tensor, op: F) -> Tensor
pub fn broadcast_and_apply<F>(&self, other: &Tensor, op: F) -> Tensor
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§impl Tensor
impl Tensor
sourcepub fn sparse_matmul(&self, other: &Tensor) -> Tensor
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§impl Tensor
impl Tensor
source§impl Tensor
impl Tensor
sourcepub fn transpose(&self) -> Tensor
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();
sourcepub fn transpose_mut(&mut self)
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§impl Tensor
impl Tensor
sourcepub fn is_symmetric(&self) -> bool
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());
sourcepub fn is_positive_definite(&self) -> bool
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 AddAssign<f64> for Tensor
impl AddAssign<f64> for Tensor
source§fn add_assign(&mut self, rhs: f64)
fn add_assign(&mut self, rhs: f64)
+=
operation. Read moresource§impl AddAssign for Tensor
impl AddAssign for Tensor
source§fn add_assign(&mut self, rhs: Tensor)
fn add_assign(&mut self, rhs: Tensor)
+=
operation. Read moresource§impl DivAssign<f64> for Tensor
impl DivAssign<f64> for Tensor
source§fn div_assign(&mut self, rhs: f64)
fn div_assign(&mut self, rhs: f64)
/=
operation. Read moresource§impl DivAssign for Tensor
impl DivAssign for Tensor
source§fn div_assign(&mut self, rhs: Tensor)
fn div_assign(&mut self, rhs: Tensor)
/=
operation. Read moresource§impl MulAssign<f64> for Tensor
impl MulAssign<f64> for Tensor
source§fn mul_assign(&mut self, rhs: f64)
fn mul_assign(&mut self, rhs: f64)
*=
operation. Read moresource§impl MulAssign for Tensor
impl MulAssign for Tensor
source§fn mul_assign(&mut self, rhs: Tensor)
fn mul_assign(&mut self, rhs: Tensor)
*=
operation. Read more