Tensor

Struct Tensor 

Source
pub struct Tensor<T>
where T: Scalar,
{ /* private fields */ }
Expand description

An N-dimensional array of numeric values.

Tensors are the core data structure for all computations in Axonml. They support arbitrary dimensions, automatic broadcasting, and efficient memory sharing between views.

Implementations§

Source§

impl<T> Tensor<T>
where T: Scalar,

Source

pub fn from_storage( storage: Storage<T>, shape: &[usize], ) -> Result<Tensor<T>, Error>

Creates a new tensor from storage with the given shape.

§Arguments
  • storage - The underlying data storage
  • shape - Shape of the tensor
§Returns

New tensor, or error if shape doesn’t match storage size.

Source

pub fn from_vec(data: Vec<T>, shape: &[usize]) -> Result<Tensor<T>, Error>

Creates a new tensor from a vector with the given shape.

§Arguments
  • data - Vector of data
  • shape - Shape of the tensor
§Returns

New tensor, or error if shape doesn’t match data length.

Source

pub fn from_slice(data: &[T], shape: &[usize]) -> Result<Tensor<T>, Error>

Creates a new tensor from a slice with the given shape.

§Arguments
  • data - Slice of data to copy
  • shape - Shape of the tensor
§Returns

New tensor, or error if shape doesn’t match data length.

Source

pub fn scalar(value: T) -> Tensor<T>

Creates a scalar tensor (0-dimensional).

§Arguments
  • value - The scalar value
§Returns

New 0-dimensional tensor.

Source

pub fn zeros(shape: &[usize]) -> Tensor<T>

Creates a tensor filled with zeros.

Source

pub fn ones(shape: &[usize]) -> Tensor<T>
where T: Numeric,

Creates a tensor filled with ones.

Source

pub fn full(shape: &[usize], value: T) -> Tensor<T>

Creates a tensor filled with a constant value.

Source

pub fn randn(shape: &[usize]) -> Tensor<T>

Creates a tensor with random values from standard normal distribution.

Source

pub fn rand(shape: &[usize]) -> Tensor<T>
where T: Float, Standard: Distribution<T>,

Creates a tensor with random values from uniform distribution [0, 1).

Source

pub fn shape(&self) -> &[usize]

Returns the shape of the tensor.

Source

pub fn strides(&self) -> &[isize]

Returns the strides 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 is_empty(&self) -> bool

Returns true if the tensor is empty (has zero elements).

Source

pub fn size(&self, dim: i64) -> Result<usize, Error>

Returns the size of a specific dimension.

§Arguments
  • dim - Dimension index (supports negative indexing)
Source

pub fn device(&self) -> Device

Returns the device this tensor is on.

Source

pub fn is_contiguous(&self) -> bool

Returns true if the tensor is contiguous in memory.

Source

pub fn is_scalar(&self) -> bool

Returns true if this tensor is a scalar (0-dimensional).

Source

pub fn get(&self, indices: &[usize]) -> Result<T, Error>

Returns the element at the given indices.

§Arguments
  • indices - Multi-dimensional indices
Source

pub fn set(&self, indices: &[usize], value: T) -> Result<(), Error>

Sets the element at the given indices.

§Arguments
  • indices - Multi-dimensional indices
  • value - Value to set
Source

pub fn item(&self) -> Result<T, Error>

Returns the scalar value for a 0-dimensional tensor.

Source

pub fn to_vec(&self) -> Vec<T>

Returns the data as a contiguous vector.

If the tensor is already contiguous, this returns a reference. Otherwise, it copies the data into a new contiguous vector.

Source

pub fn reshape(&self, new_shape: &[isize]) -> Result<Tensor<T>, Error>

Returns a new tensor with the specified shape.

The total number of elements must remain the same. Supports -1 in one dimension to infer the size.

§Arguments
  • new_shape - Target shape
Source

pub fn flatten(&self) -> Tensor<T>

Returns a new tensor with a flattened shape.

Source

pub fn squeeze(&self, dim: Option<i64>) -> Result<Tensor<T>, Error>

Returns a new tensor with dimensions of size 1 removed.

§Arguments
  • dim - Optional specific dimension to squeeze
Source

pub fn unsqueeze(&self, dim: i64) -> Result<Tensor<T>, Error>

Returns a new tensor with a dimension of size 1 inserted.

§Arguments
  • dim - Position to insert the new dimension
Source

