Tensor

Struct Tensor 

Source
pub struct Tensor<T, const N: usize> { /* private fields */ }
Expand description

张量是一种数据的容器,代表在均质的数据上附加了数据类型、形状和数据布局的动态信息。

作为一个容器,Tensor<T, N> 类似于 Option<T>Result<T, _>,可通过一系列方法变换其信息或数据:

  • clone:复制张量的信息,也复制张量的数据;
  • transform:在不改变数据的情况下变换张量的布局;
  • map:替换张量的数据;
  • as_ref:返回引用原始数据的新张量;
  • as_mut:返回可变引用原始数据的新张量;

Implementations§

Source§

impl<const N: usize> Tensor<usize, N>

Source

pub fn new(dt: DigitLayout, shape: [usize; N]) -> Self

创建使用指定数据类型 dt 和形状 shape 的张量,张量的“数据”是连续存储其数据占用的字节数。

传入的 shape 应为张量中的数值的数量。 在底层存储中,可能将多个数值捆绑为一个数据组。 获取张量的形状时,将返回作为 N 维数组的形状,其连续维度除去了组的规模。

例如,对于将 32 个数字绑定为一组的数据类型,shape[7, 1024] 时,产生的张量的形状是 [7, 32]

// 定义一个数据类型,以 32 个 8 位无符号数为一组。
digit_layout::layout!(GROUP u(8); 32);

let tensor = Tensor::new(GROUP, [7, 1024]);
assert_eq!(tensor.dt(), GROUP);
assert_eq!(tensor.shape(), [7, 32]);
assert_eq!(tensor.take(), 7 * 32 * 32);
Source

pub fn from_dim_slice(dt: DigitLayout, shape: impl AsRef<[usize]>) -> Self

Source§

impl<T, const N: usize> Tensor<T, N>

Source

pub const fn dt(&self) -> DigitLayout

Source

pub const fn layout(&self) -> &ArrayLayout<N>

Source

pub fn shape(&self) -> &[usize]

Source

pub fn strides(&self) -> &[isize]

Source

pub fn offset(&self) -> isize

Source

pub const fn get(&self) -> &T

Source

pub fn get_mut(&mut self) -> &mut T

Source

pub fn take(self) -> T

Source

pub const fn from_raw_parts( dt: DigitLayout, layout: ArrayLayout<N>, item: T, ) -> Self

Source

pub fn into_raw_parts(self) -> (DigitLayout, ArrayLayout<N>, T)

Source

pub fn use_info(&self) -> Tensor<usize, N>

Source

pub fn is_contiguous(&self) -> bool

Source§

impl<T, const N: usize> Tensor<T, N>

Source

pub fn as_ref(&self) -> Tensor<&T, N>

Source

pub fn as_mut(&mut self) -> Tensor<&mut T, N>

Source

pub fn transform(self, f: impl FnOnce(ArrayLayout<N>) -> ArrayLayout<N>) -> Self

Source

pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Tensor<U, N>

Source

pub fn replace<U>(self, u: U) -> (T, Tensor<U, N>)

Source§

impl<T: Deref, const N: usize> Tensor<T, N>

Source

pub fn as_deref(&self) -> Tensor<&<T as Deref>::Target, N>

Source§

impl<T: DerefMut, const N: usize> Tensor<T, N>

Source

pub fn as_deref_mut(&mut self) -> Tensor<&mut <T as Deref>::Target, N>

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Clone for Tensor<T, N>

Source§

fn clone(&self) -> Tensor<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Tensor<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Tensor<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Tensor<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Tensor<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for Tensor<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Tensor<T, N>
where T: UnwindSafe,

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, dest: *mut u8)

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

Source§

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