TensorReshape

Struct TensorReshape 

Source
pub struct TensorReshape<T, S, const D: usize, const D2: usize> { /* private fields */ }
Expand description

A new shape to override indexing an existing tensor. The dimensionality and individual dimension lengths can be changed, but the total number of elements in the new shape must match the existing tensor’s shape.

Elements are still in the same order (in memory) as the source tensor, none of the data is moved around - though iteration might be in a different order with the new shape.

If you just need to rename dimensions without changing them, see TensorRename

This types’ generics can be read as a TensorReshape is generic over some element of type T from an existing source of type S of dimensionality D, and this tensor has dimensionality D2, which might be different to D, but will have the same total number of elements.

use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorReshape, TensorView};
let tensor = Tensor::from([("a", 2), ("b", 2)], (0..4).collect());
let flat = TensorView::from(TensorReshape::from(tensor, [("i", 4)])); // or use tensor.reshape_view_owned
assert_eq!(flat, Tensor::from([("i", 4)], (0..4).collect()));

Implementations§

Source§

impl<T, S, const D: usize, const D2: usize> TensorReshape<T, S, D, D2>
where S: TensorRef<T, D>,

Source

pub fn from( source: S, shape: [(Dimension, usize); D2], ) -> TensorReshape<T, S, D, D2>

Creates a TensorReshape from a source and a new shape to override the view_shape with. The new shape must correspond to the same number of total elements, but it need not match on dimensionality or individual dimension lengths.

If you just need to rename dimensions without changing them, see TensorRename If you don’t need to change the dimensionality, see from

§Panics
  • If the new shape has a different number of elements to the existing shape in the source
  • If the new shape has duplicate dimension names
Source

pub fn source(self) -> S

Consumes the TensorReshape, yielding the source it was created from.

Source

pub fn source_ref(&self) -> &S

Gives a reference to the TensorReshape’s source (in which the data has its original shape).

Source§

impl<T, S, const D: usize> TensorReshape<T, S, D, D>
where S: TensorRef<T, D>,

Source

pub fn from_existing_dimensions( source: S, lengths: [usize; D], ) -> TensorReshape<T, S, D, D>

Creates a TensorReshape from a source and new dimension lengths with the same dimensionality as the source to override the view_shape with. The new shape must correspond to the same number of total elements, but it need not match on individual dimension lengths.

If you just need to rename dimensions without changing them, see TensorRename If you need to change the dimensionality, see from

§Panics
  • If the new shape has a different number of elements to the existing shape in the source

Trait Implementations§

Source§

impl<T: Clone, S: Clone, const D: usize, const D2: usize> Clone for TensorReshape<T, S, D, D2>

Source§

fn clone(&self) -> TensorReshape<T, S, D, D2>

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
Source§

impl<T: Debug, S: Debug, const D: usize, const D2: usize> Debug for TensorReshape<T, S, D, D2>

Source§

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

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

impl<T, S, const D: usize, const D2: usize> TensorMut<T, D2> for TensorReshape<T, S, D, D2>
where S: TensorMut<T, D>,

A TensorReshape implements TensorMut, with the data in the same order as the original source.

Source§

fn get_reference_mut(&mut self, indexes: [usize; D2]) -> Option<&mut T>

Gets a mutable reference to the value at the index, if the index is in range. Otherwise returns None.
Source§

unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D2]) -> &mut T

Gets a mutable reference to the value at the index without doing any bounds checking. For a safe alternative see get_reference_mut. Read more
Source§

impl<T, S, const D: usize, const D2: usize> TensorRef<T, D2> for TensorReshape<T, S, D, D2>
where S: TensorRef<T, D>,

A TensorReshape implements TensorRef, with the data in the same order as the original source.

Source§

fn get_reference(&self, indexes: [usize; D2]) -> Option<&T>

Gets a reference to the value at the index if the index is in range. Otherwise returns None.
Source§

fn view_shape(&self) -> [(Dimension, usize); D2]

The shape this tensor has. See dimensions for an overview. The product of the lengths in the pairs define how many elements are in the tensor (or the portion of it that is visible).
Source§

unsafe fn get_reference_unchecked(&self, indexes: [usize; D2]) -> &T

Gets a reference to the value at the index without doing any bounds checking. For a safe alternative see get_reference. Read more
Source§

fn data_layout(&self) -> DataLayout<D2>

The way the data in this tensor is laid out in memory. In particular, Linear has several requirements on what is returned that must be upheld by implementations of this trait. Read more

Auto Trait Implementations§

§

impl<T, S, const D: usize, const D2: usize> Freeze for TensorReshape<T, S, D, D2>
where S: Freeze,

§

impl<T, S, const D: usize, const D2: usize> RefUnwindSafe for TensorReshape<T, S, D, D2>

§

impl<T, S, const D: usize, const D2: usize> Send for TensorReshape<T, S, D, D2>
where S: Send, T: Send,

§

impl<T, S, const D: usize, const D2: usize> Sync for TensorReshape<T, S, D, D2>
where S: Sync, T: Sync,

§

impl<T, S, const D: usize, const D2: usize> Unpin for TensorReshape<T, S, D, D2>
where S: Unpin, T: Unpin,

§

impl<T, S, const D: usize, const D2: usize> UnwindSafe for TensorReshape<T, S, D, D2>
where S: UnwindSafe, 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.