Struct easy_ml::tensors::views::TensorRange
source · pub struct TensorRange<T, S, const D: usize> { /* private fields */ }
Expand description
A range over a tensor in D dimensions, hiding the values outside the range from view.
The entire source is still owned by the TensorRange however, so this does not permit creating multiple mutable ranges into a single tensor even if they wouldn’t overlap.
See also: TensorMask
use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorRange};
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_two = TensorView::from(
TensorRange::from(&numbers, [("batch", 0..2)])
.expect("Input is constucted so that our range is valid")
);
let framed = TensorView::from(
TensorRange::from(&numbers, [("rows", [1, 6]), ("columns", [1, 6])])
.expect("Input is constucted so that our range is valid")
);
assert_eq!(one_and_two.shape(), [("batch", 2), ("rows", 8), ("columns", 8)]);
assert_eq!(framed.shape(), [("batch", 4), ("rows", 6), ("columns", 6)]);
println!("{}", framed.select([("batch", 3)]));
// D = 2
// ("rows", 6), ("columns", 6)
// [ 0, 0, 0, 4, 0, 0
// 0, 0, 4, 4, 0, 0
// 0, 4, 0, 4, 0, 0
// 4, 4, 4, 4, 4, 0
// 0, 0, 0, 4, 0, 0
// 0, 0, 0, 4, 0, 0 ]
Implementations§
source§impl<T, S, const D: usize> TensorRange<T, S, D>where
S: TensorRef<T, D>,
impl<T, S, const D: usize> TensorRange<T, S, D>where
S: TensorRef<T, D>,
sourcepub fn from<R, const P: usize>(
source: S,
ranges: [(Dimension, R); P]
) -> Result<TensorRange<T, S, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn from<R, const P: usize>(
source: S,
ranges: [(Dimension, R); P]
) -> Result<TensorRange<T, S, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Constructs a TensorRange from a tensor and set of dimension name/range pairs.
Returns the Err variant if any dimension would have a length of 0 after applying the ranges, if multiple pairs with the same name are provided, or if any dimension names aren’t in the source.
sourcepub fn from_strict<R, const P: usize>(
source: S,
ranges: [(Dimension, R); P]
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn from_strict<R, const P: usize>(
source: S,
ranges: [(Dimension, R); P]
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Constructs a TensorRange from a tensor and set of dimension name/range pairs.
Returns the Err variant if any dimension would have a length of 0 after applying the ranges, if multiple pairs with the same name are provided, or if any dimension names aren’t in the source, or any range extends beyond the length of that dimension in the tensor.
sourcepub fn from_all<R>(
source: S,
ranges: [Option<R>; D]
) -> Result<TensorRange<T, S, D>, InvalidShapeError<D>>where
R: Into<IndexRange>,
pub fn from_all<R>(
source: S,
ranges: [Option<R>; D]
) -> Result<TensorRange<T, S, D>, InvalidShapeError<D>>where
R: Into<IndexRange>,
Constructs a TensorRange from a tensor and a range for each dimension in the tensor (provided in the same order as the tensor’s shape).
Returns the Err variant if any dimension would have a length of 0 after applying the ranges.
sourcepub fn from_all_strict<R>(
source: S,
range: [Option<R>; D]
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, D>>where
R: Into<IndexRange>,
pub fn from_all_strict<R>(
source: S,
range: [Option<R>; D]
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, D>>where
R: Into<IndexRange>,
Constructs a TensorRange from a tensor and a range for each dimension in the tensor (provided in the same order as the tensor’s shape), ensuring the range is within the lengths of the tensor.
Returns the Err variant if any dimension would have a length of 0 after applying the ranges or any range extends beyond the length of that dimension in the tensor.
Trait Implementations§
source§impl<T: Clone, S: Clone, const D: usize> Clone for TensorRange<T, S, D>
impl<T: Clone, S: Clone, const D: usize> Clone for TensorRange<T, S, D>
source§fn clone(&self) -> TensorRange<T, S, D>
fn clone(&self) -> TensorRange<T, S, D>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T, S, const D: usize> TensorMut<T, D> for TensorRange<T, S, D>where
S: TensorMut<T, D>,
impl<T, S, const D: usize> TensorMut<T, D> for TensorRange<T, S, D>where
S: TensorMut<T, D>,
A TensorRange implements TensorMut, with the dimension lengths reduced to the range the the TensorRange was created with.
source§fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T>
fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T>
source§unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T
unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T
source§impl<T, S, const D: usize> TensorRef<T, D> for TensorRange<T, S, D>where
S: TensorRef<T, D>,
impl<T, S, const D: usize> TensorRef<T, D> for TensorRange<T, S, D>where
S: TensorRef<T, D>,
A TensorRange implements TensorRef, with the dimension lengths reduced to the range the the TensorRange was created with.