Struct burn_tensor::TensorData

source ·
pub struct TensorData {
    pub bytes: Vec<u8>,
    pub shape: Vec<usize>,
    pub dtype: DType,
}
Expand description

Data structure for tensors.

Fields§

§bytes: Vec<u8>

The values of the tensor (as bytes).

§shape: Vec<usize>

The shape of the tensor.

§dtype: DType

The data type of the tensor.

Implementations§

source§

impl TensorData

source

pub fn new<E: Element, S: Into<Vec<usize>>>(value: Vec<E>, shape: S) -> Self

Creates a new tensor data structure.

source

pub fn quantized<E: Element, S: Into<Vec<usize>>>( value: Vec<E>, shape: S, strategy: QuantizationStrategy, ) -> Self

Creates a new quantized tensor data structure.

source

pub fn as_slice<E: Element>(&self) -> Result<&[E], DataError>

Returns the immutable slice view of the tensor data.

source

pub fn as_mut_slice<E: Element>(&mut self) -> Result<&mut [E], DataError>

Returns the mutable slice view of the tensor data.

§Panics

If the target element type is different from the stored element type.

source

pub fn to_vec<E: Element>(&self) -> Result<Vec<E>, DataError>

Returns the tensor data as a vector of scalar values.

source

pub fn iter<E: Element>(&self) -> Box<dyn Iterator<Item = E> + '_>

Returns an iterator over the values of the tensor data.

source

pub fn num_elements(&self) -> usize

Returns the total number of elements of the tensor data.

source

pub fn random<E: Element, R: RngCore, S: Into<Vec<usize>>>( shape: S, distribution: Distribution, rng: &mut R, ) -> Self

Populates the data with random values.

source

pub fn zeros<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData

Populates the data with zeros.

source

pub fn ones<E: Element, S: Into<Vec<usize>>>(shape: S) -> TensorData

Populates the data with ones.

source

pub fn full<E: Element, S: Into<Vec<usize>>>( shape: S, fill_value: E, ) -> TensorData

Populates the data with the given value

source

pub fn convert<E: Element>(self) -> Self

Converts the data to a different element type.

source

pub fn as_bytes(&self) -> &[u8]

Returns the data as a slice of bytes.

source

pub fn with_quantization(self, quantization: QuantizationStrategy) -> Self

Applies the data quantization strategy.

§Panics

Panics if the data type is not supported for quantization.

source

pub fn assert_approx_eq(&self, other: &Self, precision: usize)

Asserts the data is approximately equal to another data.

§Arguments
  • other - The other data.
  • precision - The precision of the comparison.
§Panics

Panics if the data is not approximately equal.

source

pub fn assert_eq(&self, other: &Self, strict: bool)

Asserts the data is equal to another data.

§Arguments
  • other - The other data.
  • strict - If true, the data types must the be same. Otherwise, the comparison is done in the current data type.
§Panics

Panics if the data is not equal.

source

pub fn assert_approx_eq_diff(&self, other: &Self, tolerance: f64)

Asserts the data is approximately equal to another data.

§Arguments
  • other - The other data.
  • tolerance - The tolerance of the comparison.
§Panics

Panics if the data is not approximately equal.

source

pub fn assert_within_range<E: Element>(&self, range: Range<E>)

Asserts each value is within a given range.

§Arguments
  • range - The range.
§Panics

If any value is not within the half-open range bounded inclusively below and exclusively above (start..end).

Trait Implementations§

source§

impl Clone for TensorData

source§

fn clone(&self) -> TensorData

Returns a copy 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 TensorData

source§

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

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

impl<'de> Deserialize<'de> for TensorData

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for TensorData

source§

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

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

impl<E: Element> From<&[E]> for TensorData

source§

fn from(elems: &[E]) -> Self

Converts to this type from the input type.
source§

impl From<&[usize]> for TensorData

source§

fn from(elems: &[usize]) -> Self

Converts to this type from the input type.
source§

impl<Elem: Element, const A: usize, const B: usize, const C: usize, const D: usize, const E: usize> From<[[[[[Elem; E]; D]; C]; B]; A]> for TensorData

source§

fn from(elems: [[[[[Elem; E]; D]; C]; B]; A]) -> Self

Converts to this type from the input type.
source§

impl<E: Element, const A: usize, const B: usize, const C: usize, const D: usize> From<[[[[E; D]; C]; B]; A]> for TensorData

source§

fn from(elems: [[[[E; D]; C]; B]; A]) -> Self

Converts to this type from the input type.
source§

impl<E: Element, const A: usize, const B: usize, const C: usize> From<[[[E; C]; B]; A]> for TensorData

source§

fn from(elems: [[[E; C]; B]; A]) -> Self

Converts to this type from the input type.
source§

impl<E: Element, const A: usize, const B: usize> From<[[E; B]; A]> for TensorData

source§

fn from(elems: [[E; B]; A]) -> Self

Converts to this type from the input type.
source§

impl<E: Element, const A: usize> From<[E; A]> for TensorData

source§

fn from(elems: [E; A]) -> Self

Converts to this type from the input type.
source§

impl<const A: usize> From<[usize; A]> for TensorData

source§

fn from(elems: [usize; A]) -> Self

Converts to this type from the input type.
source§

impl PartialEq for TensorData

source§

fn eq(&self, other: &TensorData) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for TensorData

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for TensorData

source§

impl StructuralPartialEq for TensorData

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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

source§

default 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>,

§

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>,

§

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

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,