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,
impl<T> Tensor<T>where
T: Scalar,
Sourcepub fn full(shape: &[usize], value: T) -> Tensor<T>
pub fn full(shape: &[usize], value: T) -> Tensor<T>
Creates a tensor filled with a constant value.
Sourcepub fn randn(shape: &[usize]) -> Tensor<T>
pub fn randn(shape: &[usize]) -> Tensor<T>
Creates a tensor with random values from standard normal distribution.
Sourcepub fn rand(shape: &[usize]) -> Tensor<T>
pub fn rand(shape: &[usize]) -> Tensor<T>
Creates a tensor with random values from uniform distribution [0, 1).
Sourcepub fn size(&self, dim: i64) -> Result<usize, Error>
pub fn size(&self, dim: i64) -> Result<usize, Error>
Returns the size of a specific dimension.
§Arguments
dim- Dimension index (supports negative indexing)
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Returns true if the tensor is contiguous in memory.
Sourcepub fn set(&self, indices: &[usize], value: T) -> Result<(), Error>
pub fn set(&self, indices: &[usize], value: T) -> Result<(), Error>
Sets the element at the given indices.
§Arguments
indices- Multi-dimensional indicesvalue- Value to set
Sourcepub fn to_vec(&self) -> Vec<T>
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.
Sourcepub fn reshape(&self, new_shape: &[isize]) -> Result<Tensor<T>, Error>
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
Sourcepub fn squeeze(&self, dim: Option<i64>) -> Result<Tensor<T>, Error>
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
Sourcepub fn unsqueeze(&self, dim: i64) -> Result<Tensor<T>, Error>
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
Sourcepub fn contiguous(&self) -> Tensor<T>
pub fn contiguous(&self) -> Tensor<T>
Returns a contiguous copy of the tensor.
Sourcepub fn clone_deep(&self) -> Tensor<T>
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: Float,
impl<T> Tensor<T>where
T: Float,
Sourcepub fn log_softmax(&self, dim: i32) -> Tensor<T>
pub fn log_softmax(&self, dim: i32) -> Tensor<T>
Log softmax along specified dimension.
Sourcepub fn broadcast_to(&self, shape: &[usize]) -> Tensor<T>
pub fn broadcast_to(&self, shape: &[usize]) -> Tensor<T>
Broadcasts tensor to a new shape.
Source§impl<T> Tensor<T>where
T: Numeric,
impl<T> Tensor<T>where
T: Numeric,
Sourcepub fn add(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
pub fn add(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
Element-wise addition with broadcasting.
Sourcepub fn sub(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
pub fn sub(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
Element-wise subtraction with broadcasting.
Sourcepub fn mul(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
pub fn mul(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
Element-wise multiplication with broadcasting.
Sourcepub fn div(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
pub fn div(&self, other: &Tensor<T>) -> Result<Tensor<T>, Error>
Element-wise division with broadcasting.
Sourcepub fn add_scalar(&self, scalar: T) -> Tensor<T>
pub fn add_scalar(&self, scalar: T) -> Tensor<T>
Scalar addition.
Sourcepub fn mul_scalar(&self, scalar: T) -> Tensor<T>
pub fn mul_scalar(&self, scalar: T) -> Tensor<T>
Scalar multiplication.
Source§impl<T> Tensor<T>where
T: Scalar,
impl<T> Tensor<T>where
T: Scalar,
Sourcepub fn slice_dim0(&self, start: usize, end: usize) -> Result<Tensor<T>, Error>
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)
Sourcepub fn select(&self, dim: usize, index: usize) -> Result<Tensor<T>, Error>
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 fromindex- Index to select
Sourcepub fn narrow(
&self,
dim: usize,
start: usize,
length: usize,
) -> Result<Tensor<T>, Error>
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 narrowstart- Start indexlength- Length of the narrow view
Source§impl<T> Tensor<T>where
T: Numeric,
impl<T> Tensor<T>where
T: Numeric,
Trait Implementations§
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> 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