Struct Var

Source
pub struct Var { /* private fields */ }
Expand description

Var can be thought as the value it holds plus a link to the computation graph. Majority of operators are methods on Var.

Implementations§

Source§

impl Var

Source

pub fn new(input: &[f64], dim: &[usize]) -> Var

Source

pub fn new_f64(input: &[f64], dim: &[usize]) -> Var

Source

pub fn new_f32(input: &[f32], dim: &[usize]) -> Var

Source

pub fn ref_copy(self: &Var) -> Var

Where it needs a assign operator, we should use this ref_copy. If a hard copy is necessary, then call clone().

Source

pub fn set(&self, o: &Var)

With a &Var, use set to copy a value.

Source

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

Source

pub fn numel(&self) -> usize

Source

pub fn get_f32(&self, o: &[usize]) -> Result<f32, AutoDiffError>

Source

pub fn set_f32(&self, o: &[usize], v: f32) -> Result<(), AutoDiffError>

Source

pub fn get_f64(&self, o: &[usize]) -> Result<f64, AutoDiffError>

Source

pub fn set_f64(&self, o: &[usize], v: f64) -> Result<(), AutoDiffError>

Source

pub fn fill(size: &[usize], fill_value: &Var) -> Var

Source

pub fn fill_f32(size: &[usize], fill_value: f32) -> Var

Source

pub fn fill_f64(size: &[usize], fill_value: f64) -> Var

Source

pub fn zeros(dim: &[usize]) -> Var

Source

pub fn ones(dim: &[usize]) -> Var

Source

pub fn twos(dim: &[usize]) -> Var

Source

pub fn eye(n: usize, m: usize) -> Var

Identity matrix

Source

pub fn empty(dim: &[usize]) -> Var

Source

pub fn from_record_f32(&self, row: usize, record: &[f32])

Fill row by row.

Source

pub fn from_record_f64(&self, row: usize, record: &[f64])

Source

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

Source

pub fn normal_f64(rng: &mut StdRng, dim: &[usize], mean: f64, std: f64) -> Var

Source

pub fn normal_f32(rng: &mut StdRng, dim: &[usize], mean: f32, std: f32) -> Var

Source

pub fn normal(rng: &mut StdRng, dim: &[usize], mean: f64, std: f64) -> Var

Source

pub fn uniform_f64(rng: &mut StdRng, dim: &[usize], from: f64, to: f64) -> Var

Source

pub fn uniform_f32(rng: &mut StdRng, dim: &[usize], from: f32, to: f32) -> Var

Source

pub fn uniform(rng: &mut StdRng, dim: &[usize], from: f64, to: f64) -> Var

Source

pub fn _add(&self, other: &Var) -> Var

Source

pub fn _sub(&self, other: &Var) -> Var

Source

pub fn _mul(&self, other: &Var) -> Var

Source

pub fn _div(&self, other: &Var) -> Var

Source

pub fn matmul(&self, other: &Var) -> Result<Var, AutoDiffError>

Matrix/inner/dot product

§use auto_diff::{Var, var_f64, AutoDiffError};
§extern crate openblas_src;
§fn test_matmul() -> Result<(), AutoDiffError> {

let v1 = var_f64!([[1., 2., 3.], [4., 5., 6.]]); let v2 = var_f64!([[11., 12., 13.], [14., 15., 16.], [17., 18., 19.]]); let v3 = v1.matmul(&v2)?; let em = var_f64!([[90.0, 96.0, 102.0], [216.0, 231.0, 246.0]]); assert_eq!(v3, em);

§Ok(())
§}
§test_matmul();
Source

pub fn outer(&self, other: &Var) -> Result<Var, AutoDiffError>

Outer product

let v1 = Var::new_f64(&[1., 2., 3.], &[3]);
let v2 = Var::new_f64(&[4., 5., 6.], &[3]);
let v3 = v1.outer(&v2)?;
let em = var_f64!([[4.,   5.,  6.],
                   [8.,  10., 12.],
                   [12., 15., 18.]]);
assert_eq!(v3, em);
Source

pub fn elu(&self, alpha: Var) -> Result<Var, AutoDiffError>

Source

pub fn relu(&self) -> Result<Var, AutoDiffError>

Source

pub fn sigmoid(&self) -> Result<Var, AutoDiffError>

Source

