Gradient Tape
Gradient tapes provide support for performing reverse-mode, automatic differentiation.
Overview
To use gradient tapes, first add the following using directives:
using Mathematics.NET.Core;
using Mathematics.NET.AutoDiff;
To create a gradient tape, write
GradientTape<Complex> tape = new();
Implements
- Name
ITape<T>- Type
- interface
- Description
Defines support for tapes.
Constraints
- Name
T- Type
- generic type constraint
- Description
A type that implements
IComplex<T>andIDifferentiableFunctions<T>.
GradientTape · 1
Create an instance of a gradient tape.
Optional Parameters
- Name
isTracking- Type
- bool
- Description
Whether or not the tape should be tracking nodes. This can be changed with the
IsTrackingproperty.
Snippet
// No optional parameters.
GradientTape<Complex> tape = new();
// With optional parameters.
GradientTape<Complex> tape = new(true);
GradientTape · 2
Create an instance of a gradient tape that will hold an expected number of nodes.
Required Parameters
- Name
n- Type
- int
- Description
The initial capacity of the gradient tape.
Optional Parameters
- Name
isTracking- Type
- bool
- Description
Whether or not the tape should be tracking nodes. This can be changed with the
IsTrackingproperty.
Snippet
// No optional parameters.
GradientTape<Complex> tape = new(64);
// With optional parameters.
GradientTape<Complex> tape = new(64, true);
CreateCheckpoint
Create a checkpoint on a gradient tape. During reverse accumulation, once a node with a checkpoint is reached, further computation along its branches will be skipped, and its precomputed value be used instead.
Required Parameters
- Name
x- Type
- Variable<T>
- Description
A variable type used in reverse-mode, autodiff.
Return Type
- Name
x- Type
- Variable<T>
- Description
A variable type used in reverse-mode, autodiff.
Snippet
GradientTape<Complex> tape = new();
var x = tape.CreateVariable(1.23);
var y = tape.CreateVariable(4.56);
var checkpoint = tape.CreateCheckpoint(tape.Multiply(x, y));
Result
checkpoint