pub struct Tensor<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: Scalar> Tensor<T>
impl<T: Scalar> Tensor<T>
Sourcepub fn from_storage(storage: Storage<T>, shape: &[usize]) -> Result<Self>
pub fn from_storage(storage: Storage<T>, shape: &[usize]) -> Result<Self>
Sourcepub fn from_slice(data: &[T], shape: &[usize]) -> Result<Self>
pub fn from_slice(data: &[T], shape: &[usize]) -> Result<Self>
Sourcepub fn randn(shape: &[usize]) -> Self
pub fn randn(shape: &[usize]) -> Self
Creates a tensor with random values from standard normal distribution.
Sourcepub fn rand(shape: &[usize]) -> Self
pub fn rand(shape: &[usize]) -> Self
Creates a tensor with random values from uniform distribution [0, 1).
Sourcepub fn size(&self, dim: i64) -> Result<usize>
pub fn size(&self, dim: i64) -> Result<usize>
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<()>
pub fn set(&self, indices: &[usize], value: T) -> Result<()>
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<Self>
pub fn reshape(&self, new_shape: &[isize]) -> Result<Self>
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<Self>
pub fn squeeze(&self, dim: Option<i64>) -> Result<Self>
Returns a new tensor with dimensions of size 1 removed.
§Arguments
dim- Optional specific dimension to squeeze
Sourcepub fn unsqueeze(&self, dim: i64) -> Result<Self>
pub fn unsqueeze(&self, dim: i64) -> Result<Self>
Returns a new tensor with a dimension of size 1 inserted.
§Arguments
dim- Position to insert the new dimension
Sourcepub fn contiguous(&self) -> Self
pub fn contiguous(&self) -> Self
Returns a contiguous copy of the tensor.
Sourcepub fn clone_deep(&self) -> Self
pub fn clone_deep(&self) -> Self
Creates a deep copy of this tensor with its own storage.
Source§impl<T: Float> Tensor<T>
impl<T: Float> Tensor<T>
Sourcepub fn log_softmax(&self, dim: i32) -> Self
pub fn log_softmax(&self, dim: i32) -> Self
Log softmax along specified dimension.
Sourcepub fn broadcast_to(&self, shape: &[usize]) -> Self
pub fn broadcast_to(&self, shape: &[usize]) -> Self
Broadcasts tensor to a new shape.
Source§impl<T: Numeric> Tensor<T>
impl<T: Numeric> Tensor<T>
Sourcepub fn add_scalar(&self, scalar: T) -> Self
pub fn add_scalar(&self, scalar: T) -> Self
Scalar addition.
Sourcepub fn mul_scalar(&self, scalar: T) -> Self
pub fn mul_scalar(&self, scalar: T) -> Self
Scalar multiplication.
Source§impl<T: Scalar> Tensor<T>
impl<T: Scalar> Tensor<T>
Sourcepub fn slice_dim0(&self, start: usize, end: usize) -> Result<Self>
pub fn slice_dim0(&self, start: usize, end: usize) -> Result<Self>
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<Self>
pub fn select(&self, dim: usize, index: usize) -> Result<Self>
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<Self>
pub fn narrow(&self, dim: usize, start: usize, length: usize) -> Result<Self>
Returns a narrow view along a dimension.
§Arguments
dim- Dimension to narrowstart- Start indexlength- Length of the narrow view
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