Struct tensor_rs::tensor_impl::gen_tensor::GenTensor
source · [−]pub struct GenTensor<T> { /* private fields */ }
Expand description
Naive tensor implementation, single thread
Implementations
sourceimpl<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.
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> 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 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> where
F: Fn(&T) -> T,
pub fn _right_broadcast<F>(&self, o: &GenTensor<T>, closure: F) -> GenTensor<T> where
F: Fn(&T, &T) -> 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
sourceimpl<T> CompareTensor for GenTensor<T> where
T: Float,
impl<T> CompareTensor for GenTensor<T> where
T: Float,
sourceimpl<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>)
sourceimpl<'de, T> Deserialize<'de> for GenTensor<T> where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for GenTensor<T> where
T: Deserialize<'de>,
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<T> ElemwiseTensorOp for GenTensor<T> where
T: Float,
impl<T> ElemwiseTensorOp for GenTensor<T> where
T: Float,
sourcefn 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>
sourceimpl<T> IndexSlicing for GenTensor<T> where
T: Float,
impl<T> IndexSlicing for GenTensor<T> where
T: Float,
sourcefn 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.
sourcefn 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.
sourcefn 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.
sourcefn 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]);
sourcefn 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.
sourcefn 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]);
sourcefn gather(&self, dim: usize, index: &Self) -> Self
fn gather(&self, dim: usize, index: &Self) -> Self
Pick elements on the given dimension by the index, and gather them in the output. A restriction is that self.size() and index.size() should be the same on other dimensions. Read more
sourcefn spread(&self, dim: usize, index: &Self, value: &Self) -> Self
fn spread(&self, dim: usize, index: &Self, value: &Self) -> Self
The opposite of gather. Self will be replaced with value along dim by index. Read more
sourcefn index_select(&self, dim: usize, index: &Self) -> Self
fn index_select(&self, dim: usize, index: &Self) -> Self
Select on dim and collect those subtensor by index.
sourcefn index_exclude(&self, dim: usize, index: &Self) -> Self
fn index_exclude(&self, dim: usize, index: &Self) -> Self
Inverse of index_select, remove those subtensor by index along dim.
sourcefn conditional_select(&self, x: &Self, y: &Self) -> Self
fn conditional_select(&self, x: &Self, y: &Self) -> Self
Self is the bool condition, at each position of self, select from x if self at the position is positive or zero, Otherwise , use value from y if self at the position is negative. The restriction is that, self, x, and y all have the same size. Read more
sourceimpl<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
sourcefn normalize_unit(&self) -> Self::TensorType
fn normalize_unit(&self) -> Self::TensorType
Assuming the input is 2 dimensional array, normalize_unit Read more
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
sourceimpl<T> PartialEq<GenTensor<T>> for GenTensor<T> where
T: Float,
impl<T> PartialEq<GenTensor<T>> 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)
sourceimpl<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
sourcefn 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 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
sourceimpl<T> ReduceTensor for GenTensor<T> where
T: Float,
impl<T> ReduceTensor for GenTensor<T> where
T: Float,
sourcefn 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.
sourcefn 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.);
fn argmax(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self
fn argmin(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self
fn dist()
sourcefn logsumexp(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self
fn logsumexp(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self
log(sum(exp(x))), dim is the dimension along which sum is applied. if keep_dim, the dimension along which sum is applied will be kept and be 1. Read more
fn median()
fn mode()
fn prod(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
fn std(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
fn std_mean()
fn unique()
fn unique_consecutive()
fn var(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>
fn var_mean()
fn max(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self
fn min(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self
impl<T> Eq for GenTensor<T> where
T: Float,
Auto Trait Implementations
impl<T> RefUnwindSafe for GenTensor<T> where
T: RefUnwindSafe,
impl<T> Send for GenTensor<T> where
T: Send,
impl<T> Sync for GenTensor<T> where
T: Sync,
impl<T> Unpin for GenTensor<T> where
T: Unpin,
impl<T> UnwindSafe for GenTensor<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more