pub struct Tensor<T> {
pub storage: TensorStorage<T>,
/* private fields */
}Expand description
Core tensor structure that holds data and metadata
Fields§
§storage: TensorStorage<T>Implementations§
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn mul_scalar(&self, scalar: T) -> Result<Tensor<T>>
pub fn mul_scalar(&self, scalar: T) -> Result<Tensor<T>>
Convenience method to multiply tensor by a scalar
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn eq(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialEq,
pub fn eq(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialEq,
Element-wise equality comparison
Sourcepub fn ne(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialEq,
pub fn ne(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialEq,
Element-wise not-equal comparison
Sourcepub fn gt(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialOrd,
pub fn gt(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialOrd,
Element-wise greater-than comparison
Sourcepub fn ge(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialOrd,
pub fn ge(&self, other: &Self) -> Result<Tensor<bool>>where
T: PartialOrd,
Element-wise greater-than-or-equal comparison
Source§impl Tensor<bool>
impl Tensor<bool>
Sourcepub fn cast_to_u8(&self) -> Result<Tensor<u8>>
pub fn cast_to_u8(&self) -> Result<Tensor<u8>>
Cast boolean tensor to u8 tensor (false -> 0, true -> 1)
Sourcepub fn logical_and(&self, other: &Self) -> Result<Self>
pub fn logical_and(&self, other: &Self) -> Result<Self>
Element-wise logical AND operation
Sourcepub fn logical_or(&self, other: &Self) -> Result<Self>
pub fn logical_or(&self, other: &Self) -> Result<Self>
Element-wise logical OR operation
Sourcepub fn logical_not(&self) -> Result<Self>
pub fn logical_not(&self) -> Result<Self>
Element-wise logical NOT operation
Sourcepub fn logical_xor(&self, other: &Self) -> Result<Self>
pub fn logical_xor(&self, other: &Self) -> Result<Self>
Element-wise logical XOR operation
Source§impl Tensor<u8>
impl Tensor<u8>
Sourcepub fn cast_to_bool(&self) -> Result<Tensor<bool>>
pub fn cast_to_bool(&self) -> Result<Tensor<bool>>
Cast u8 tensor to boolean tensor (0 -> false, non-zero -> true)
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn requires_grad(&self) -> bool
pub fn requires_grad(&self) -> bool
Check if tensor requires gradient computation
Sourcepub fn set_requires_grad(&mut self, requires_grad: bool)
pub fn set_requires_grad(&mut self, requires_grad: bool)
Set whether tensor requires gradient computation
Sourcepub fn get(&self, index: &[usize]) -> Option<T>where
T: Clone,
pub fn get(&self, index: &[usize]) -> Option<T>where
T: Clone,
Get the value at a specific index (for CPU tensors only)
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Get memory usage in bytes
Sourcepub fn same_shape(&self, other: &Self) -> bool
pub fn same_shape(&self, other: &Self) -> bool
Check if two tensors have the same shape
Sourcepub fn is_broadcastable_with(&self, other: &Self) -> bool
pub fn is_broadcastable_with(&self, other: &Self) -> bool
Check if tensors are broadcastable
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if tensor data is contiguous in memory
Source§impl<T: Clone + Default> Tensor<T>
impl<T: Clone + Default> Tensor<T>
Sourcepub fn from_data(data: Vec<T>, shape: &[usize]) -> Result<Self>
pub fn from_data(data: Vec<T>, shape: &[usize]) -> Result<Self>
Create a tensor from raw data vector with specified shape
Sourcepub fn from_array(array: ArrayD<T>) -> Self
pub fn from_array(array: ArrayD<T>) -> Self
Create a tensor from an existing ndarray
Sourcepub fn randn(shape: &[usize]) -> Result<Self>
pub fn randn(shape: &[usize]) -> Result<Self>
Create a tensor filled with random values from normal distribution
Sourcepub fn from_storage(storage: TensorStorage<T>, device: Device) -> Self
pub fn from_storage(storage: TensorStorage<T>, device: Device) -> Self
Create a tensor from storage and device
Sourcepub fn from_scalar(value: T) -> Self
pub fn from_scalar(value: T) -> Self
Create a scalar tensor from a single value
Sourcepub fn from_vec(data: Vec<T>, shape: &[usize]) -> Result<Self>
pub fn from_vec(data: Vec<T>, shape: &[usize]) -> Result<Self>
Create a tensor from a vector of data with specified shape
Sourcepub fn full(shape: &[usize], value: T) -> Selfwhere
T: Clone,
pub fn full(shape: &[usize], value: T) -> Selfwhere
T: Clone,
Create a tensor filled with a specific value
Sourcepub fn arange(start: T, end: T, step: T) -> Result<Self>
pub fn arange(start: T, end: T, step: T) -> Result<Self>
Create a tensor with evenly spaced values in a given interval
Source§impl<T: Clone> Tensor<T>
impl<T: Clone> Tensor<T>
Sourcepub fn to_device(&self, target_device: Device) -> Result<Self>
pub fn to_device(&self, target_device: Device) -> Result<Self>
Transfer tensor to a different device
Sourcepub fn copy_from_device(&mut self, src: &Self) -> Result<()>
pub fn copy_from_device(&mut self, src: &Self) -> Result<()>
Copy tensor data from another device (for collective operations)
Sourcepub fn can_transfer_to(&self, target_device: Device) -> boolwhere
T: Pod,
pub fn can_transfer_to(&self, target_device: Device) -> boolwhere
T: Pod,
Check if tensor can be transferred to target device
Source§impl<T: Clone> Tensor<T>
impl<T: Clone> Tensor<T>
Source§impl<T> Tensor<T>
impl<T> Tensor<T>
Sourcepub fn sub(&self, other: &Self) -> Result<Self>where
T: Sub<Output = T>,
pub fn sub(&self, other: &Self) -> Result<Self>where
T: Sub<Output = T>,
Element-wise subtraction
Sourcepub fn mul(&self, other: &Self) -> Result<Self>where
T: Mul<Output = T>,
pub fn mul(&self, other: &Self) -> Result<Self>where
T: Mul<Output = T>,
Element-wise multiplication
Sourcepub fn leaky_relu(&self, alpha: T) -> Result<Self>
pub fn leaky_relu(&self, alpha: T) -> Result<Self>
Leaky ReLU activation function
Sourcepub fn hard_swish(&self) -> Result<Self>where
T: Float + PartialOrd,
pub fn hard_swish(&self) -> Result<Self>where
T: Float + PartialOrd,
Hard Swish activation function
Sourcepub fn prelu(&self, alpha: &Self) -> Result<Self>where
T: Float + PartialOrd,
pub fn prelu(&self, alpha: &Self) -> Result<Self>where
T: Float + PartialOrd,
Parametric ReLU activation function
Sourcepub fn slice(&self, ranges: &[Range<usize>]) -> Result<Self>
pub fn slice(&self, ranges: &[Range<usize>]) -> Result<Self>
Slice tensor along specified ranges
Sourcepub fn slice_with_stride(&self, slice_params: &[SliceParams]) -> Result<Self>
pub fn slice_with_stride(&self, slice_params: &[SliceParams]) -> Result<Self>
Slice tensor with stride parameters
Sourcepub fn sum(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: Zero,
pub fn sum(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: Zero,
Sum tensor along specified axes
Sourcepub fn mean(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: Float + FromPrimitive,
pub fn mean(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: Float + FromPrimitive,
Mean tensor along specified axes
Sourcepub fn max(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: PartialOrd,
pub fn max(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: PartialOrd,
Maximum values along specified axes
Sourcepub fn min(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: PartialOrd,
pub fn min(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>where
T: PartialOrd,
Minimum values along specified axes
Sourcepub fn squeeze(&self, axes: Option<&[usize]>) -> Result<Self>
pub fn squeeze(&self, axes: Option<&[usize]>) -> Result<Self>
Squeeze tensor - remove dimensions of size 1
Sourcepub fn unsqueeze(&self, axes: &[usize]) -> Result<Self>
pub fn unsqueeze(&self, axes: &[usize]) -> Result<Self>
Unsqueeze tensor - add dimensions of size 1
Sourcepub fn scalar_mul(&self, scalar: T) -> Result<Self>
pub fn scalar_mul(&self, scalar: T) -> Result<Self>
Scalar multiplication
Sourcepub fn max_axis(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
pub fn max_axis(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
Maximum values along specified axes
Sourcepub fn sum_axis(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
pub fn sum_axis(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
Sum along specified axes
Sourcepub fn clamp(&self, min: T, max: T) -> Result<Self>where
T: PartialOrd + Clone,
pub fn clamp(&self, min: T, max: T) -> Result<Self>where
T: PartialOrd + Clone,
Clamp tensor values between min and max
Sourcepub fn allclose(&self, other: &Self, rtol: T, atol: T) -> Result<bool>
pub fn allclose(&self, other: &Self, rtol: T, atol: T) -> Result<bool>
Check if all elements are close to another tensor within tolerance
Sourcepub fn fill_(&mut self, value: T) -> Result<()>where
T: Clone,
pub fn fill_(&mut self, value: T) -> Result<()>where
T: Clone,
Fill tensor with specified value
Sourcepub fn to_scalar(&self) -> Result<T>where
T: Clone,
pub fn to_scalar(&self) -> Result<T>where
T: Clone,
Extract scalar value from a 0-dimensional tensor
Sourcepub fn argmax(&self, axis: i32) -> Result<Tensor<usize>>where
T: PartialOrd + Clone,
pub fn argmax(&self, axis: i32) -> Result<Tensor<usize>>where
T: PartialOrd + Clone,
Find the indices of the maximum values along the specified axis
Sourcepub fn flatten(&self) -> Result<Self>
pub fn flatten(&self) -> Result<Self>
Flatten the tensor into a 1D tensor
This operation reshapes the tensor into a 1-dimensional tensor containing the same elements in row-major (C-style) order.
§Returns
A 1D tensor containing all elements from the input tensor
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("from_vec should succeed");
let flattened = tensor.flatten().expect("flatten should not fail");
assert_eq!(flattened.shape().dims(), &[4]);Sourcepub fn cumsum(&self, axis: Option<i32>) -> Result<Self>
pub fn cumsum(&self, axis: Option<i32>) -> Result<Self>
Compute the cumulative sum of elements along the given axis
§Arguments
axis- Axis along which to compute the cumulative sum. If None, flatten the tensor first.
§Returns
A tensor with cumulative sums along the specified axis
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("from_vec should succeed");
let cumsum = tensor.cumsum(Some(0)).expect("operation should succeed");Sourcepub fn cumprod(&self, axis: Option<i32>) -> Result<Self>
pub fn cumprod(&self, axis: Option<i32>) -> Result<Self>
Compute the cumulative product of elements along the given axis
§Arguments
axis- Axis along which to compute the cumulative product. If None, flatten the tensor first.
§Returns
A tensor with cumulative products along the specified axis
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("from_vec should succeed");
let cumprod = tensor.cumprod(Some(0)).expect("operation should succeed");Sourcepub fn tile(&self, multiples: &[usize]) -> Result<Self>
pub fn tile(&self, multiples: &[usize]) -> Result<Self>
Tile the tensor by repeating it along each axis
§Arguments
multiples- The number of repetitions along each axis
§Returns
A tensor with the input tiled according to the multiples
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[1, 2]).expect("from_vec should succeed");
let tiled = tensor.tile(&[2, 3]).expect("tile should succeed");
assert_eq!(tiled.shape().dims(), &[2, 6]);Sourcepub fn repeat(&self, repeats: usize, axis: Option<usize>) -> Result<Self>
pub fn repeat(&self, repeats: usize, axis: Option<usize>) -> Result<Self>
Repeat elements of the tensor
§Arguments
repeats- The number of repetitions for each elementaxis- The axis along which to repeat values. If None, the input tensor is flattened first.
§Returns
A tensor with repeated elements
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0], &[3]).expect("from_vec should succeed");
let repeated = tensor.repeat(2, Some(0)).expect("operation should succeed");
assert_eq!(repeated.shape().dims(), &[6]);Sourcepub fn broadcast_to(&self, target_shape: &[usize]) -> Result<Self>
pub fn broadcast_to(&self, target_shape: &[usize]) -> Result<Self>
Broadcast the tensor to a new shape
§Arguments
target_shape- The shape to broadcast to
§Returns
A tensor broadcasted to the target shape
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[1, 2]).expect("from_vec should succeed");
let broadcasted = tensor.broadcast_to(&[3, 2]).expect("broadcast_to should succeed");
assert_eq!(broadcasted.shape().dims(), &[3, 2]);Sourcepub fn expand_as(&self, target: &Self) -> Result<Self>
pub fn expand_as(&self, target: &Self) -> Result<Self>
Expand tensor dimensions to match another tensor’s shape
§Arguments
target- The tensor whose shape to match
§Returns
A tensor expanded to match the target tensor’s shape
§Examples
use tenflowers_core::Tensor;
let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[1, 2]).expect("from_vec should succeed");
let target = Tensor::<f32>::zeros(&[3, 2]);
let expanded = tensor.expand_as(&target).expect("expand_as should succeed");
assert_eq!(expanded.shape().dims(), &[3, 2]);Sourcepub fn multiply_scalar(&self, scalar: T) -> Result<Self>
pub fn multiply_scalar(&self, scalar: T) -> Result<Self>
Scalar multiplication
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Tensor<T>
impl<T> RefUnwindSafe for Tensor<T>where
T: RefUnwindSafe,
impl<T> Send for Tensor<T>
impl<T> Sync for Tensor<T>
impl<T> Unpin for Tensor<T>
impl<T> UnsafeUnpin for Tensor<T>
impl<T> UnwindSafe for Tensor<T>where
T: RefUnwindSafe,
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