pub struct Tensor {
pub shape: Vec<usize>,
pub data: Vec<f32>,
}Expand description
An N-dimensional tensor stored in row-major order.
Fields§
§shape: Vec<usize>§data: Vec<f32>Implementations§
Source§impl Tensor
impl Tensor
pub fn zeros(shape: Vec<usize>) -> Self
pub fn ones(shape: Vec<usize>) -> Self
Sourcepub fn rand(shape: Vec<usize>, rng: u64) -> Self
pub fn rand(shape: Vec<usize>, rng: u64) -> Self
Pseudo-random tensor using a simple xorshift seeded from rng.
pub fn from_vec(data: Vec<f32>, shape: Vec<usize>) -> Self
pub fn get(&self, indices: &[usize]) -> f32
pub fn set(&mut self, indices: &[usize], val: f32)
Sourcepub fn slice(&self, ranges: &[Range<usize>]) -> Tensor
pub fn slice(&self, ranges: &[Range<usize>]) -> Tensor
Slice along each axis with the given ranges. Produces a new tensor whose shape matches the range extents.
pub fn add(&self, other: &Tensor) -> Tensor
pub fn sub(&self, other: &Tensor) -> Tensor
pub fn mul(&self, other: &Tensor) -> Tensor
pub fn scale(&self, s: f32) -> Tensor
Sourcepub fn matmul(a: &Tensor, b: &Tensor) -> Tensor
pub fn matmul(a: &Tensor, b: &Tensor) -> Tensor
2-D matrix multiply: (M, K) x (K, N) -> (M, N).
Sourcepub fn transpose(&self) -> Tensor
pub fn transpose(&self) -> Tensor
Transpose the last two dimensions. For 2-D tensors this is the standard matrix transpose.
pub fn sum(&self) -> f32
pub fn mean(&self) -> f32
pub fn max(&self) -> f32
pub fn min(&self) -> f32
Sourcepub fn argmax(&self, axis: usize) -> Tensor
pub fn argmax(&self, axis: usize) -> Tensor
Argmax along a given axis, returning a tensor with that axis removed.
pub fn reshape(&self, new_shape: Vec<usize>) -> Tensor
pub fn flatten(&self) -> Tensor
Sourcepub fn broadcast_to(&self, target: &[usize]) -> Tensor
pub fn broadcast_to(&self, target: &[usize]) -> Tensor
Broadcast this tensor to the target shape, repeating data as needed.
pub fn relu(&self) -> Tensor
pub fn sigmoid(&self) -> Tensor
pub fn tanh_act(&self) -> Tensor
Sourcepub fn gelu(&self) -> Tensor
pub fn gelu(&self) -> Tensor
GELU activation: x * 0.5 * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
Sourcepub fn conv2d(&self, kernel: &Tensor, stride: usize, padding: usize) -> Tensor
pub fn conv2d(&self, kernel: &Tensor, stride: usize, padding: usize) -> Tensor
2-D convolution. Input shape: (C_in, H, W). Kernel shape: (C_out, C_in, kH, kW). Returns shape (C_out, H_out, W_out).
Sourcepub fn max_pool2d(&self, kernel_size: usize, stride: usize) -> Tensor
pub fn max_pool2d(&self, kernel_size: usize, stride: usize) -> Tensor
Max pooling 2-D. Input shape: (C, H, W).
Sourcepub fn avg_pool2d(&self, kernel_size: usize, stride: usize) -> Tensor
pub fn avg_pool2d(&self, kernel_size: usize, stride: usize) -> Tensor
Average pooling 2-D. Input shape: (C, H, W).
Sourcepub fn batch_norm(
&self,
mean: &Tensor,
var: &Tensor,
gamma: &Tensor,
beta: &Tensor,
eps: f32,
) -> Tensor
pub fn batch_norm( &self, mean: &Tensor, var: &Tensor, gamma: &Tensor, beta: &Tensor, eps: f32, ) -> Tensor
Batch normalization: y = gamma * (x - mean) / sqrt(var + eps) + beta.
All parameter tensors must have the same total length as self.
Sourcepub fn layer_norm(&self, axis: usize, eps: f32) -> Tensor
pub fn layer_norm(&self, axis: usize, eps: f32) -> Tensor
Layer normalization along the last n dimensions starting from axis.