pub struct TensorStack<T, S, const D: usize> { /* private fields */ }
Expand description
Combines two or more tensors with the same shape along a new dimension to create a Tensor with one additional dimension which stacks the sources together along that dimension.
Note: due to limitations in Rust’s const generics support, TensorStack only implements
TensorRef for D from 1
to 6
(from sources of 0
to 5
dimensions respectively), and
only supports tuple combinations for 2
to 4
. If you need to stack more than four tensors
together, you can stack any number with the [S; N]
implementation, though note this requires
that all the tensors are the same type so you may need to box and erase the types to
Box<dyn TensorRef<T, D>>
.
use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorStack, TensorRef};
let vector1 = Tensor::from([("data", 5)], vec![0, 1, 2, 3, 4]);
let vector2 = Tensor::from([("data", 5)], vec![2, 4, 8, 16, 32]);
// Because there are 4 variants of `TensorStack::from` you may need to use the turbofish
// to tell the Rust compiler which variant you're using, but the actual type of `S` can be
// left unspecified by using an underscore.
let matrix = TensorStack::<i32, [_; 2], 1>::from([&vector1, &vector2], (0, "sample"));
let equal_matrix = Tensor::from([("sample", 2), ("data", 5)], vec![
0, 1, 2, 3, 4,
2, 4, 8, 16, 32
]);
assert_eq!(equal_matrix, TensorView::from(matrix));
let also_matrix = TensorStack::<i32, (_, _), 1>::from((vector1, vector2), (0, "sample"));
assert_eq!(equal_matrix, TensorView::from(&also_matrix));
// To stack `equal_matrix` and `also_matrix` using the `[S; N]` implementation we have to first
// make them the same type, which we can do by boxing and erasing.
let matrix_erased: Box<dyn TensorRef<i32, 2>> = Box::new(also_matrix);
let equal_matrix_erased: Box<dyn TensorRef<i32, 2>> = Box::new(equal_matrix);
let tensor = TensorStack::<i32, [_; 2], 2>::from(
[matrix_erased, equal_matrix_erased], (0, "experiment")
);
assert!(
TensorView::from(tensor).eq(
&Tensor::from([("experiment", 2), ("sample", 2), ("data", 5)], vec![
0, 1, 2, 3, 4,
2, 4, 8, 16, 32,
0, 1, 2, 3, 4,
2, 4, 8, 16, 32
])
),
);
Implementations§
Source§impl<T, S, const D: usize, const N: usize> TensorStack<T, [S; N], D>where
S: TensorRef<T, D>,
impl<T, S, const D: usize, const N: usize> TensorStack<T, [S; N], D>where
S: TensorRef<T, D>,
Sourcepub fn from(sources: [S; N], along: (usize, Dimension)) -> Self
pub fn from(sources: [S; N], along: (usize, Dimension)) -> Self
Creates a TensorStack from an array of sources of the same type and a tuple of which
dimension and name to stack the sources along in the range of 0 <= d
<= D. The sources
must all have an identical shape, and the dimension name to add must not be in the sources’
shape already.
§Panics
If N == 0, the shapes of the sources are not identical, the dimension for stacking is out of bounds, or the name is already in the sources’ shape.
While N == 1 arguments may be valid TensorExpansion is a more general way to add dimensions with no additional data.
Sourcepub fn sources(self) -> [S; N]
pub fn sources(self) -> [S; N]
Consumes the TensorStack, yielding the sources it was created from in the same order.
Sourcepub fn sources_ref(&self) -> &[S; N]
pub fn sources_ref(&self) -> &[S; N]
Gives a reference to all the TensorStack’s sources it was created from in the same order
Source§impl<T, S1, S2, const D: usize> TensorStack<T, (S1, S2), D>
impl<T, S1, S2, const D: usize> TensorStack<T, (S1, S2), D>
Sourcepub fn from(sources: (S1, S2), along: (usize, Dimension)) -> Self
pub fn from(sources: (S1, S2), along: (usize, Dimension)) -> Self
Creates a TensorStack from two sources and a tuple of which dimension and name to stack
the sources along in the range of 0 <= d
<= D. The sources must all have an identical
shape, and the dimension name to add must not be in the sources’ shape already.
§Panics
If the shapes of the sources are not identical, the dimension for stacking is out of bounds, or the name is already in the sources’ shape.
Sourcepub fn sources(self) -> (S1, S2)
pub fn sources(self) -> (S1, S2)
Consumes the TensorStack, yielding the sources it was created from in the same order.
Sourcepub fn sources_ref(&self) -> &(S1, S2)
pub fn sources_ref(&self) -> &(S1, S2)
Gives a reference to all the TensorStack’s sources it was created from in the same order
Source§impl<T, S1, S2, S3, const D: usize> TensorStack<T, (S1, S2, S3), D>
impl<T, S1, S2, S3, const D: usize> TensorStack<T, (S1, S2, S3), D>
Sourcepub fn from(sources: (S1, S2, S3), along: (usize, Dimension)) -> Self
pub fn from(sources: (S1, S2, S3), along: (usize, Dimension)) -> Self
Creates a TensorStack from three sources and a tuple of which dimension and name to stack
the sources along in the range of 0 <= d
<= D. The sources must all have an identical
shape, and the dimension name to add must not be in the sources’ shape already.
§Panics
If the shapes of the sources are not identical, the dimension for stacking is out of bounds, or the name is already in the sources’ shape.
Sourcepub fn sources(self) -> (S1, S2, S3)
pub fn sources(self) -> (S1, S2, S3)
Consumes the TensorStack, yielding the sources it was created from in the same order.
Sourcepub fn sources_ref(&self) -> &(S1, S2, S3)
pub fn sources_ref(&self) -> &(S1, S2, S3)
Gives a reference to all the TensorStack’s sources it was created from in the same order
Source§impl<T, S1, S2, S3, S4, const D: usize> TensorStack<T, (S1, S2, S3, S4), D>
impl<T, S1, S2, S3, S4, const D: usize> TensorStack<T, (S1, S2, S3, S4), D>
Sourcepub fn from(sources: (S1, S2, S3, S4), along: (usize, Dimension)) -> Self
pub fn from(sources: (S1, S2, S3, S4), along: (usize, Dimension)) -> Self
Creates a TensorStack from four sources and a tuple of which dimension and name to stack
the sources along in the range of 0 <= d
<= D. The sources must all have an identical
shape, and the dimension name to add must not be in the sources’ shape already.
§Panics
If the shapes of the sources are not identical, the dimension for stacking is out of bounds, or the name is already in the sources’ shape.
Sourcepub fn sources(self) -> (S1, S2, S3, S4)
pub fn sources(self) -> (S1, S2, S3, S4)
Consumes the TensorStack, yielding the sources it was created from in the same order.
Sourcepub fn sources_ref(&self) -> &(S1, S2, S3, S4)
pub fn sources_ref(&self) -> &(S1, S2, S3, S4)
Gives a reference to all the TensorStack’s sources it was created from in the same order
Trait Implementations§
Source§impl<T: Clone, S: Clone, const D: usize> Clone for TensorStack<T, S, D>
impl<T: Clone, S: Clone, const D: usize> Clone for TensorStack<T, S, D>
Source§fn clone(&self) -> TensorStack<T, S, D>
fn clone(&self) -> TensorStack<T, S, D>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more