[][src]Struct auto_diff::tensor::GenTensor

pub struct GenTensor<T> { /* fields omitted */ }

Naive tensor implementation, single thread, no check.

Methods

impl<T> GenTensor<T> where
    T: Float
[src]

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

Create a tensor with given Vec.

pub fn fill(d: T, shape: &Vec<usize>) -> GenTensor<T>[src]

Create a tensor filled with the same value d

let m1 = GenTensor::<f64>::fill(1., &vec![3,5,2]);

pub fn stride(&self) -> Vec<usize>[src]

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

pub fn get(&self, o: &Vec<usize>) -> T[src]

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 get_mut(&mut self, o: &Vec<usize>) -> &mut T[src]

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

dump the underlying vec

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

Returns the size of the self tensor.

pub fn numel(&self) -> usize[src]

Returns the total number of elements in the input tensor.

pub fn add(&self, o: &GenTensor<T>) -> GenTensor<T>[src]

element-wise add.

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>[src]

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

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

pub fn mm(&self, o: &GenTensor<T>) -> GenTensor<T>[src]

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 matmul(&self, o: &GenTensor<T>) -> GenTensor<T>[src]

matrix multiplication of two tensor

pub fn stack(tensors: &Vec<&Self>, dim: usize) -> GenTensor<T>[src]

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 = GenTensor::<f64>::stack(&vec![&m1, &m2], 1);
let raw = result.get_raw();
for i in raw {
    println!("{}", i);
}
assert_eq!(result.size(), vec![3,2,2]);

pub fn permute(&mut self, dims: &Vec<usize>)[src]

Permute the dimensions of this tensor.

let mut m1 = GenTensor::<f64>::fill(1., &vec![2, 3, 5]);
m1.permute(&vec![2, 0, 1]);

pub fn eq_t(&self, o: &GenTensor<T>) -> GenTensor<T>[src]

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

pub fn equal(&self, o: &GenTensor<T>) -> Result<(), ()>[src]

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), Ok(()))

Trait Implementations

impl<T> Display for GenTensor<T>[src]

impl<T> Eq for GenTensor<T> where
    T: Float
[src]

impl<T> PartialEq<GenTensor<T>> for GenTensor<T> where
    T: Float
[src]

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)

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.