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>(data: &[T], shape: &[usize]) -> Result<Tensor, GhostError>where
T: TensorElement,
pub fn from_slice<T>(data: &[T], shape: &[usize]) -> Result<Tensor, GhostError>where
T: TensorElement,
Create a new tensor from a flat slice and shape
Sourcepub fn full<T>(shape: &[usize], value: T) -> Tensorwhere
T: TensorElement,
pub fn full<T>(shape: &[usize], value: T) -> Tensorwhere
T: TensorElement,
Create a tensor filled with a constant value
Sourcepub fn rand(shape: &[usize]) -> Tensor
pub fn rand(shape: &[usize]) -> Tensor
Create a tensor with random values from uniform distribution [0, 1)
Sourcepub fn randn(shape: &[usize]) -> Tensor
pub fn randn(shape: &[usize]) -> Tensor
Create a tensor with random values from standard normal distribution
Sourcepub fn arange(start: f32, end: f32, step: f32) -> Tensor
pub fn arange(start: f32, end: f32, step: f32) -> Tensor
Create a 1D tensor with evenly spaced values
Sourcepub fn linspace(start: f32, end: f32, n: usize) -> Tensor
pub fn linspace(start: f32, end: f32, n: usize) -> Tensor
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, GhostError>
pub fn reshape(&self, new_shape: &[usize]) -> Result<Tensor, GhostError>
Reshape tensor to new shape (must have same numel)
Sourcepub fn flatten(&self) -> Result<Tensor, GhostError>
pub fn flatten(&self) -> Result<Tensor, GhostError>
Flatten tensor to 1D
Sourcepub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Tensor, GhostError>
pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Tensor, GhostError>
Transpose dimensions
Sourcepub fn t(&self) -> Result<Tensor, GhostError>
pub fn t(&self) -> Result<Tensor, GhostError>
Transpose for 2D tensors (matrix transpose)
Sourcepub fn unsqueeze(&self, dim: usize) -> Result<Tensor, GhostError>
pub fn unsqueeze(&self, dim: usize) -> Result<Tensor, GhostError>
Unsqueeze: add dimension of size 1 at position
Sourcepub fn deep_clone(&self) -> Tensor
pub fn deep_clone(&self) -> Tensor
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 sum_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
pub fn sum_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
Sum along dimension
Sourcepub fn mean_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
pub fn mean_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
Mean along dimension
Sourcepub fn max_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
pub fn max_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
Maximum along dimension
Sourcepub fn min_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
pub fn min_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor, GhostError>
Minimum along dimension
Sourcepub fn argmax_dim(
&self,
dim: usize,
keepdim: bool,
) -> Result<Tensor, GhostError>
pub fn argmax_dim( &self, dim: usize, keepdim: bool, ) -> Result<Tensor, GhostError>
Argmax along dimension
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
Sourcepub fn matmul(&self, other: &Tensor) -> Result<Tensor, GhostError>
pub fn matmul(&self, other: &Tensor) -> Result<Tensor, GhostError>
Matrix multiplication Supports:
- 2D x 2D: standard matmul
- Batched: broadcast batch dimensions
Sourcepub fn bmm(&self, other: &Tensor) -> Result<Tensor, GhostError>
pub fn bmm(&self, other: &Tensor) -> Result<Tensor, GhostError>
Batch matrix-matrix multiplication (bmm)
Sourcepub fn trace(&self) -> Result<Tensor, GhostError>
pub fn trace(&self) -> Result<Tensor, GhostError>
Compute trace of a matrix
Sourcepub fn diag(&self) -> Result<Tensor, GhostError>
pub fn diag(&self) -> Result<Tensor, GhostError>
Compute diagonal of a matrix
Trait Implementations§
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