pub struct Tensor { /* private fields */ }Expand description
The core Tensor type
Tensors are multi-dimensional arrays with:
- Shared storage (enables zero-copy views)
- Shape and strides (enables non-contiguous layouts)
- Optional gradient tracking for autograd
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn from_slice<T: TensorElement>(data: &[T], shape: &[usize]) -> Result<Self>
pub fn from_slice<T: TensorElement>(data: &[T], shape: &[usize]) -> Result<Self>
Create a new tensor from a flat slice and shape
Sourcepub fn full<T: TensorElement>(shape: &[usize], value: T) -> Self
pub fn full<T: TensorElement>(shape: &[usize], value: T) -> Self
Create a tensor filled with a constant value
Sourcepub fn rand(shape: &[usize]) -> Self
pub fn rand(shape: &[usize]) -> Self
Create a tensor with random values from uniform distribution [0, 1)
Sourcepub fn randn(shape: &[usize]) -> Self
pub fn randn(shape: &[usize]) -> Self
Create a tensor with random values from standard normal distribution
Sourcepub fn arange(start: f32, end: f32, step: f32) -> Self
pub fn arange(start: f32, end: f32, step: f32) -> Self
Create a 1D tensor with evenly spaced values
Sourcepub fn linspace(start: f32, end: f32, n: usize) -> Self
pub fn linspace(start: f32, end: f32, n: usize) -> Self
Create a 1D tensor with n evenly spaced values between start and end
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if tensor is contiguous in memory
Sourcepub fn requires_grad(&self) -> bool
pub fn requires_grad(&self) -> bool
Check if gradient tracking is enabled
Sourcepub fn set_requires_grad(&mut self, requires_grad: bool)
pub fn set_requires_grad(&mut self, requires_grad: bool)
Enable gradient tracking
Sourcepub fn reshape(&self, new_shape: &[usize]) -> Result<Tensor>
pub fn reshape(&self, new_shape: &[usize]) -> Result<Tensor>
Reshape tensor to new shape (must have same numel)
Sourcepub fn unsqueeze(&self, dim: usize) -> Result<Tensor>
pub fn unsqueeze(&self, dim: usize) -> Result<Tensor>
Unsqueeze: add dimension of size 1 at position
Sourcepub fn deep_clone(&self) -> Self
pub fn deep_clone(&self) -> Self
Deep clone (copies data)
Source§impl Tensor
impl Tensor
Sourcepub fn add_scalar(&self, scalar: f32) -> Tensor
pub fn add_scalar(&self, scalar: f32) -> Tensor
Add scalar
Sourcepub fn sub_scalar(&self, scalar: f32) -> Tensor
pub fn sub_scalar(&self, scalar: f32) -> Tensor
Subtract scalar
Sourcepub fn mul_scalar(&self, scalar: f32) -> Tensor
pub fn mul_scalar(&self, scalar: f32) -> Tensor
Multiply by scalar
Sourcepub fn div_scalar(&self, scalar: f32) -> Tensor
pub fn div_scalar(&self, scalar: f32) -> Tensor
Divide by scalar
Source§impl Tensor
impl Tensor
Sourcepub fn leaky_relu(&self, alpha: f32) -> Tensor
pub fn leaky_relu(&self, alpha: f32) -> Tensor
Leaky ReLU: max(alpha * x, x)
Sourcepub fn log_softmax(&self, dim: i32) -> Tensor
pub fn log_softmax(&self, dim: i32) -> Tensor
Log softmax (numerically stable)
Sourcepub fn hardsigmoid(&self) -> Tensor
pub fn hardsigmoid(&self) -> Tensor
Hard sigmoid: clamp((x + 3) / 6, 0, 1)
Source§impl Tensor
impl Tensor
Trait Implementations§
Source§impl HardwareOps for Tensor
impl HardwareOps for Tensor
Source§fn matmul_hw(&self, other: &Tensor, device: &HardwareDevice) -> Result<Tensor>
fn matmul_hw(&self, other: &Tensor, device: &HardwareDevice) -> Result<Tensor>
Source§fn conv2d_hw(&self, kernel: &Tensor, device: &HardwareDevice) -> Result<Tensor>
fn conv2d_hw(&self, kernel: &Tensor, device: &HardwareDevice) -> Result<Tensor>
Source§fn elementwise_hw(
&self,
op: ElementwiseOp,
device: &HardwareDevice,
) -> Result<Tensor>
fn elementwise_hw( &self, op: ElementwiseOp, device: &HardwareDevice, ) -> Result<Tensor>
Auto Trait Implementations§
impl Freeze for Tensor
impl !RefUnwindSafe for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl !UnwindSafe for Tensor
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