Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor<'a> { /* private fields */ }
Expand description

An immutable view of a TensorFlow Lite tensor.

Tensor borrows the underlying C tensor pointer and the dynamically loaded library handle for the duration of its lifetime 'a. It provides read-only access to tensor metadata (name, shape, type) and data.

Use Tensor::as_slice to obtain a typed slice over the tensor data.

Implementations§

Source§

impl Tensor<'_>

Source

pub fn tensor_type(&self) -> TensorType

Returns the element data type of this tensor.

If the C API returns a type value not represented by TensorType, this method defaults to TensorType::NoType.

Source

pub fn name(&self) -> &str

Returns the name of this tensor as a string slice.

Returns "<invalid-utf8>" if the C API returns a name that is not valid UTF-8.

Source

pub fn num_dims(&self) -> Result<usize>

Returns the number of dimensions (rank) of this tensor.

§Errors

Returns an error if the tensor does not have its dimensions set (the C API returns -1).

Source

pub fn dim(&self, index: usize) -> Result<usize>

Returns the size of the index-th dimension.

§Errors

Returns an error if index is out of bounds (>= num_dims).

Source

pub fn shape(&self) -> Result<Vec<usize>>

Returns the full shape of this tensor as a Vec<usize>.

§Errors

Returns an error if the tensor dimensions are not set.

Source

pub fn byte_size(&self) -> usize

Returns the total number of bytes required to store this tensor’s data.

Source

pub fn volume(&self) -> Result<usize>

Returns the total number of elements in this tensor (product of all dimensions).

§Errors

Returns an error if the tensor dimensions are not set.

Source

pub fn quantization_params(&self) -> QuantizationParams

Returns the affine quantization parameters for this tensor.

Source

pub fn as_slice<T: Copy>(&self) -> Result<&[T]>

Returns an immutable slice over the tensor data, interpreted as elements of type T.

The slice length equals Tensor::volume. The caller must ensure that T matches the tensor’s actual element type (e.g., f32 for a Float32 tensor, u8 for a UInt8 tensor).

§Errors

Returns an error if:

  • size_of::<T>() * volume exceeds Tensor::byte_size
  • The underlying data pointer is null (tensor not yet allocated)

Trait Implementations§

Source§

impl Debug for Tensor<'_>

Formats the tensor as "name: 1x224x224x3 Float32".

Source§

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

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

impl Display for Tensor<'_>

Displays the tensor as "name: 1x224x224x3 Float32".

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Tensor<'a>

§

impl<'a> !RefUnwindSafe for Tensor<'a>

§

impl<'a> !Send for Tensor<'a>

§

impl<'a> !Sync for Tensor<'a>

§

impl<'a> Unpin for Tensor<'a>

§

impl<'a> UnsafeUnpin for Tensor<'a>

§

impl<'a> !UnwindSafe for Tensor<'a>

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.