pub struct Trace<T: Primitive> {
    pub number: T,
    pub derivative: T,
}
Expand description

A dual number which traces a real number and keeps track of its derivative. This is used to perform Forward Automatic Differentiation

Trace implements only first order differentiation. For example, given a function 3x2, you can use calculus to work out that its derivative with respect to x is 6x. You can also take the derivative of 6x with respect to x and work out that the second derivative is 6. By instead writing the function 3x2 in code using Trace types as your numbers you can compute the first order derivative for a given value of x by passing your function Trace { number: x, derivative: 1.0 }.

use easy_ml::differentiation::Trace;
let x = Trace { number: 3.2, derivative: 1.0 };
let dx = Trace::constant(3.0) * x * x;
assert_eq!(dx.derivative, 3.2 * 6.0);

Why the one for the starting derivative? Because δx/δx = 1, as with symbolic differentiation.

Acknowledgments

The wikipedia page on Automatic Differentiation provided a very useful overview and explanation for understanding Forward Mode Automatic Differentiation as well as the implementation rules.

Fields

number: T

The real number

derivative: T

The first order derivative of this number.

Implementations

The main set of methods for using Trace types for Forward Differentiation.

The general steps are

  1. create one variable
  2. create as many constants as needed
  3. do operations on the variable and constants
  4. the outputs will have derivatives computed which can be accessed from the .derivative field, with each derivative being the output with respect to the input variable.
  5. if you need derivatives for a different input then do everything all over again or do them all in parallel

Constants are lifted to Traces with a derivative of 0

Why zero for the starting derivative? Because for any constant C δC/δx = 0, as with symbolic differentiation.

To lift a variable that you want to find the derivative of a function to, the Trace starts with a derivative of 1

Why the one for the starting derivative? Because δx/δx = 1, as with symbolic differentiation.

Computes the derivative of a function with respect to its input x.

This is a shorthand for (function(Trace::variable(x))).derivative

In the more general case, if you provide a function with an input x and it returns N outputs y1 to yN then you have computed all the derivatives δyi/δx for i = 1 to N.

Creates a new Trace from a reference to an existing Trace by applying some unary function to it which operates on the type the Trace wraps.

To compute the new trace, the unary function of some input x to some output y is needed along with its derivative with respect to its input x.

For example, tanh is a commonly used activation function, but the Real trait does not include this operation and Trace has no operations for it specifically. However, you can use this function to compute the tanh of a Trace like so:

use easy_ml::differentiation::Trace;
let x = Trace::variable(0.7f32);
// the derivative of tanh(x) is sech(x) * sech(x) which is equivalent to
// 1 / (cosh(x) * cosh(x))
let y = x.unary(|x| x.tanh(), |x| 1.0 / (x.cosh() * x.cosh()));
assert_eq!(y.derivative, 1.0f32 / (0.7f32.cosh() * 0.7f32.cosh()));

Creates a new Trace from a reference to two existing Traces by applying some binary function to them which operates on two arguments of the type the Traces wrap.

To compute the new trace, the binary function of some inputs x and y to some output z is needed along with its derivative with respect to its first input x and its derivative with respect to its second input y.

For example, atan2 takes two arguments, but the Real trait does not include this operation and Trace has no operations for it specifically. However, you can use this function to compute the atan2 of two Traces like so:

use easy_ml::differentiation::Trace;
let x = Trace::variable(3.0f32);
let y = Trace::variable(3.0f32);
// the derivative of atan2 with respect to x is y/(x*x + y*y)
// https://www.wolframalpha.com/input/?i=d%28atan2%28x%2Cy%29%29%2Fdx
// the derivative of atan2 with respect to y is -x/(x*x + y*y)
// https://www.wolframalpha.com/input/?i=d%28atan2%28x%2Cy%29%29%2Fdy
let z = x.binary(&y,
    |x, y| x.atan2(y),
    |x, y| y/((x*x) + (y*y)),
    |x, y| -x/((x*x) + (y*y))
);

Trait Implementations

Addition for a trace and a constant of the same type with both referenced.

The resulting type after applying the + operator.

Performs the + operation. Read more

Operation for a trace and a constant of the same type with the right referenced.

The resulting type after applying the + operator.

Performs the + operation. Read more

Operation for two traces of the same type with the right referenced.

The resulting type after applying the + operator.

Performs the + operation. Read more

Addition for two traces of the same type with both referenced.

The resulting type after applying the + operator.

Performs the + operation. Read more

Operation for a trace and a constant of the same type.

The resulting type after applying the + operator.

Performs the + operation. Read more

Operation for a trace and a constant of the same type with the left referenced.

The resulting type after applying the + operator.

Performs the + operation. Read more

Operation for two traces of the same type.

The resulting type after applying the + operator.

Performs the + operation. Read more

Operation for two traces of the same type with the left referenced.

The resulting type after applying the + operator.

