pub struct GenTensor<T> { /* private fields */ }
Expand description
Naive tensor implementation, single thread
Implementations§
Source§impl<T> GenTensor<T>where
T: Float,
impl<T> GenTensor<T>where
T: Float,
pub fn new_move(data: Vec<T>, shape: Vec<usize>) -> GenTensor<T>
pub fn data_copy(&mut self, other: &GenTensor<T>)
Sourcepub fn index2dimpos(&self, index: usize) -> Vec<usize>
pub fn index2dimpos(&self, index: usize) -> Vec<usize>
Convert 1 dim index to multi-dim index.
Sourcepub fn dimpos2index(&self, dimpos: &[usize]) -> usize
pub fn dimpos2index(&self, dimpos: &[usize]) -> usize
Convert multi-dim index to 1 dim index.
pub fn zeros(size: &[usize]) -> GenTensor<T>
pub fn zeros_like(&self) -> GenTensor<T>
pub fn ones(size: &[usize]) -> GenTensor<T>
pub fn ones_like(&self) -> GenTensor<T>
pub fn arange(end: usize) -> GenTensor<T>
pub fn eye(n: usize, m: usize) -> GenTensor<T>
Sourcepub fn fill(d: T, shape: &[usize]) -> GenTensor<T>
pub fn fill(d: T, shape: &[usize]) -> GenTensor<T>
Create a tensor filled with the same value d
let m1 = GenTensor::<f64>::fill(1., &vec![3,5,2]);
Sourcepub fn from_record_f32(
&mut self,
row: usize,
record: &[f32],
) -> Result<(), &'static str>
pub fn from_record_f32( &mut self, row: usize, record: &[f32], ) -> Result<(), &'static str>
assign a row.
pub fn from_record_f64( &mut self, row: usize, record: &[f64], ) -> Result<(), &'static str>
Sourcepub fn stride(&self) -> Vec<usize>
pub fn stride(&self) -> Vec<usize>
stride() returns a vector contains steps for each dimension on linear storage. 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]);
Sourcepub fn get(&self, o: &[usize]) -> T
pub fn get(&self, o: &[usize]) -> T
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.);
pub fn set(&mut self, o: &[usize], v: T)
pub fn set_1d(&mut self, o: usize, v: T)
pub fn get_mut(&mut self, o: &[usize]) -> &mut T
pub fn get_u8(&self) -> Option<Vec<u8>>
Sourcepub fn get_scale(&self) -> T
pub fn get_scale(&self) -> T
dump the single value in the tensor if it is the single value in the tensor.
Sourcepub fn get_n(&self) -> GenTensor<T>
pub fn get_n(&self) -> GenTensor<T>
get NCHW elements, always return the size of left most dimension.
Sourcepub fn get_c(&self) -> GenTensor<T>
pub fn get_c(&self) -> GenTensor<T>
get NCHW elements, always return the size of second left most dimension.
Sourcepub fn get_d(&self) -> GenTensor<T>
pub fn get_d(&self) -> GenTensor<T>
get NCDHW elements, will require the self.dim has 5 dimensions.
Sourcepub fn get_h(&self) -> GenTensor<T>
pub fn get_h(&self) -> GenTensor<T>
get NCDHW elements, will require the self.dim has 5 dimensions or 4 dimensions.
Sourcepub fn get_w(&self) -> GenTensor<T>
pub fn get_w(&self) -> GenTensor<T>
get NCDHW elements, will require the self.dim has 5 dimensions or 4 dimensions.
pub fn get_size(&self) -> &Vec<usize>
pub fn get_data(&self) -> &Vec<T>
pub fn get_data_mut(&mut self) -> &mut Vec<T>
Sourcepub fn numel_tensor(&self) -> GenTensor<T>
pub fn numel_tensor(&self) -> GenTensor<T>
Returns the total number of elements in the input tensor
Sourcepub fn get_patch(
&self,
range: &[(usize, usize)],
step: Option<&[usize]>,
) -> GenTensor<T>
pub fn get_patch( &self, range: &[(usize, usize)], step: Option<&[usize]>, ) -> GenTensor<T>
Return portion of the image. Every range of each dim with inclusive start and exclusive end. The pixel can be skipped by setting step larger than 1.
pub fn set_patch( &mut self, val: &GenTensor<T>, range: &[(usize, usize)], step: Option<&[usize]>, )
pub fn _iter_patch<F>( &self, dim: Option<&[usize]>, keep_dim: bool, closure: F, ) -> GenTensor<T>
pub fn _dim_statistic<F>( &self, dim: usize, keepdim: bool, closure: F, ) -> GenTensor<T>
pub fn get_diag(&self) -> GenTensor<T>
pub fn set_diag(&mut self, o: &GenTensor<T>)
pub fn get_column(&self, i: usize) -> GenTensor<T>
pub fn set_column(&mut self, o: &GenTensor<T>, i: usize)
pub fn get_row(&self, i: usize) -> GenTensor<T>
pub fn set_row(&mut self, o: &GenTensor<T>, i: usize)
pub fn _pointwise<F>(&self, closure: F) -> GenTensor<T>
pub fn _right_broadcast<F>(&self, o: &GenTensor<T>, closure: F) -> GenTensor<T>
pub fn log10_like(&self) -> GenTensor<T>
pub fn log2_like(&self) -> GenTensor<T>
Sourcepub fn add(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn add(&self, o: &GenTensor<T>) -> GenTensor<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.);
pub fn sub(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn mul(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn div(&self, o: &GenTensor<T>) -> GenTensor<T>
Sourcepub fn mm(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn mm(&self, o: &GenTensor<T>) -> GenTensor<T>
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]), "");
pub fn dot(&self, b: &GenTensor<T>) -> T
Sourcepub fn matmul(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn matmul(&self, o: &GenTensor<T>) -> GenTensor<T>
matrix multiplication of two tensor This is also for dot/inner product.
Sourcepub fn outer(&self, o: &GenTensor<T>, avg: Option<bool>) -> GenTensor<T>
pub fn outer(&self, o: &GenTensor<T>, avg: Option<bool>) -> GenTensor<T>
outer product of right-most dimensions.
pub fn all_close(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn arg_sort(&self, dim: usize, descending: bool) -> GenTensor<T>
Sourcepub fn eq_t(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn eq_t(&self, o: &GenTensor<T>) -> GenTensor<T>
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.);
Sourcepub fn equal(&self, o: &GenTensor<T>) -> bool
pub fn equal(&self, o: &GenTensor<T>) -> bool
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)
pub fn ge(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn gt(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn le(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn lt(&self, o: &GenTensor<T>) -> GenTensor<T>
pub fn ne(&self, o: &GenTensor<T>) -> GenTensor<T>
Trait Implementations§
Source§impl<T> CompareTensor for GenTensor<T>where
T: Float,
impl<T> CompareTensor for GenTensor<T>where
T: Float,
type TensorType = GenTensor<T>
type ElementType = T
fn max_pair(&self, o: &GenTensor<T>) -> GenTensor<T>
fn min_pair(&self, o: &GenTensor<T>) -> GenTensor<T>
fn all(&self, f: &dyn Fn(Self::ElementType) -> bool) -> bool
fn any(&self, f: &dyn Fn(Self::ElementType) -> bool) -> bool
Source§impl<T> Convolution for GenTensor<T>where
T: Float,
impl<T> Convolution for GenTensor<T>where
T: Float,
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)
fn conv_gen( &self, filter: &GenTensor<T>, stride: &[usize], padding: &[usize], dilation: &[usize], padding_mode: PaddingMode, ) -> GenTensor<T>
fn conv_grad_gen( &self, filter: &GenTensor<T>, stride: &[usize], padding: &[usize], dilation: &[usize], padding_mode: PaddingMode, output_grad: &GenTensor<T>, ) -> (GenTensor<T>, GenTensor<T>)
Source§impl<'de, T> Deserialize<'de> for GenTensor<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for GenTensor<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T> ElemwiseTensorOp for GenTensor<T>where
T: Float,
impl<T> ElemwiseTensorOp for GenTensor<T>where
T: Float,
Source§fn log1pexp(&self) -> GenTensor<T>
fn log1pexp(&self) -> GenTensor<T>
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
fn abs(&self) -> GenTensor<T>
fn acos(&self) -> GenTensor<T>
fn asin(&self) -> GenTensor<T>
fn atan(&self) -> GenTensor<T>
fn ceil(&self) -> GenTensor<T>
fn clamp(&self, min: T, max: T) -> GenTensor<T>
fn cos(&self) -> GenTensor<T>
fn cosh(&self) -> GenTensor<T>
fn exp(&self) -> GenTensor<T>
fn expm1(&self) -> GenTensor<T>
fn floor(&self) -> GenTensor<T>
fn frac(&self) -> GenTensor<T>
fn log(&self) -> GenTensor<T>
fn log10(&self) -> GenTensor<T>
fn log1p(&self) -> GenTensor<T>
fn log2(&self) -> GenTensor<T>
fn neg(&self) -> GenTensor<T>
fn pow(&self, n: T) -> GenTensor<T>
fn reciprocal(&self) -> GenTensor<T>
fn round(&self) -> GenTensor<T>
fn rsqrt(&self) -> GenTensor<T>
fn sigmoid(&self) -> GenTensor<T>
fn sign(&self) -> GenTensor<T>
fn sin(&self) -> GenTensor<T>
fn sinh(&self) -> GenTensor<T>
fn sqrt(&self) -> GenTensor<T>
fn square(&self) -> GenTensor<T>
fn tan(&self) -> GenTensor<T>
fn tanh(&self) -> GenTensor<T>
fn trunc(&self) -> GenTensor<T>
Source§impl<T> IndexSlicing for GenTensor<T>where
T: Float,
impl<T> IndexSlicing for GenTensor<T>where
T: Float,
Source§fn cat(&self, tensors: &[Self], dim: usize) -> Self
fn cat(&self, tensors: &[Self], dim: usize) -> Self
Concatenates the given sequence of seq tensors in the given dimension.
Source§fn chunk(&self, chunks: usize, dim: usize) -> Vec<Self>
fn chunk(&self, chunks: usize, dim: usize) -> Vec<Self>
Splits a tensor into a specific number of chunks.
Source§fn split(&self, sections: &[usize], dim: usize) -> Vec<Self>
fn split(&self, sections: &[usize], dim: usize) -> Vec<Self>
Splits the tensor into chunks. Each chunk is a view of the original tensor.
Source§fn stack(&self, tensors: &[Self], dim: usize) -> Self
fn stack(&self, tensors: &[Self], dim: usize) -> Self
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]);
Source§fn take(&self, index: &[usize]) -> Self
fn take(&self, index: &[usize]) -> Self
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.
Source§fn permute(&self, dims: &[usize]) -> Self
fn permute(&self, dims: &[usize]) -> Self
Permute the dimensions of this tensor.
let mut m1 = GenTensor::<f64>::fill(1., &vec![2, 3, 5]);
m1.permute(&vec![2, 0, 1]);
Source§fn gather(&self, dim: usize, index: &Self) -> Self
fn gather(&self, dim: usize, index: &Self) -> Self
Source§fn spread(&self, dim: usize, index: &Self, value: &Self) -> Self
fn spread(&self, dim: usize, index: &Self, value: &Self) -> Self
Source§fn index_select(&self, dim: usize, index: &Self) -> Self
fn index_select(&self, dim: usize, index: &Self) -> Self
Source§fn index_exclude(&self, dim: usize, index: &Self) -> Self
fn index_exclude(&self, dim: usize, index: &Self) -> Self
Source§fn conditional_select(&self, x: &Self, y: &Self) -> Self
fn conditional_select(&self, x: &Self, y: &Self) -> Self
Source§impl<T> LinearAlgbra for GenTensor<T>where
T: Float,
impl<T> LinearAlgbra for GenTensor<T>where
T: Float,
type TensorType = GenTensor<T>
type ElementType = T
fn norm(&self) -> Self::TensorType
Source§fn normalize_unit(&self) -> Self::TensorType
fn normalize_unit(&self) -> Self::TensorType
fn lu(&self) -> Option<[Self::TensorType; 2]>
fn lu_solve(&self, b: &Self::TensorType) -> Option<Self::TensorType>
fn qr(&self) -> Option<[Self::TensorType; 2]>
fn eigen(&self) -> Option<[Self::TensorType; 2]>
fn cholesky(&self) -> Option<Self::TensorType>
fn det(&self) -> Option<Self::TensorType>
fn svd(&self) -> Option<[Self::TensorType; 3]>
fn inv(&self) -> Option<Self::TensorType>
fn pinv(&self) -> Self::TensorType
fn tr(&self) -> Self::TensorType
Source§impl<T> PartialEq for GenTensor<T>where
T: Float,
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> PartialEq for GenTensor<T>where
T: Float,
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)
Source§impl<T> Random for GenTensor<T>
impl<T> Random for GenTensor<T>
type TensorType = GenTensor<T>
type ElementType = T
Source§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
fn bernoulli() -> Self::TensorType
fn cauchy() -> Self::TensorType
fn exponential() -> Self::TensorType
fn geometric() -> Self::TensorType
fn log_normal() -> Self::TensorType
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
Source§impl<T> ReduceTensor for GenTensor<T>where
T: Float,
impl<T> ReduceTensor for GenTensor<T>where
T: Float,
Source§fn mean(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
fn mean(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
Returns the mean value of the tensor along dim row.
Source§fn sum(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
fn sum(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
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.);