pub struct Tensor { /* private fields */ }Expand description
A typed, shaped buffer of data.
Tensors hold raw bytes with a dtype and shape. Data is reference-counted so cloning a tensor is cheap (no copy). The actual bytes are only copied when mutated (copy-on-write).
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn f32(data: &[f32], shape: Vec<usize>) -> Self
pub fn f32(data: &[f32], shape: Vec<usize>) -> Self
Create a float32 tensor from a slice of f32 values.
Sourcepub fn f64(data: &[f64], shape: Vec<usize>) -> Self
pub fn f64(data: &[f64], shape: Vec<usize>) -> Self
Create a float64 tensor from a slice of f64 values.
Sourcepub fn from_bytes(
data: Vec<u8>,
dtype: DType,
shape: Vec<usize>,
) -> Result<Self>
pub fn from_bytes( data: Vec<u8>, dtype: DType, shape: Vec<usize>, ) -> Result<Self>
Create a tensor from raw bytes, dtype, and shape.
§Errors
Returns an error if the provided byte length does not match the product of shape elements
multiplied by the element size for the given dtype.
Sourcepub fn num_elements(&self) -> Option<usize>
pub fn num_elements(&self) -> Option<usize>
Total number of elements.
Returns None if the element count overflows usize.
Sourcepub fn try_as_f32(&self) -> Result<&[f32]>
pub fn try_as_f32(&self) -> Result<&[f32]>
Sourcepub fn try_as_f64(&self) -> Result<&[f64]>
pub fn try_as_f64(&self) -> Result<&[f64]>
Sourcepub fn try_as_i32(&self) -> Result<&[i32]>
pub fn try_as_i32(&self) -> Result<&[i32]>
Sourcepub fn try_as_i64(&self) -> Result<&[i64]>
pub fn try_as_i64(&self) -> Result<&[i64]>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Tensor
impl RefUnwindSafe for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl UnsafeUnpin for Tensor
impl UnwindSafe for Tensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more