pub fn transpose(&self, dim0: i64, dim1: i64) -> Result<Tensor<T>, Error>

Transposes two dimensions.

§Arguments
  • dim0 - First dimension
  • dim1 - Second dimension
Source

pub fn t(&self) -> Result<Tensor<T>, Error>

Returns the transpose of a 2D tensor.

Source

pub fn permute(&self, dims: &[usize]) -> Result<Tensor<T>, Error>

Returns a permuted tensor with dimensions reordered.

§Arguments
  • dims - New order of dimensions
Source

pub fn contiguous(&self) -> Tensor<T>

Returns a contiguous copy of the tensor.

Source

pub fn to_device(&self, device: Device) -> Result<Tensor<T>, Error>

Transfers the tensor to a different device.

§Arguments
  • device - Target device
Source

pub fn cpu(&self) -> Result<Tensor<T>, Error>

Transfers to CPU.

Source

pub fn clone_deep(&self) -> Tensor<T>

Creates a deep copy of this tensor with its own storage.

Source§

impl<T> Tensor<T>
where T: Numeric,

Source

pub fn fill_(&self, value: T)

Fills the tensor with a value.

Source

pub fn zero_(&self)

Fills the tensor with zeros.

Source

pub fn sum(&self) -> Tensor<T>

Returns the sum of all elements.

Source

pub fn prod(&self) -> Tensor<T>

Returns the product of all elements.

Source

pub fn max(&self) -> Result<Tensor<T>, Error>

Returns the maximum element.

Source

pub fn min(&self) -> Result<Tensor<T>, Error>

Returns the minimum element.

Source

pub fn argmax(&self) -> Result<usize, Error>

Returns the index of the maximum element.

Source

pub fn argmin(&self) -> Result<usize, Error>

Returns the index of the minimum element.

Source§

impl<T> Tensor<T>
where T: Float,

Source

pub fn mean(&self) -> Result<Tensor<T>, Error>

Returns the mean of all elements.

Source

pub fn relu(&self) -> Tensor<T>

Applies ReLU activation: max(0, x).

Source

pub fn sigmoid(&self) -> Tensor<T>

Applies sigmoid activation: 1 / (1 + exp(-x)).

Source

pub fn tanh(&self) -> Tensor<T>

Applies tanh activation.

Source

pub fn exp(&self) -> Tensor<T>

Applies exponential function.

Source

pub fn ln(&self) -> Tensor<T>

Applies natural logarithm.

Source

pub fn sqrt(&self) -> Tensor<T>

Applies square root.

Source

pub fn pow(&self, exp: T) -> Tensor<T>

Computes element-wise power.

Source

pub fn gelu(&self) -> Tensor<T>

GELU activation function (Gaussian Error Linear Unit).

Source

pub fn silu(&self) -> Tensor<T>

SiLU/Swish activation function.

Source

pub fn softmax(&self, dim: i32) -> Tensor<T>

Softmax along specified dimension.

Source

pub fn log_softmax(&self, dim: i32) -> Tensor<T>

Log softmax along specified dimension.

Source

pub fn mean_dim(&self, dim: i32, keepdim: bool) -> Tensor<T>

Mean along a dimension.

Source

pub fn var_dim(&self, dim: i32, keepdim: bool) -> Tensor<T>

Variance along a dimension.

Source

pub fn broadcast_to(&self, shape: &[usize]) -> Tensor<T>

Broadcasts tensor to a new shape.

Source

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

Slices the tensor using ranges for each dimension.

Source§

impl<T> Tensor<T>
where T: Numeric,

Source

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

Element-wise addition with broadcasting.

Source

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

Element-wise subtraction with broadcasting.

Source

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

Element-wise multiplication with broadcasting.

Source

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

Element-wise division with broadcasting.

Source

pub fn add_scalar(&self, scalar: T) -> Tensor<T>

Scalar addition.

Source

pub fn mul_scalar(&self, scalar: T) -> Tensor<T>

Scalar multiplication.

Source

pub fn neg(&self) -> Tensor<T>

Element-wise negation.

Source

