Struct Variable

Source
pub struct Variable<'v> {
    pub graph: &'v Graph,
    pub index: usize,
    pub value: f64,
}
Expand description

Struct to contain the initial variables.

Fields§

§graph: &'v Graph

Pointer to the graph.

§index: usize

Index to the vertex.

§value: f64

Value associated to the vertex.

Implementations§

Source§

impl<'v> Variable<'v>

Source

pub fn abs(self) -> Variable<'v>

Absolute value function. d/dx abs(x) = sign(x)


let g = Graph::new();

let x1 = g.var(1.0);
let z1 = x1.abs();
let grad1 = z1.accumulate();
assert!((z1.value - 1.0).abs() <= 1e-15);
assert!((grad1.wrt(&x1) - 1.0).abs() <= 1e-15);

let x2 = g.var(-1.0);
let z2 = x2.abs();
let grad2 = z2.accumulate();
assert!((z2.value - 1.0).abs() <= 1e-15);
assert!((grad2.wrt(&x2) - (-1.0)).abs() <= 1e-15);
Source

pub fn acos(self) -> Variable<'v>

Inverse cosine function. d/dx cos^-1(x) = - 1 / sqrt(1 - x^2)


let g = Graph::new();

let x1 = g.var(0.0);
let z1 = x1.acos();
let grad1 = z1.accumulate();
assert!((z1.value - 1.5707963267948966).abs() <= 1e-15);
assert!((grad1.wrt(&x1) - (-1.0)).abs() <= 1e-15);
Source

pub fn acosh(self) -> Variable<'v>

Inverse hyperbolic cosine function. d/dx cosh^-1(x) = 1 / ( sqrt(x-1) * sqrt(x+1) )


let g = Graph::new();

let x = g.var(5.0);
let z = x.acosh();
let grad = z.accumulate();
assert!((z.value - 2.2924316695611777).abs() <= 1e-15);
assert!((grad.wrt(&x) - 0.20412414523193150818).abs() <= 1e-15);
Source

pub fn asin(self) -> Variable<'v>

Inverse sine function. d/dx sin^-1(x) = 1 / sqrt(1 - x^2)


let g = Graph::new();

let x = g.var(0.0);
let z = x.asin();
let grad = z.accumulate();

//assert_eq!(z.value, std::f64::consts::PI / 2.0);
assert_eq!(z.value, 0.0);
assert_eq!(grad.wrt(&x), 1.0);
Source

pub fn asinh(self) -> Variable<'v>

Inverse hyperbolic sine function. d/dx sinh^-1(x) = 1 / sqrt(1 + x^2)


let g = Graph::new();

let x = g.var(0.0);
let z = x.asinh();
let grad = z.accumulate();

assert_eq!(z.value, 0.0);
assert_eq!(grad.wrt(&x), 1.0);
Source

pub fn atan(self) -> Variable<'v>

Inverse tangent function. d/dx tan^-1(x) = 1 / (1 + x^2)


let g = Graph::new();

let x = g.var(0.0);
let z = x.atan();
let grad = z.accumulate();

assert_eq!(z.value, 0.0);
assert_eq!(grad.wrt(&x), 1.0);
Source

pub fn atanh(self) -> Variable<'v>

Inverse hyperbolic tangent function. d/dx tanh^-1(x) = 1 / (1 + x^2)


let g = Graph::new();

let x = g.var(0.0);
let z = x.atanh();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.00000000000, 1e-10);
assert_approx_equal!(grad.wrt(&x), 1.00000000000, 1e-10);
Source

pub fn cbrt(self) -> Variable<'v>

Cuberoot function. d/dx cuberoot(x) = 1 / ( 3 * x^(2/3) )


let g = Graph::new();

let x = g.var(1.0);
let z = x.cbrt();
let grad = z.accumulate();

assert_approx_equal!(z.value,      1.00000000000, 1e-10);
assert_approx_equal!(grad.wrt(&x), 0.33333333333, 1e-10);
Source

pub fn cos(self) -> Variable<'v>

