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.

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

§

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 Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · 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 Twhere
    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, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.