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<'_>
impl Tensor<'_>
Sourcepub fn tensor_type(&self) -> TensorType
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.
Sourcepub fn name(&self) -> &str
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.
Sourcepub fn num_dims(&self) -> Result<usize>
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).
Sourcepub fn dim(&self, index: usize) -> Result<usize>
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).
Sourcepub fn shape(&self) -> Result<Vec<usize>>
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.
Sourcepub fn byte_size(&self) -> usize
pub fn byte_size(&self) -> usize
Returns the total number of bytes required to store this tensor’s data.
Sourcepub fn volume(&self) -> Result<usize>
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.
Sourcepub fn quantization_params(&self) -> QuantizationParams
pub fn quantization_params(&self) -> QuantizationParams
Returns the affine quantization parameters for this tensor.
Sourcepub fn as_slice<T: Copy>(&self) -> Result<&[T]>
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>() * volumeexceedsTensor::byte_size- The underlying data pointer is null (tensor not yet allocated)