pub struct TensorMut<'a> { /* private fields */ }Expand description
A mutable view of a TensorFlow Lite tensor.
TensorMut provides all the read-only operations of Tensor plus
mutable data access via TensorMut::as_mut_slice and
TensorMut::copy_from_slice.
The pointer is stored as NonNull because the C API returns
*mut TfLiteTensor for input tensors, which must be non-null after
successful interpreter creation.
Implementations§
Source§impl TensorMut<'_>
impl TensorMut<'_>
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 TensorMut::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>() * volumeexceedsTensorMut::byte_size- The underlying data pointer is null (tensor not yet allocated)
Sourcepub fn as_mut_slice<T: Copy>(&mut self) -> Result<&mut [T]>
pub fn as_mut_slice<T: Copy>(&mut self) -> Result<&mut [T]>
Returns a mutable slice over the tensor data, interpreted as elements
of type T.
The slice length equals TensorMut::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>() * volumeexceedsTensorMut::byte_size- The underlying data pointer is null (tensor not yet allocated)
Sourcepub fn copy_from_slice<T: Copy>(&mut self, data: &[T]) -> Result<()>
pub fn copy_from_slice<T: Copy>(&mut self, data: &[T]) -> Result<()>
Copies the contents of data into this tensor’s buffer.
This is a convenience wrapper around TensorMut::as_mut_slice that
copies elements from the provided slice into the tensor.
§Errors
Returns an error if:
- The tensor cannot be mapped as a mutable slice of
T data.len()does not matchTensorMut::volume