Struct GenTensor

Source
pub struct GenTensor<T> { /* private fields */ }
Expand description

Naive tensor implementation, single thread

Implementations§

Source§

impl<T> GenTensor<T>
where T: Float,

Source

pub fn new() -> GenTensor<T>

Creation Ops

Source

pub fn new_raw(data: &[T], shape: &[usize]) -> GenTensor<T>

Create a tensor with given Vec.

Source

pub fn new_move(data: Vec<T>, shape: Vec<usize>) -> GenTensor<T>

Source

pub fn data_copy(&mut self, other: &GenTensor<T>)

Source

pub fn index2dimpos(&self, index: usize) -> Vec<usize>

Convert 1 dim index to multi-dim index.

Source

pub fn dimpos2index(&self, dimpos: &[usize]) -> usize

Convert multi-dim index to 1 dim index.

Source

pub fn zeros(size: &[usize]) -> GenTensor<T>

Source

pub fn zeros_like(&self) -> GenTensor<T>

Source

pub fn ones(size: &[usize]) -> GenTensor<T>

Source

pub fn ones_like(&self) -> GenTensor<T>

Source

pub fn arange(end: usize) -> GenTensor<T>

Source

pub fn eye(n: usize, m: usize) -> GenTensor<T>

Source

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]);
Source

pub fn from_record_f32( &mut self, row: usize, record: &[f32], ) -> Result<(), &'static str>

assign a row.

Source

pub fn from_record_f64( &mut self, row: usize, record: &[f64], ) -> Result<(), &'static str>

Source

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]);
Source

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.);
Source

pub fn set(&mut self, o: &[usize], v: T)

Source

pub fn set_1d(&mut self, o: usize, v: T)

Source

pub fn get_mut(&mut self, o: &[usize]) -> &mut T

Source

pub fn get_raw(&self) -> Vec<T>

dump the underlying vec

Source

pub fn get_u8(&self) -> Option<Vec<u8>>

Source

pub fn get_scale(&self) -> T

dump the single value in the tensor if it is the single value in the tensor.

Source

pub fn get_n(&self) -> GenTensor<T>

get NCHW elements, always return the size of left most dimension.

Source

pub fn get_c(&self) -> GenTensor<T>

get NCHW elements, always return the size of second left most dimension.

Source

pub fn get_d(&self) -> GenTensor<T>

get NCDHW elements, will require the self.dim has 5 dimensions.

Source

pub fn get_h(&self) -> GenTensor<T>

get NCDHW elements, will require the self.dim has 5 dimensions or 4 dimensions.

Source

pub fn get_w(&self) -> GenTensor<T>

get NCDHW elements, will require the self.dim has 5 dimensions or 4 dimensions.

Source

pub fn size(&self) -> &Vec<usize>

Returns the size of the self tensor.

Source

pub fn get_size(&self) -> &Vec<usize>

Source

pub fn get_data(&self) -> &Vec<T>

Source

pub fn get_data_mut(&mut self) -> &mut Vec<T>

Source

pub fn numel(&self) -> usize

Returns the total number of elements in the input tensor

Source

pub fn numel_tensor(&self) -> GenTensor<T>

Returns the total number of elements in the input tensor

Source

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.

Source

pub fn set_patch( &mut self, val: &GenTensor<T>, range: &[(usize, usize)], step: Option<&[usize]>, )

Source

pub fn _iter_patch<F>( &self, dim: Option<&[usize]>, keep_dim: bool, closure: F, ) -> GenTensor<T>
where F: Fn(&[T]) -> T,

Source

pub fn _dim_statistic<F>( &self, dim: usize, keepdim: bool, closure: F, ) -> GenTensor<T>
where F: Fn(usize, usize, usize, usize, usize) -> T,

Source

pub fn get_diag(&self) -> GenTensor<T>

Source

pub fn set_diag(&mut self, o: &GenTensor<T>)

Source

pub fn get_column(&self, i: usize) -> GenTensor<T>

Source

pub fn set_column(&mut self, o: &GenTensor<T>, i: usize)

Source

pub fn get_row(&self, i: usize) -> GenTensor<T>

Source

pub fn set_row(&mut self, o: &GenTensor<T>, i: usize)

Source

pub fn _pointwise<F>(&self, closure: F) -> GenTensor<T>
where F: Fn(&T) -> T,