Performs the + operation. Read more

Any trace of a Cloneable type implements clone

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Cosine of a Trace by reference.

Operation for a trace by value.

Formats the value using the given formatter. Read more

A trace is displayed by showing its number component.

Formats the value using the given formatter. Read more

Dvision for a trace and a constant of the same type with both referenced.

The resulting type after applying the / operator.

Performs the / operation. Read more

Operation for a trace and a constant of the same type with the right referenced.

The resulting type after applying the / operator.

Performs the / operation. Read more

Operation for two traces of the same type with the right referenced.

The resulting type after applying the / operator.

Performs the / operation. Read more

Division for two referenced traces of the same type.

The resulting type after applying the / operator.

Performs the / operation. Read more

Operation for a trace and a constant of the same type.

The resulting type after applying the / operator.

Performs the / operation. Read more

Operation for a trace and a constant of the same type with the left referenced.

The resulting type after applying the / operator.

Performs the / operation. Read more

Operation for two traces of the same type.

The resulting type after applying the / operator.

Performs the / operation. Read more

Operation for two traces of the same type with the left referenced.

The resulting type after applying the / operator.

Performs the / operation. Read more

Exponential, ie ex of a Trace by reference.

Operation for a trace by value.

Natural logarithm, ie ln(x) of a Trace by reference.

Operation for a trace by value.

Multiplication for a trace and a constant of the same type with both referenced.

The resulting type after applying the * operator.

Performs the * operation. Read more

Operation for a trace and a constant of the same type with the right referenced.

The resulting type after applying the * operator.

Performs the * operation. Read more

Operation for two traces of the same type with the right referenced.

The resulting type after applying the * operator.

Performs the * operation. Read more

Multiplication for two referenced traces of the same type.

The resulting type after applying the * operator.

Performs the * operation. Read more

Operation for a trace and a constant of the same type.

The resulting type after applying the * operator.

Performs the * operation. Read more

Operation for a trace and a constant of the same type with the left referenced.

The resulting type after applying the * operator.

Performs the * operation. Read more

Operation for two traces of the same type.

The resulting type after applying the * operator.

Performs the * operation. Read more

Operation for two traces of the same type with the left referenced.

The resulting type after applying the * operator.

Performs the * operation. Read more

Negation for a referenced Trace of some type.

The resulting type after applying the - operator.

Performs the unary - operation. Read more

Negation for a Trace by value of some type.

The resulting type after applying the - operator.

Performs the unary - operation. Read more

Any trace of a PartialEq type implements PartialEq

Note that as a Trace is intended to be substitutable with its type T only the number parts of the trace are compared. Hence the following is true

use easy_ml::differentiation::Trace;
assert_eq!(Trace { number: 0, derivative: 1 }, Trace { number: 0, derivative: 2 })

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Any trace of a PartialOrd type implements PartialOrd

Note that as a Trace is intended to be substitutable with its type T only the number parts of the trace are compared. Hence the following is true

use easy_ml::differentiation::Trace;
assert!(Trace { number: 1, derivative: 1 } > Trace { number: 0, derivative: 2 })

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Power of a trace to a constant of the same type with both referenced.

Operation for a trace and a constant of the same type with the right referenced.

Operation for two traces of the same type with the right referenced.

Power of a constant to a trace of the same type with both referenced.

Operation for a trace and a constant of the same type with the right referenced.

Power of one Trace to another, ie self^rhs for two traces of the same type with both referenced.

Operation for a trace and a constant of the same type.

Operation for a trace and a constant of the same type with the left referenced.

Operation for two traces of the same type.

Operation for two traces of the same type with the left referenced.

Operation for a trace and a constant of the same type.

Operation for a trace and a constant of the same type with the left referenced.

Sine of a Trace by reference.

Operation for a trace by value.

Square root of a Trace by reference.

Operation for a trace by value.

Subtraction for a trace and a constant of the same type with both referenced.

The resulting type after applying the - operator.

Performs the - operation. Read more

Operation for a trace and a constant of the same type with the right referenced.

The resulting type after applying the - operator.

Performs the - operation. Read more

Operation for two traces of the same type with the right referenced.

The resulting type after applying the - operator.

Performs the - operation. Read more

Subtraction for two referenced traces of the same type.

The resulting type after applying the - operator.

Performs the - operation. Read more

Operation for a trace and a constant of the same type.

The resulting type after applying the - operator.

Performs the - operation. Read more

Operation for a trace and a constant of the same type with the left referenced.

The resulting type after applying the - operator.

Performs the - operation. Read more

Operation for two traces of the same type.

The resulting type after applying the - operator.

Performs the - operation. Read more

Operation for two traces of the same type with the left referenced.

The resulting type after applying the - operator.

Performs the - operation. Read more

Any trace of a Numeric type implements Sum, which is the same as adding a bunch of Trace types together.

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Any trace of a Copy type implements Copy

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.