pub fn mse_loss(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn bce_with_logits_loss(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn cross_entropy_loss(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn abs(&self) -> Result<Var, AutoDiffError>

Source

pub fn acos(&self) -> Result<Var, AutoDiffError>

Source

pub fn asin(&self) -> Result<Var, AutoDiffError>

Source

pub fn atan(&self) -> Result<Var, AutoDiffError>

Source

pub fn ceil(&self) -> Result<Var, AutoDiffError>

Source

pub fn cos(&self) -> Result<Var, AutoDiffError>

Source

pub fn cosh(&self) -> Result<Var, AutoDiffError>

Source

pub fn exp(&self) -> Result<Var, AutoDiffError>

Source

pub fn expm1(&self) -> Result<Var, AutoDiffError>

Source

pub fn floor(&self) -> Result<Var, AutoDiffError>

Source

pub fn frac(&self) -> Result<Var, AutoDiffError>

Source

pub fn log(&self) -> Result<Var, AutoDiffError>

Source

pub fn log10(&self) -> Result<Var, AutoDiffError>

Source

pub fn log1p(&self) -> Result<Var, AutoDiffError>

Source

pub fn log1pexp(&self) -> Result<Var, AutoDiffError>

Source

pub fn log2(&self) -> Result<Var, AutoDiffError>

Source

pub fn neg(&self) -> Result<Var, AutoDiffError>

Source

pub fn _neg(&self) -> Var

Source

pub fn reciprocal(&self) -> Result<Var, AutoDiffError>

Source

pub fn round(&self) -> Result<Var, AutoDiffError>

Source

pub fn rsqrt(&self) -> Result<Var, AutoDiffError>

Source

pub fn sign(&self) -> Result<Var, AutoDiffError>

Source

pub fn sin(&self) -> Result<Var, AutoDiffError>

Source

pub fn sinh(&self) -> Result<Var, AutoDiffError>

Source

pub fn sqrt(&self) -> Result<Var, AutoDiffError>

Source

pub fn tan(&self) -> Result<Var, AutoDiffError>

Source

pub fn tanh(&self) -> Result<Var, AutoDiffError>

Source

pub fn trunc(&self) -> Result<Var, AutoDiffError>

Source

pub fn max_pair(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn min_pair(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn arg_sort( &self, dim: usize, descending: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn eq_elem(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn equal(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn ge(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn gt(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn le(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn lt(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn ne(&self, other: &Var) -> Result<Var, AutoDiffError>

Source

pub fn cat(&self, other: &[Var], dim: usize) -> Result<Var, AutoDiffError>

Concatenates the given sequence of seq tensors in the given dimension. The input tensor should all have the same size except on the given dimension. The output tensor will have all the same size as the input except the given dimension, which will be the sum of the inputs on the given dimension. Apply cat on [tensor(5, 3, 2), tensor(5, 7, 2), ] will get a tensor(5, 10, 2).

§use auto_diff::{Var, var_f64, AutoDiffError};
§extern crate openblas_src;
§fn test_cat() -> Result<(), AutoDiffError> {

let m1 = Var::empty(&[3, 1]); let m2 = Var::empty(&[3, 1]); let m3 = Var::empty(&[3, 1]); let m4 = m1.cat(&[m2, m3], 1)?; assert_eq!(m4.size(), [3, 3]);

§Ok(())
§}
§test_cat();
Source

pub fn chunk( &self, chunks: usize, dim: usize, ) -> Result<Vec<Var>, AutoDiffError>

Source

pub fn conditional_select(&self, x: &Var, y: &Var) -> Result<Var, AutoDiffError>

Source

pub fn gather(&self, dim: usize, index: Var) -> Result<Var, AutoDiffError>

Source

pub fn index_select(&self, dim: usize, index: Var) -> Result<Var, AutoDiffError>

Source

pub fn index_exclude( &self, dim: usize, index: Var, ) -> Result<Var, AutoDiffError>

Source

pub fn permute(&self, dim: &[usize]) -> Result<Var, AutoDiffError>

Source

pub fn repeat(&self, dim: &[usize]) -> Result<Var, AutoDiffError>

Source

pub fn reshape(&self, new_shape: &[usize]) -> Result<Var, AutoDiffError>

Source

pub fn split( &self, sections: &[usize], dim: usize, ) -> Result<Vec<Var>, AutoDiffError>

Source

pub fn squeeze(&self, dim: Option<usize>) -> Result<Var, AutoDiffError>

Source

pub fn t(&self) -> Result<Var, AutoDiffError>

Source

pub fn take(&self, index: &[usize]) -> Result<Var, AutoDiffError>

Source

pub fn unsqueeze(&self, dim: usize) -> Result<Var, AutoDiffError>

Source

pub fn stack(&self, other: &[Var], dim: usize) -> Result<Var, AutoDiffError>

Stack tensor with the same size along a new dimension specified by dim. The difference from cat is that cat don’t create new dimension.

let m1 = var_f64!([[1., 2., ],
               [3., 4., ]]);
let m2 = var_f64!([[5., 6., ],
               [7., 8., ]]);
let m3 = m1.stack(&[m2], 1)?;
Source

pub fn det(&self) -> Result<Var, AutoDiffError>

Source

pub fn inv(&self) -> Result<Var, AutoDiffError>

Source

pub fn normalize_unit(&self) -> Result<Var, AutoDiffError>

Source

pub fn tr(&self) -> Result<Var, AutoDiffError>

Source

pub fn argmax( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn argmin( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn logsumexp( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn mean( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn prod( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn std( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn sum( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn var( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn max( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn min( &self, dim: Option<&[usize]>, keepdim: bool, ) -> Result<Var, AutoDiffError>

Source

pub fn get_patch( &self, range: &[(usize, usize)], step: Option<&[usize]>, ) -> Result<Var, AutoDiffError>

Get a portion of the tensor and return it.

let m1 = var_f64!([[1., 2., 3.],
                   [4., 5., 6.],
                   [7., 8., 9.]]);
let m2 = var_f64!([[4., 5.],
                   [7., 8.]]);
assert_eq!(m1.get_patch(&[(1, 3), (0, 2)], None)?, m2);
Source

pub fn set_patch( &self, other: &Var, range: &[(usize, usize)], step: Option<&[usize]>, ) -> Result<Var, AutoDiffError>

Set a portion of the tensor.

let m1 = var_f64!([[1., 2., 3.],
                   [4., 5., 6.],
                   [7., 8., 9.]]);
let m2 = var_f64!([[10., 11.],
                   [12., 13.]]);
let m3 = var_f64!([[1.,   2., 3.],
                   [10., 11., 6.],
                   [12., 13., 9.]]);
assert_eq!(m1.set_patch(&m2, &[(1, 3), (0, 2)], None)?, m3);
Source

pub fn view(&self, new_shape: &[usize]) -> Result<Var, AutoDiffError>

Source

pub fn val(&self) -> Tensor

Source

pub fn set_grad(&self, use_gradient: bool)

Use gradient or not, default is to use.

Source

pub fn reset_net(&self)

Reset net in the background.

Source

pub fn grad(&self) -> Result<Var, AutoDiffError>

The current gradient for the Var.

Source

pub fn bp(&self) -> Result<(), AutoDiffError>

Apply back propagation to get numerical gradient.

Source

pub fn step(&self, opt: &mut dyn Optimizer) -> Result<(), AutoDiffError>

Source

pub fn rerun(&self) -> Result<(), AutoDiffError>

Run the computation graph again.

Source

pub fn get_io_var(&self) -> Result<(Vec<Var>, Vec<Var>), AutoDiffError>

Extract input and output from the hidden net.

Source

pub fn get_var_by_label(&self, label: &str) -> Result<Var, AutoDiffError>

Get var by string label

Source

pub fn set_label(&self, label: &str) -> Result<(), AutoDiffError>

Source

pub fn set_predict(&self) -> Result<(), AutoDiffError>

Source

pub fn predict(&self) -> Result<Var, AutoDiffError>

Source

pub fn dump_net(&self) -> Rc<RefCell<Net>>

For debug.

Trait Implementations§

Source§

impl Add for Var

Source§

type Output = Var

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Var

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 Debug for Var

Source§

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

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

impl<'de> Deserialize<'de> for Var

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 Var

Source§

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

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

impl Div for Var

Source§

type Output = Var

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
Source§

impl Mul for Var

Source§

type Output = Var

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl Neg for Var

Source§

type Output = Var

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Var

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 Serialize for Var

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 Sub for Var

Source§

type Output = Var

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl TryFrom<Var> for Vec<usize>

Source§

type Error = AutoDiffError

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

fn try_from(value: Var) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Var> for f32

Source§

type Error = AutoDiffError

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

fn try_from(value: Var) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Var> for f64

Source§

type Error = AutoDiffError

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

fn try_from(value: Var) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for Var

Auto Trait Implementations§

§

impl Freeze for Var

§

impl !RefUnwindSafe for Var

§

impl !Send for Var

§

impl !Sync for Var

§

impl Unpin for Var

§

impl !UnwindSafe for Var

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