Cosine function. d/dx cos(x) = -sin(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.cos();
let grad = z.accumulate();

assert_approx_equal!(z.value,       0.54030230586, 1e-10);
assert_approx_equal!(grad.wrt(&x), -0.84147098480, 1e-10);
Source

pub fn cosh(self) -> Variable<'v>

Inverse hyperbolic cosine function. d/dx cosh(x) = sinh(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.cosh();
let grad = z.accumulate();

assert_approx_equal!(z.value,      1.54308063481, 1e-10);
assert_approx_equal!(grad.wrt(&x), 1.17520119364, 1e-10);
Source

pub fn exp(self) -> Variable<'v>

Exponential function (base e). d/dx exp(x) = exp(x) = y

  
let g = Graph::new();

let x = g.var(1.0);
let z = x.exp();
let grad = z.accumulate();

assert_eq!(z.value, std::f64::consts::E);
assert_eq!(grad.wrt(&x), std::f64::consts::E);
Source

pub fn exp2(self) -> Variable<'v>

Exponential function (base 2) d/dx 2^x = 2^x * ln(2)


let g = Graph::new();

let x = g.var(1.0);
let z = x.exp2();
let grad = z.accumulate();

assert_approx_equal!(z.value,      2.00000000000, 1e-10);
assert_approx_equal!(grad.wrt(&x), 1.38629436111, 1e-10);
Source

pub fn exp_m1(self) -> Variable<'v>

Exponential function minus 1 function. d/dx exp(x) - 1 = exp(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.exp_m1();
let grad = z.accumulate();

assert_approx_equal!(z.value,      1.71828182845, 1e-10);
assert_approx_equal!(grad.wrt(&x), 2.71828182845, 1e-10);
Source

pub fn ln(self) -> Variable<'v>

Logarithm (natural) of x. d/dx ln(x) = 1 / x

   
let g = Graph::new();

let x = g.var(std::f64::consts::E);
let z = x.ln();
let grad = z.accumulate();

assert_eq!(z.value, 1.0);
assert_eq!(grad.wrt(&x), 0.36787944117144233);
Source

pub fn ln_1p(self) -> Variable<'v>

Logarithm (natural) of 1 + x. d/dx ln(1+x) = 1 / (1+x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.ln_1p();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.69314718055, 1e-10);
assert_approx_equal!(grad.wrt(&x), 0.50000000000, 1e-10);
Source

pub fn log10(self) -> Variable<'v>

Logarithm (base 10). d/dx log_10(x) = 1 / x


let g = Graph::new();

let x = g.var(1.0);
let z = x.log10();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.00000000000, 1e-10);
assert_approx_equal!(grad.wrt(&x), 1.00000000000, 1e-10);
Source

pub fn log2(self) -> Variable<'v>

Logarithm (base 2). d/dx log_2(x) = 1 / x


let g = Graph::new();

let x = g.var(1.0);
let z = x.log2();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.00000000000, 1e-10);
assert_approx_equal!(grad.wrt(&x), 1.00000000000, 1e-10);
Source

pub fn recip(self) -> Variable<'v>

Reciprocal function. d/dx 1 / x = - 1 / x^2


let g = Graph::new();

let x = g.var(1.0);
let z = x.recip();
let grad = z.accumulate();

assert_eq!(z.value, 1.0);
assert_eq!(grad.wrt(&x), -1.0);
Source

pub fn sin(self) -> Variable<'v>

Sine function. d/dx sin(x) = cos(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.sin();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.84147098480, 1e-10);
assert_approx_equal!(grad.wrt(&x), 0.54030230586, 1e-10);
Source

pub fn sinh(self) -> Variable<'v>

Hyperbolic sine function. d/dx sinh(x) = cosh(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.sinh();
let grad = z.accumulate();

assert_approx_equal!(z.value,      1.17520119364, 1e-10);
assert_approx_equal!(grad.wrt(&x), 1.54308063481, 1e-10);
Source

pub fn sqrt(self) -> Variable<'v>

Square root function. d/dx sqrt(x) = 1 / 2*sqrt(x)


let g = Graph::new();

let x = g.var(2.0);
let z = x.sqrt();
let grad = z.accumulate();

assert_eq!(z.value, std::f64::consts::SQRT_2);
assert_eq!(grad.wrt(&x), 1.0 / (2.0 * std::f64::consts::SQRT_2));
Source

pub fn tan(self) -> Variable<'v>

Tangent function. d/dx tan(x) = 1 / cos^2(x) = sec^2(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.tan();
let grad = z.accumulate();

assert_approx_equal!(z.value,      1.55740772465, 1e-10);
assert_approx_equal!(grad.wrt(&x), 3.42551882081, 1e-10);
Source

pub fn tanh(self) -> Variable<'v>

Hyperbolic tangent function. d/dx tanh(x) = sech^2(x) = 1 / cosh^2(x)


let g = Graph::new();

let x = g.var(1.0);
let z = x.tanh();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.7615941559, 1e-10);
assert_approx_equal!(grad.wrt(&x), 0.4199743416, 1e-10);
Source§

impl<'v> Variable<'v>

Source

pub fn erf(self) -> Variable<'v>

Error function. d/dx erf(x) = 2e^(-x^2) / sqrt(PI)


let g = Graph::new();

let x = g.var(1.0);
let z = x.erf();
let grad = z.accumulate();

assert_approx_equal!(z.value,      0.84270079294, 1e-10);
assert_approx_equal!(grad.wrt(&x), 0.41510749742, 1e-10);
Source

pub fn erfc(self) -> Variable<'v>

Error function (complementary). d/dx erfc(x) = -2e^(-x^2) / sqrt(PI)


let g = Graph::new();

let x = g.var(1.0);
let z = x.erfc();
let grad = z.accumulate();

assert_approx_equal!(z.value,       0.15729920705, 1e-10);
assert_approx_equal!(grad.wrt(&x), -0.41510749742, 1e-10);
Source§

impl<'v> Variable<'v>

Source

pub const fn new(graph: &'v Graph, index: usize, value: f64) -> Variable<'v>

Instantiate a new variable.

Source

pub fn value(&self) -> f64

Function to return the value contained in a vertex.

Source

pub fn index(&self) -> usize

Function to return the index of a vertex.

Source

pub fn graph(&self) -> &'v Graph

Function to return the graph.

Source

pub fn is_finite(&self) -> bool

Check if variable is finite.

Source

pub fn is_infinite(&self) -> bool

Check if variable is infinite.

Source

pub fn is_nan(&self) -> bool

Check if variable is NaN.

Source

pub fn is_normal(&self) -> bool

Check if variable is normal.

Source

pub fn is_subnormal(&self) -> bool

Check if variable is subnormal.

Source

pub fn is_zero(&self) -> bool

Check if variable is zero.

Source

pub fn is_positive(&self) -> bool

Check if variable is positive.

Source

pub fn is_negative(&self) -> bool

Check if variable is negative.

Source

pub fn round(&mut self)

Round variable to nearest integer.

Source

pub fn signum(&self) -> f64

Returns the sign of the variable.

Trait Implementations§

Source§

impl Accumulate<Vec<f64>> for Variable<'_>

Source§

fn accumulate(&self) -> Vec<f64>

Function to reverse accumulate the gradient for a Variable.

  1. Allocate the array of adjoints.
  2. Set the seed (dx/dx = 1).
  3. Traverse the graph backwards, updating the adjoints for the parent vertices.
Source§

impl ActivationFunction for Variable<'_>

Source§

fn sigmoid(&self) -> Variable<'_>

Applies the sigmoid function to the input.
Source§

fn identity(&self) -> Variable<'_>

Applies the identity function to the input.
Source§

fn logistic(&self) -> Variable<'_>

Applies the logistic function to the input. Read more
Source§

fn relu(&self) -> Variable<'_>

Applies the rectified linear unit function to the input.
Source§

fn gelu(&self) -> Variable<'_>

Applies the gaussian error linear unit function to the input.
Source§

fn tanh(&self) -> Variable<'_>

Applies the hyperbolic tangent function to the input.
Source§

fn softplus(&self) -> Variable<'_>

Applies the softplus function to the input.
Source§

fn gaussian(&self) -> Variable<'_>

Applies the gaussian function to the input.
Source§

impl<'v> Add<f64> for Variable<'v>

Variable<’v> + f64

Source§

fn add(self, other: f64) -> <Variable<'v> as Add<f64>>::Output


let g = Graph::new();

let x = g.var(2.0);
let a = 5.0;
let z = x + a;

let grad = z.accumulate();

assert_eq!(z.value, 7.0);
assert_eq!(grad.wrt(&x), 1.0);
Source§

type Output = Variable<'v>

The resulting type after applying the + operator.
Source§

impl<'v> Add for Variable<'v>

Variable<’v> + Variable<’v>

Source§

fn add(self, other: Variable<'v>) -> <Variable<'v> as Add>::Output


let g = Graph::new();

let x = g.var(5.0);
let y = g.var(2.0);
let z = x + y;

let grad = z.accumulate();

assert_eq!(z.value, 7.0);
assert_eq!(grad.wrt(&x), 1.0);
assert_eq!(grad.wrt(&y), 1.0);
Source§

type Output = Variable<'v>

The resulting type after applying the + operator.
Source§

impl<'v> AddAssign<f64> for Variable<'v>

AddAssign: Variable<’v> += f64

Source§

fn add_assign(&mut self, other: f64)

Performs the += operation. Read more
Source§

impl<'v> AddAssign for Variable<'v>

Overload the standard addition operator (+). d/dx x + y = 1 d/dy x + y = 1 AddAssign: Variable<’v> += Variable<’v>

Source§

fn add_assign(&mut self, other: Variable<'v>)

Performs the += operation. Read more
Source§

impl<'v> Clone for Variable<'v>

Source§

fn clone(&self) -> Variable<'v>

Returns a copy 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<'v> Debug for Variable<'v>

Source§

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

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

impl<'v> Display for Variable<'v>

Implement formatting for the Variable struct.

Source§

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

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

impl<'v> Div<f64> for Variable<'v>

Variable<’v> / f64

Source§

fn div(self, other: f64) -> <Variable<'v> as Div<f64>>::Output


let g = Graph::new();

let x = g.var(5.0);
let a = 2.0;
let z = x / a;

let grad = z.accumulate();

assert_eq!(z.value, 5.0 / 2.0);
assert_eq!(grad.wrt(&x), 1.0 / 2.0);
Source§

type Output = Variable<'v>

The resulting type after applying the / operator.
Source§

impl<'v> Div for Variable<'v>

Variable<’v> / Variable<’v>

Source§

fn div(self, other: Variable<'v>) -> <Variable<'v> as Div>::Output

  
let g = Graph::new();

let x = g.var(5.0);
let y = g.var(2.0);
let z = x / y;

let grad = z.accumulate();

assert_eq!(z.value, 5.0 / 2.0);
assert_eq!(grad.wrt(&x), 1.0 / 2.0);
assert_eq!(grad.wrt(&y), - 5.0 / (2.0 * 2.0));
Source§

type Output = Variable<'v>

The resulting type after applying the / operator.
Source§

impl<'v> DivAssign<f64> for Variable<'v>

DivAssign: Variable<’v> /= f64

Source§

fn div_assign(&mut self, other: f64)

Performs the /= operation. Read more
Source§

impl<'v> DivAssign for Variable<'v>

Overload the standard division operator (/). d/dx x/y = 1/y d/dy x/y = -x/y^2 DivAssign: Variable<’v> /= Variable<’v>

Source§

fn div_assign(&mut self, other: Variable<'v>)

Performs the /= operation. Read more
Source§

impl<'v> Gradient<&Variable<'v>, f64> for Vec<f64>

wrt a single variable.

Source§

fn wrt(&self, variable: &Variable<'_>) -> f64

Returns the derivative/s with-respect-to the chosen variables.
Source§

impl<'v> Log<Variable<'v>> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Log
Source§

fn log(&self, base: Variable<'_>) -> <Variable<'v> as Log<Variable<'v>>>::Output

Overloaded log function.
Source§

impl<'v> Log<Variable<'v>> for f64

Source§

type Output = Variable<'v>

Return type of Log
Source§

fn log(&self, base: Variable<'v>) -> <f64 as Log<Variable<'v>>>::Output

Overloaded log function.
Source§

impl<'v> Log<f64> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Log
Source§

fn log(&self, base: f64) -> <Variable<'v> as Log<f64>>::Output

Overloaded log function.
Source§

impl<'v> Max<Variable<'v>> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Max
Source§

fn max(&self, rhs: Variable<'v>) -> <Variable<'v> as Max<Variable<'v>>>::Output

Overloaded max function.
Source§

impl<'v> Max<Variable<'v>> for f64

Source§

type Output = Variable<'v>

Return type of Max
Source§

fn max(&self, rhs: Variable<'v>) -> <f64 as Max<Variable<'v>>>::Output

Overloaded max function.
Source§

impl<'v> Max<f64> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Max
Source§

fn max(&self, rhs: f64) -> <Variable<'v> as Max<f64>>::Output

Overloaded max function.
Source§

impl<'v> Min<Variable<'v>> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Min
Source§

fn min(&self, rhs: Variable<'v>) -> <Variable<'v> as Min<Variable<'v>>>::Output

Overloaded min function.
Source§

impl<'v> Min<Variable<'v>> for f64

Source§

type Output = Variable<'v>

Return type of Min
Source§

fn min(&self, rhs: Variable<'v>) -> <f64 as Min<Variable<'v>>>::Output

Overloaded min function.
Source§

impl<'v> Min<f64> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Min
Source§

fn min(&self, rhs: f64) -> <Variable<'v> as Min<f64>>::Output

Overloaded min function.
Source§

impl<'v> Mul<f64> for Variable<'v>

Variable<’v> * f64

Source§

fn mul(self, other: f64) -> <Variable<'v> as Mul<f64>>::Output


let g = Graph::new();

let x = g.var(5.0);
let a = 2.0;
let z = x * a;

let grad = z.accumulate();

assert_eq!(z.value, 10.0);
assert_eq!(grad.wrt(&x), 2.0);
Source§

type Output = Variable<'v>

The resulting type after applying the * operator.
Source§

impl<'v> Mul for Variable<'v>

Variable<’v> * Variable<’v>

Source§

fn mul(self, other: Variable<'v>) -> <Variable<'v> as Mul>::Output


let g = Graph::new();

let x = g.var(5.0);
let y = g.var(2.0);
let z = x * y;

let grad = z.accumulate();

assert_eq!(z.value, 10.0);
assert_eq!(grad.wrt(&x), 2.0);
assert_eq!(grad.wrt(&y), 5.0);
Source§

type Output = Variable<'v>

The resulting type after applying the * operator.
Source§

impl<'v> MulAssign<f64> for Variable<'v>

MulAssign: Variable<’v> *= f64

Source§

fn mul_assign(&mut self, other: f64)

Performs the *= operation. Read more
Source§

impl<'v> MulAssign for Variable<'v>

Overload the standard multiplication operator (*). d/dx x * y = y d/dy x * y = x MulAssign: Variable<’v> *= Variable<’v>

Source§

fn mul_assign(&mut self, other: Variable<'v>)

Performs the *= operation. Read more
Source§

impl<'v> Neg for Variable<'v>

Source§

type Output = Variable<'v>

The resulting type after applying the - operator.
Source§

fn neg(self) -> <Variable<'v> as Neg>::Output

Performs the unary - operation. Read more
Source§

impl<'v> Ord for Variable<'v>

Source§

fn cmp(&self, other: &Variable<'v>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'v> PartialEq<f64> for Variable<'v>

Source§

fn eq(&self, other: &f64) -> 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<'v> PartialEq for Variable<'v>

Source§

fn eq(&self, other: &Variable<'v>) -> 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<'v> PartialOrd for Variable<'v>

Source§

fn partial_cmp(&self, other: &Variable<'v>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'v> Powf<Variable<'v>> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Powf
Source§

fn powf( &self, other: Variable<'v>, ) -> <Variable<'v> as Powf<Variable<'v>>>::Output

Overloaded powf function.
Source§

impl<'v> Powf<Variable<'v>> for f64

Source§

type Output = Variable<'v>

Return type of Powf
Source§

fn powf(&self, other: Variable<'v>) -> <f64 as Powf<Variable<'v>>>::Output

Overloaded powf function.
Source§

impl<'v> Powf<f64> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Powf
Source§

fn powf(&self, n: f64) -> <Variable<'v> as Powf<f64>>::Output

Overloaded powf function.
Source§

impl<'v> Powi<Variable<'v>> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Powi
Source§

fn powi( &self, other: Variable<'v>, ) -> <Variable<'v> as Powi<Variable<'v>>>::Output

Overloaded powi function.
Source§

impl<'v> Powi<Variable<'v>> for f64

Source§

type Output = Variable<'v>

Return type of Powi
Source§

fn powi(&self, other: Variable<'v>) -> <f64 as Powi<Variable<'v>>>::Output

Overloaded powi function.
Source§

impl<'v> Powi<i32> for Variable<'v>

Source§

type Output = Variable<'v>

Return type of Powi
Source§

fn powi(&self, n: i32) -> <Variable<'v> as Powi<i32>>::Output

Overloaded powi function.
Source§

impl<'v> Product for Variable<'v>

Source§

fn product<I>(iter: I) -> Variable<'v>
where I: Iterator<Item = Variable<'v>>,


let g = Graph::new();

let params = (1..=5).map(|x| g.var(x as f64)).collect::<Vec<_>>();

let prod = params.iter().copied().product::<Variable>();

let derivs = prod.accumulate();
let true_gradient = vec![120.0, 60.0, 40.0, 30.0, 24.0];

let n = derivs.wrt(&params).len();
let m = true_gradient.len();
assert_eq!(n, m);

for i in 0..n {
    assert_eq!(derivs.wrt(&params)[i], true_gradient[i]);
}
Source§

impl<'v> Sub<f64> for Variable<'v>

Variable<’v> - f64

Source§

fn sub(self, other: f64) -> <Variable<'v> as Sub<f64>>::Output


let g = Graph::new();

let x = g.var(5.0);
let a = 2.0;
let z = x - a;

let grad = z.accumulate();

assert_eq!(z.value, 3.0);
assert_eq!(grad.wrt(&x), 1.0);
Source§

type Output = Variable<'v>

The resulting type after applying the - operator.
Source§

impl<'v> Sub for Variable<'v>

Variable<’v> - Variable<’v>

Source§

fn sub(self, other: Variable<'v>) -> <Variable<'v> as Sub>::Output


let g = Graph::new();

let x = g.var(5.0);
let y = g.var(2.0);
let z = x - y;

let grad = z.accumulate();

assert_eq!(z.value, 3.0);
assert_eq!(grad.wrt(&x), 1.0);
assert_eq!(grad.wrt(&y), -1.0);
Source§

type Output = Variable<'v>

The resulting type after applying the - operator.
Source§

impl<'v> SubAssign<f64> for Variable<'v>

SubAssign: Variable<’v> -= f64

Source§

fn sub_assign(&mut self, other: f64)

Performs the -= operation. Read more
Source§

impl<'v> SubAssign for Variable<'v>

Overload the standard subtraction operator (-). d/dx x - y = 1 d/dy x - y = -1 SubAssign: Variable<’v> -= Variable<’v>

Source§

fn sub_assign(&mut self, other: Variable<'v>)

Performs the -= operation. Read more
Source§

impl<'v> Sum for Variable<'v>

Source§

fn sum<I>(iter: I) -> Variable<'v>
where I: Iterator<Item = Variable<'v>>,


let g = Graph::new();

let params = (0..100).map(|x| g.var(x as f64)).collect::<Vec<_>>();

let sum = params.iter().copied().sum::<Variable>();

let derivs = sum.accumulate();

for i in derivs.wrt(&params) {
    assert_eq!(i, 1.0);
}
Source§

impl<'v> Copy for Variable<'v>

Source§

impl<'v> Eq for Variable<'v>

Auto Trait Implementations§

§

impl<'v> Freeze for Variable<'v>

§

impl<'v> !RefUnwindSafe for Variable<'v>

§

impl<'v> !Send for Variable<'v>

§

impl<'v> !Sync for Variable<'v>

§

impl<'v> Unpin for Variable<'v>

§

impl<'v> !UnwindSafe for Variable<'v>

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToCompactString for T
where T: Display,

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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

Source§

impl<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

Source§

impl<T, Right> ClosedAddAssign<Right> for T
where T: ClosedAdd<Right> + AddAssign<Right>,

Source§

impl<T, Right> ClosedDiv<Right> for T
where T: Div<Right, Output = T> + DivAssign<Right>,

Source§

impl<T, Right> ClosedDiv<Right> for T
where T: Div<Right, Output = T> + DivAssign<Right>,

Source§

impl<T, Right> ClosedDivAssign<Right> for T
where T: ClosedDiv<Right> + DivAssign<Right>,

Source§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

Source§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

Source§

impl<T, Right> ClosedMulAssign<Right> for T
where T: ClosedMul<Right> + MulAssign<Right>,

Source§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

Source§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

Source§

impl<T, Right> ClosedSubAssign<Right> for T
where T: ClosedSub<Right> + SubAssign<Right>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<T> SendAlias for T

Source§

impl<T> SyncAlias for T