pub fn matmul(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>

Matrix multiplication with batching support.

Supports:

  • 2D @ 2D: [m, k] @ [k, n] -> [m, n]
  • 3D @ 3D: [batch, m, k] @ [batch, k, n] -> [batch, m, n]
  • 4D @ 4D: [b1, b2, m, k] @ [b1, b2, k, n] -> [b1, b2, m, n]
Source

pub fn dot(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>

Dot product for 1D tensors.

Source§

impl<T> Tensor<T>
where T: Scalar,

Source

pub fn slice_dim0(&self, start: usize, end: usize) -> Result<Tensor<T>, Error>

Returns a slice of the tensor along the first dimension.

§Arguments
  • start - Start index (inclusive)
  • end - End index (exclusive)
Source

pub fn select(&self, dim: usize, index: usize) -> Result<Tensor<T>, Error>

Returns a view selecting a single index along a dimension.

This reduces the dimensionality by 1.

§Arguments
  • dim - Dimension to select from
  • index - Index to select
Source

pub fn narrow( &self, dim: usize, start: usize, length: usize, ) -> Result<Tensor<T>, Error>

Returns a narrow view along a dimension.

§Arguments
  • dim - Dimension to narrow
  • start - Start index
  • length - Length of the narrow view
Source

pub fn chunk(&self, chunks: usize, dim: usize) -> Result<Vec<Tensor<T>>, Error>

Splits the tensor into chunks along a dimension.

§Arguments
  • chunks - Number of chunks
  • dim - Dimension to split along
Source

pub fn split( &self, sizes: &[usize], dim: usize, ) -> Result<Vec<Tensor<T>>, Error>

Splits the tensor into parts of specified sizes along a dimension.

§Arguments
  • sizes - Size of each part
  • dim - Dimension to split along
Source§

impl<T> Tensor<T>
where T: Numeric,

Source

pub fn gather( &self, dim: usize, indices: &Tensor<i64>, ) -> Result<Tensor<T>, Error>

Gathers values along a dimension according to indices.

§Arguments
  • dim - Dimension to gather along
  • indices - Indices tensor
Source

pub fn masked_select(&self, mask: &[bool]) -> Result<Tensor<T>, Error>

Returns elements selected by a boolean mask.

§Arguments
  • mask - Boolean mask tensor
Source

pub fn masked_fill_(&self, mask: &[bool], value: T) -> Result<(), Error>

Sets elements according to a boolean mask.

§Arguments
  • mask - Boolean mask tensor
  • value - Value to set where mask is true

Trait Implementations§

Source§

impl<T> Add<T> for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the + operator.
Source§

fn add(self, scalar: T) -> <&Tensor<T> as Add<T>>::Output

Performs the + operation. Read more
Source§

impl<T> Add for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: &Tensor<T>) -> <&Tensor<T> as Add>::Output

Performs the + operation. Read more
Source§

impl<T> Clone for Tensor<T>
where T: Clone + Scalar,

Source§

fn clone(&self) -> Tensor<T>

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 Collate<Tensor<f32>> for DefaultCollate

Source§

type Output = Tensor<f32>

The output batch type.
Source§

fn collate(&self, batch: Vec<Tensor<f32>>) -> Self::Output

Collates a vector of samples into a batch.
Source§

impl Collate<Tensor<f32>> for StackCollate

Source§

type Output = Tensor<f32>

The output batch type.
Source§

fn collate(&self, batch: Vec<Tensor<f32>>) -> Self::Output

Collates a vector of samples into a batch.
Source§

impl<T> Debug for Tensor<T>
where T: Scalar + Display,

Source§

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

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

impl<T> Display for Tensor<T>
where T: Scalar + Display,

Source§

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

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

impl<T> Div for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: &Tensor<T>) -> <&Tensor<T> as Div>::Output

Performs the / operation. Read more
Source§

impl<T> Mul<T> for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> <&Tensor<T> as Mul<T>>::Output

Performs the * operation. Read more
Source§

impl<T> Mul for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Tensor<T>) -> <&Tensor<T> as Mul>::Output

Performs the * operation. Read more
Source§

impl<T> Neg for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> <&Tensor<T> as Neg>::Output

Performs the unary - operation. Read more
Source§

impl<T> Sub for &Tensor<T>
where T: Numeric,

Source§

type Output = Tensor<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: &Tensor<T>) -> <&Tensor<T> as Sub>::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Tensor<T>

§

impl<T> !RefUnwindSafe for Tensor<T>

§

impl<T> Send for Tensor<T>

§

impl<T> Sync for Tensor<T>

§

impl<T> Unpin for Tensor<T>

§

impl<T> !UnwindSafe for Tensor<T>

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

Source§

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

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