Source

pub fn _right_broadcast<F>(&self, o: &GenTensor<T>, closure: F) -> GenTensor<T>
where F: Fn(&T, &T) -> T,

Source

pub fn log10_like(&self) -> GenTensor<T>

Source

pub fn log2_like(&self) -> GenTensor<T>

Source

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.);
Source

pub fn sub(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn mul(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn div(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

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]), "");
Source

pub fn dot(&self, b: &GenTensor<T>) -> T

Source

pub fn proj(&self, b: &GenTensor<T>) -> GenTensor<T>

Project a vector onto another one.

Source

pub fn matmul(&self, o: &GenTensor<T>) -> GenTensor<T>

matrix multiplication of two tensor This is also for dot/inner product.

Source

pub fn outer(&self, o: &GenTensor<T>, avg: Option<bool>) -> GenTensor<T>

outer product of right-most dimensions.

Source

pub fn all_close(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn arg_sort(&self, dim: usize, descending: bool) -> GenTensor<T>

Source

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.);
Source

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)
Source

pub fn ge(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn gt(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn le(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn lt(&self, o: &GenTensor<T>) -> GenTensor<T>

Source

pub fn ne(&self, o: &GenTensor<T>) -> GenTensor<T>

Source§

impl GenTensor<f32>

Source

pub fn squared_error(t1: &Self, t2: &Self) -> GenTensor<f32>

Source§

impl GenTensor<f64>

Source

pub fn squared_error(t1: &Self, t2: &Self) -> GenTensor<f64>

Trait Implementations§

Source§

impl<T> Clone for GenTensor<T>
where T: Float,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> CompareTensor for GenTensor<T>
where T: Float,

Source§

type TensorType = GenTensor<T>

Source§

type ElementType = T

Source§

fn max_pair(&self, o: &GenTensor<T>) -> GenTensor<T>

Source§

fn min_pair(&self, o: &GenTensor<T>) -> GenTensor<T>

Source§

fn all(&self, f: &dyn Fn(Self::ElementType) -> bool) -> bool

Source§

fn any(&self, f: &dyn Fn(Self::ElementType) -> bool) -> bool

Source§

impl<T> Convolution for GenTensor<T>
where T: Float,

Source§

fn conv2d( &self, filter: &GenTensor<T>, stride: (usize, usize), padding: (usize, usize), dilation: (usize, usize), padding_mode: PaddingMode, ) -> Self

Source§

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)

Source§

fn conv_gen( &self, filter: &GenTensor<T>, stride: &[usize], padding: &[usize], dilation: &[usize], padding_mode: PaddingMode, ) -> GenTensor<T>

Source§

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 Debug for GenTensor<f32>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Debug for GenTensor<f64>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for GenTensor<T>
where T: Float,

Source§

fn default() -> GenTensor<T>

Returns the “default value” for a type. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for GenTensor<f32>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for GenTensor<f64>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> ElemwiseTensorOp for GenTensor<T>
where T: Float,

Source§

fn log1pexp(&self) -> GenTensor<T>

Source§

type TensorType = GenTensor<T>

Source§

type ElementType = T

Source§

fn abs(&self) -> GenTensor<T>

Source§

fn acos(&self) -> GenTensor<T>

Source§

fn asin(&self) -> GenTensor<T>

Source§

fn atan(&self) -> GenTensor<T>

Source§

fn ceil(&self) -> GenTensor<T>

Source§

fn clamp(&self, min: T, max: T) -> GenTensor<T>

Source§

fn cos(&self) -> GenTensor<T>

Source§

fn cosh(&self) -> GenTensor<T>

Source§

fn exp(&self) -> GenTensor<T>

Source§

fn expm1(&self) -> GenTensor<T>

Source§

fn floor(&self) -> GenTensor<T>

Source§

fn frac(&self) -> GenTensor<T>

Source§

fn log(&self) -> GenTensor<T>

Source§

fn log10(&self) -> GenTensor<T>

Source§

fn log1p(&self) -> GenTensor<T>

Source§

fn log2(&self) -> GenTensor<T>

Source§

fn neg(&self) -> GenTensor<T>

Source§

fn pow(&self, n: T) -> GenTensor<T>

Source§

fn reciprocal(&self) -> GenTensor<T>

Source§

fn round(&self) -> GenTensor<T>

Source§

fn rsqrt(&self) -> GenTensor<T>

Source§

fn sigmoid(&self) -> GenTensor<T>

Source§

fn sign(&self) -> GenTensor<T>

Source§

fn sin(&self) -> GenTensor<T>

Source§

fn sinh(&self) -> GenTensor<T>

Source§

fn sqrt(&self) -> GenTensor<T>

Source§

fn square(&self) -> GenTensor<T>

Source§

fn tan(&self) -> GenTensor<T>

Source§

fn tanh(&self) -> GenTensor<T>

Source§

fn trunc(&self) -> GenTensor<T>

Source§

impl<T> IndexSlicing for GenTensor<T>
where T: Float,

Source§

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>

Splits a tensor into a specific number of chunks.

Source§

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

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

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

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

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.
Source§

fn spread(&self, dim: usize, index: &Self, value: &Self) -> Self

The opposite of gather. Self will be replaced with value along dim by index.
Source§

fn index_select(&self, dim: usize, index: &Self) -> Self

Select on dim and collect those subtensor by index.
Source§

fn index_exclude(&self, dim: usize, index: &Self) -> Self

Inverse of index_select, remove those subtensor by index along dim.
Source§

fn reshape(&self, new_shape: &[usize]) -> Self

Just change the index boundary.
Source§

fn squeeze(&self, dim: Option<usize>) -> Self

Remove dimension with length of 1.
Source§

fn t(&self) -> Self

Transpose
Source§

fn unsqueeze(&self, dim: usize) -> Self

Add size 1 dimension at dim.
Source§

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.
Source§

fn repeat(&self, sizes: &[usize]) -> Self

Repeat the tensor along all dimensions, the number of repeat is specified in sizes. Thus the restriction is that self.size().len() is equal to sizes.len().
Source§

impl<T> LinearAlgbra for GenTensor<T>
where T: Float,

Source§

type TensorType = GenTensor<T>

Source§

type ElementType = T

Source§

fn norm(&self) -> Self::TensorType

Source§

fn normalize_unit(&self) -> Self::TensorType

Assuming the input is 2 dimensional array, normalize_unit
Source§

fn lu(&self) -> Option<[Self::TensorType; 2]>

Source§

fn lu_solve(&self, b: &Self::TensorType) -> Option<Self::TensorType>

Source§

fn qr(&self) -> Option<[Self::TensorType; 2]>

Source§

fn eigen(&self) -> Option<[Self::TensorType; 2]>

Source§

fn cholesky(&self) -> Option<Self::TensorType>

Source§

fn det(&self) -> Option<Self::TensorType>

Source§

fn svd(&self) -> Option<[Self::TensorType; 3]>

Source§

fn inv(&self) -> Option<Self::TensorType>

Source§

fn pinv(&self) -> Self::TensorType

Source§

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)
Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Random for GenTensor<T>

Source§

type TensorType = GenTensor<T>

Source§

type ElementType = T

Source§

fn rand_usize( rng: &mut StdRng, dim: &[usize], left: usize, right: usize, ) -> Self::TensorType

Generate a random int close on left, open on right.
Source§

fn bernoulli() -> Self::TensorType

Source§

fn cauchy() -> Self::TensorType

Source§

fn exponential() -> Self::TensorType

Source§

fn geometric() -> Self::TensorType

Source§

fn log_normal() -> Self::TensorType

Source§

fn normal( rng: &mut StdRng, dim: &[usize], mean: Self::ElementType, std: Self::ElementType, ) -> Self::TensorType

Source§

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,

Source§

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>

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.);
Source§

fn argmax(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self

Source§

fn argmin(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self

Source§

fn dist()

Source§

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.
Source§

fn median()

Source§

fn mode()

Source§

fn prod(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>

Source§

fn std(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>

Source§

fn std_mean()

Source§

fn unique()

Source§

fn unique_consecutive()

Source§

fn var(&self, dim: Option<&[usize]>, keep_dim: bool) -> GenTensor<T>

Source§

fn var_mean()

Source§

fn max(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self

Source§

fn min(&self, dim: Option<&[usize]>, keep_dim: bool) -> Self

Source§

impl<T> Serialize for GenTensor<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> Eq for GenTensor<T>
where T: Float,

Auto Trait Implementations§

§

impl<T> Freeze for GenTensor<T>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,