Struct tensor_rs::tensor_impl::gen_tensor::GenTensor
source · [−]pub struct GenTensor<T> { /* private fields */ }
Expand description
Naive tensor implementation, single thread
Implementations
Convert 1 dim index to multi-dim index.
Create a tensor filled with the same value d
let m1 = GenTensor::<f64>::fill(1., &vec![3,5,2]);
assign a row.
Right dimension changes fastest. Right dimension has the stride 1.
let m1 = GenTensor::<f64>::new_raw(&vec![0.; 3*5*2], &vec![3,5,2]);
assert_eq!(m1.stride(), vec![10,2,1]);
Return value at the index of the tensor.
let m1 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,5.,6.], &vec![2,3]);
assert_eq!(m1.get(&vec![1,1]), 5.);
dump the single value in the tensor if it is the single value in the tensor.
get NCHW elements, always return the size of left most dimension.
get NCHW elements, always return the size of second left most dimension.
get NCDHW elements, will require the self.dim has 5 dimensions.
get NCDHW elements, will require the self.dim has 5 dimensions or 4 dimensions.
get NCDHW elements, will require the self.dim has 5 dimensions or 4 dimensions.
Returns the total number of elements in the input tensor
Return portion of the image. Every range of each dim with inclusive start and exclusive end.
pub fn _iter_patch<F>(
&self,
dim: Option<&[usize]>,
keep_dim: bool,
closure: F
) -> GenTensor<T> where
F: Fn(&[T]) -> T,
pub fn _dim_statistic<F>(
&self,
dim: usize,
keepdim: bool,
closure: F
) -> GenTensor<T> where
F: Fn(usize, usize, usize, usize, usize) -> T,
pub fn _right_broadcast<F>(&self, o: &GenTensor<T>, closure: F) -> GenTensor<T> where
F: Fn(&T, &T) -> T,
element-wise add with right-hand broadcast.
let m1 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,], &vec![2,2]);
let m2 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,], &vec![2,2]);
let m3 = m1.add(&m2);
assert_eq!(m3.get(&vec![0,0]), 2.);
assert_eq!(m3.get(&vec![1,1]), 8.);
matrix multiplication
let m1 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,5.,6.], &vec![3,2]);
let m2 = GenTensor::<f64>::new_raw(&vec![2.,3.,4.,5.,6.,7.], &vec![2,3]);
let mut result = m1.mm(&m2);
assert!(result == GenTensor::<f64>::new_raw(&vec![12.,15.,18.,26.,33.,40.,40.,51.,62.,], &vec![3,3]), "");
matrix multiplication of two tensor This is also for dot/inner product.
outer product of right-most dimensions.
Computes element-wise equality use eq_t instead, as eq is reserved for == overloading.
let m1 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,5.,6.], &vec![3,2]);
let m2 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,5.,6.], &vec![3,2]);
assert_eq!(m1.eq_t(&m2).get(&vec![0,0]), 1.);
assert_eq!(m1.eq_t(&m2).get(&vec![2,1]), 1.);
true if two tensors have the same size and elements, false otherwise.
let m1 = GenTensor::<f64>::fill(1., &vec![3,5,2]);
let m2 = GenTensor::<f64>::fill(1., &vec![3,5,2]);
assert_eq!(m1.equal(&m2), true)
Trait Implementations
fn conv2d(
&self,
filter: &GenTensor<T>,
stride: (usize, usize),
padding: (usize, usize),
dilation: (usize, usize),
padding_mode: PaddingMode
) -> Self
fn conv2d_grad(
&self,
filter: &GenTensor<T>,
stride: (usize, usize),
padding: (usize, usize),
dilation: (usize, usize),
padding_mode: PaddingMode,
output_grad: &GenTensor<T>
) -> (Self, Self)
Better log(1 + exp(x)) see https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf
type TensorType = GenTensor<T>
type ElementType = T
Concatenates the given sequence of seq tensors in the given dimension.
Splits a tensor into a specific number of chunks.
Splits the tensor into chunks. Each chunk is a view of the original tensor.
Concatenates sequence of tensors along a new dimension.
All tensors need to be of the same size.
let m1 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,5.,6.], &vec![3,2]);
let m2 = GenTensor::<f64>::new_raw(&vec![2.,3.,4.,5.,6.,7.], &vec![3,2]);
let result = m1.stack(&vec![m2], 1);
let raw = result.get_raw();
for i in raw {
println!("{}", i);
}
assert_eq!(*result.size(), vec![3,2,2]);
Returns a new tensor with the elements of input at the given indices. The input tensor is treated as if it were viewed as a 1-D tensor. The result takes the same shape as the indices.
Permute the dimensions of this tensor.
let mut m1 = GenTensor::<f64>::fill(1., &vec![2, 3, 5]);
m1.permute(&vec![2, 0, 1]);
Pick elements on the given dimension by the index given in index, and gather them in the output. A restriction os that self size and index size should be the same. Read more
Select on dim and collect those subtensor by index.
Inverse of index_select, remove those subtensor by index along dim.
Self is the bool condition, at each position of self, select from x if self at the position is true, Otherwise , use value from y. The restriction is that, self, x, and y all have the same size. Read more
type TensorType = GenTensor<T>
type ElementType = T
let m1 = GenTensor::<f64>::fill(1., &vec![3,5,2]);
let m2 = GenTensor::<f64>::fill(1., &vec![3,5,2]);
assert_eq!(m1==m2, true)
impl<T> Random for GenTensor<T> where
T: Float + SampleUniform,
StandardNormal: Distribution<T>,
impl<T> Random for GenTensor<T> where
T: Float + SampleUniform,
StandardNormal: Distribution<T>,
type TensorType = GenTensor<T>
type ElementType = T
fn rand_usize(
rng: &mut StdRng,
dim: &[usize],
left: usize,
right: usize
) -> Self::TensorType
fn rand_usize(
rng: &mut StdRng,
dim: &[usize],
left: usize,
right: usize
) -> Self::TensorType
Generate a random int close on left, open on right.
fn normal(
rng: &mut StdRng,
dim: &[usize],
mean: Self::ElementType,
std: Self::ElementType
) -> Self::TensorType
fn uniform(
rng: &mut StdRng,
dim: &[usize],
from: Self::ElementType,
to: Self::ElementType
) -> Self::TensorType
Returns the mean value of the tensor along dim row.
Returns the sum of all elements.
let m1 = GenTensor::<f64>::new_raw(&vec![1.,2.,3.,4.,], &vec![2,2]);
assert_eq!(m1.sum(None, false).get_scale(), 10.);
Auto Trait Implementations
impl<T> RefUnwindSafe for GenTensor<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for GenTensor<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more