Struct TensorMask

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

A mask over a tensor in D dimensions, hiding the values inside the range from view.

The entire source is still owned by the TensorMask however, so this does not permit creating multiple mutable masks into a single tensor even if they wouldn’t overlap.

See also: TensorRange

use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorMask};
let numbers = Tensor::from([("batch", 4), ("rows", 8), ("columns", 8)], vec![
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 1, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 1, 1, 1, 1, 0, 0,
    0, 0, 1, 1, 1, 1, 0, 0,

    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 2, 2, 0, 0, 0,
    0, 0, 2, 0, 0, 2, 0, 0,
    0, 0, 0, 0, 0, 2, 0, 0,
    0, 0, 0, 0, 2, 0, 0, 0,
    0, 0, 0, 2, 0, 0, 0, 0,
    0, 0, 2, 0, 0, 0, 0, 0,
    0, 0, 2, 2, 2, 2, 0, 0,

    0, 0, 0, 3, 3, 0, 0, 0,
    0, 0, 3, 0, 0, 3, 0, 0,
    0, 0, 0, 0, 0, 3, 0, 0,
    0, 0, 0, 0, 3, 0, 0, 0,
    0, 0, 0, 0, 3, 0, 0, 0,
    0, 0, 0, 0, 0, 3, 0, 0,
    0, 0, 3, 0, 0, 3, 0, 0,
    0, 0, 0, 3, 3, 0, 0, 0,

    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0,
    0, 0, 0, 4, 4, 0, 0, 0,
    0, 0, 4, 0, 4, 0, 0, 0,
    0, 4, 4, 4, 4, 4, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0
]);
let one_and_four = TensorView::from(
    TensorMask::from(&numbers, [("batch", 1..3)])
        .expect("Input is constucted so that our mask is valid")
);
let corners = TensorView::from(
    TensorMask::from(&numbers, [("rows", [3, 2]), ("columns", [3, 2])])
        .expect("Input is constucted so that our mask is valid")
);
assert_eq!(one_and_four.shape(), [("batch", 2), ("rows", 8), ("columns", 8)]);
assert_eq!(corners.shape(), [("batch", 4), ("rows", 6), ("columns", 6)]);
println!("{}", corners.select([("batch", 2)]));
// D = 2
// ("rows", 6), ("columns", 6)
// [ 0, 0, 0, 0, 0, 0
//   0, 0, 3, 3, 0, 0
//   0, 0, 0, 3, 0, 0
//   0, 0, 0, 3, 0, 0
//   0, 0, 3, 3, 0, 0
//   0, 0, 0, 0, 0, 0 ]

Implementations§

Source§

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

Source

pub fn from<R, const P: usize>( source: S, masks: [(Dimension, R); P], ) -> Result<TensorMask<T, S, D>, IndexRangeValidationError<D, P>>
where R: Into<IndexRange>,

Constructs a TensorMask from a tensor and set of dimension name/mask pairs.

Returns the Err variant if any masked dimension would have a length of 0, if multiple pairs with the same name are provided, or if any dimension names aren’t in the source.

Source

pub fn from_strict<R, const P: usize>( source: S, masks: [(Dimension, R); P], ) -> Result<TensorMask<T, S, D>, StrictIndexRangeValidationError<D, P>>
where R: Into<IndexRange>,

Constructs a TensorMask from a tensor and set of dimension name/range pairs.

Returns the Err variant if any masked dimension would have a length of 0, if multiple pairs with the same name are provided, or if any dimension names aren’t in the source, or any mask extends beyond the length of that dimension in the tensor.

Source

pub fn from_all<R>( source: S, mask: [Option<R>; D], ) -> Result<TensorMask<T, S, D>, InvalidShapeError<D>>
where R: Into<IndexRange>,

Constructs a TensorMask from a tensor and a mask for each dimension in the tensor (provided in the same order as the tensor’s shape).

Returns the Err variant if any masked dimension would have a length of 0.

Source

pub fn from_all_strict<R>( source: S, masks: [Option<R>; D], ) -> Result<TensorMask<T, S, D>, StrictIndexRangeValidationError<D, D>>
where R: Into<IndexRange>,

Constructs a TensorMask from a tensor and a mask for each dimension in the tensor (provided in the same order as the tensor’s shape), ensuring the mask is within the lengths of the tensor.

Returns the Err variant if any masked dimension would have a length of 0 or any mask extends beyond the length of that dimension in the tensor.

Source

pub fn source(self) -> S

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

Source

pub fn source_ref(&self) -> &S

Gives a reference to the TensorMask’s source (in which the data is not masked).

Trait Implementations§

Source§

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

Source§

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

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> Debug for TensorMask<T, S, D>

Source§

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

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

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

A TensorMask implements TensorMut, with the dimension lengths reduced by the mask the the TensorMask was created with.

Source§

fn get_reference_mut(&mut self, indexes: [usize; D]) -> 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; D]) -> &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> TensorRef<T, D> for TensorMask<T, S, D>
where S: TensorRef<T, D>,

A TensorMask implements TensorRef, with the dimension lengths reduced by the mask the the TensorMask was created with.

Source§

fn get_reference(&self, indexes: [usize; D]) -> 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); D]

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; D]) -> &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<D>

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> Freeze for TensorMask<T, S, D>
where S: Freeze,

§

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

§

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

§

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

§

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

§

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