Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor { /* private fields */ }
Expand description

A multi-dimensional tensor with NHWC layout

Implementations§

Source§

impl Tensor

Source

pub fn zeros(shape: &[usize]) -> Self

Create a new tensor with the given shape, initialized to zeros

Source

pub fn ones(shape: &[usize]) -> Self

Create a new tensor with the given shape, initialized to ones

Source

pub fn from_data(data: Vec<f32>, shape: &[usize]) -> CnnResult<Self>

Create a tensor from raw data with the given shape

Source

pub fn full(shape: &[usize], value: f32) -> Self

Create a tensor filled with a constant value

Source

pub fn shape(&self) -> &[usize]

Get the shape of the tensor

Source

pub fn strides(&self) -> &[usize]

Get the strides of the tensor

Source

pub fn ndim(&self) -> usize

Get the number of dimensions

Source

pub fn numel(&self) -> usize

Get the total number of elements

Source

pub fn data(&self) -> &[f32]

Get a reference to the raw data

Source

pub fn data_mut(&mut self) -> &mut [f32]

Get a mutable reference to the raw data

Source

pub fn get_4d(&self, n: usize, h: usize, w: usize, c: usize) -> f32

Get element at index (for 4D NHWC tensor)

Source

pub fn set_4d(&mut self, n: usize, h: usize, w: usize, c: usize, value: f32)

Set element at index (for 4D NHWC tensor)

Source

pub fn batch_size(&self) -> usize

Get batch size (first dimension)

Source

pub fn height(&self) -> usize

Get height (second dimension for NHWC)

Source

pub fn width(&self) -> usize

Get width (third dimension for NHWC)

Source

pub fn channels(&self) -> usize

Get channels (fourth dimension for NHWC)

Source

pub fn reshape(&self, new_shape: &[usize]) -> CnnResult<Self>

Reshape the tensor to a new shape

Source

pub fn view(&self, new_shape: &[usize]) -> CnnResult<Self>

Clone with a new shape (must have same numel)

Source

pub fn slice_batch(&self, start: usize, end: usize) -> CnnResult<Self>

Get a slice of the tensor along the batch dimension

Source

pub fn map<F>(&self, f: F) -> Self
where F: Fn(f32) -> f32,

Apply a function element-wise

Source

pub fn map_inplace<F>(&mut self, f: F)
where F: Fn(f32) -> f32,

Apply a function element-wise in place

Source

pub fn add(&self, other: &Self) -> CnnResult<Self>

Element-wise addition

Source

pub fn mul(&self, other: &Self) -> CnnResult<Self>

Element-wise multiplication

Source

pub fn scale(&self, scalar: f32) -> Self

Scalar multiplication

Source

pub fn sum(&self) -> f32

Sum all elements

Source

pub fn mean(&self) -> f32

Mean of all elements

Source

pub fn max(&self) -> f32

Maximum element

Source

pub fn min(&self) -> f32

Minimum element

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Tensor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V