pub struct ReverseModeEngine<T: ReverseScalar> { /* private fields */ }Expand description
Reverse-mode AD engine (gradient tape)
Implementations§
Source§impl<T: ReverseScalar> ReverseModeEngine<T>
impl<T: ReverseScalar> ReverseModeEngine<T>
Sourcepub fn set_recording(&mut self, recording: bool)
pub fn set_recording(&mut self, recording: bool)
Enable/disable gradient recording.
While recording is disabled, subsequent operations are still evaluated and appended to the tape, but they are recorded as constants: they keep the tape’s index invariant intact while blocking gradient flow.
Sourcepub fn is_recording(&self) -> bool
pub fn is_recording(&self) -> bool
Check if recording is enabled
Sourcepub fn enable_higher_order(&mut self, enabled: bool)
pub fn enable_higher_order(&mut self, enabled: bool)
Enable higher-order gradients
Sourcepub fn higher_order_enabled(&self) -> bool
pub fn higher_order_enabled(&self) -> bool
Whether higher-order gradient tracking is enabled
Sourcepub fn clear_tape(&mut self)
pub fn clear_tape(&mut self)
Clear the computation tape
Sourcepub fn create_variable(
&mut self,
name: &str,
value: Array1<T>,
requiresgrad: bool,
) -> usize
pub fn create_variable( &mut self, name: &str, value: Array1<T>, requiresgrad: bool, ) -> usize
Create a variable with gradient tracking (1-D, shape [value.len()])
Sourcepub fn create_variable_with_shape(
&mut self,
name: &str,
value: Array1<T>,
shape: &[usize],
requiresgrad: bool,
) -> Result<usize>
pub fn create_variable_with_shape( &mut self, name: &str, value: Array1<T>, shape: &[usize], requiresgrad: bool, ) -> Result<usize>
Create a variable with an explicit logical shape.
value is the row-major flattening of a tensor whose logical shape is
shape; the product of shape must equal value.len().
Sourcepub fn create_constant(&mut self, value: Array1<T>) -> usize
pub fn create_constant(&mut self, value: Array1<T>) -> usize
Create a constant (no gradient tracking)
Sourcepub fn set_variable_value(
&mut self,
varid: usize,
value: Array1<T>,
) -> Result<()>
pub fn set_variable_value( &mut self, varid: usize, value: Array1<T>, ) -> Result<()>
Replace the value of an existing leaf variable.
The tape is marked dirty; ReverseModeEngine::forward (called
automatically by ReverseModeEngine::backward) re-evaluates every
dependent node.
Sourcepub fn set_variable_value_by_name(
&mut self,
name: &str,
value: Array1<T>,
) -> Result<()>
pub fn set_variable_value_by_name( &mut self, name: &str, value: Array1<T>, ) -> Result<()>
Replace the value of an existing leaf variable, by name
Sourcepub fn multiply(&mut self, lhs: usize, rhs: usize) -> Result<usize>
pub fn multiply(&mut self, lhs: usize, rhs: usize) -> Result<usize>
Element-wise multiplication operation
Sourcepub fn divide(&mut self, lhs: usize, rhs: usize) -> Result<usize>
pub fn divide(&mut self, lhs: usize, rhs: usize) -> Result<usize>
Element-wise division operation
Sourcepub fn matmul(&mut self, lhs: usize, rhs: usize) -> Result<usize>
pub fn matmul(&mut self, lhs: usize, rhs: usize) -> Result<usize>
Matrix multiplication.
lhs must have logical shape [m, k]; rhs must have shape [k, n]
or [k] (interpreted as a [k, 1] column). The result has shape
[m, n] (or [m] when rhs was 1-D).
Sourcepub fn dot(&mut self, lhs: usize, rhs: usize) -> Result<usize>
pub fn dot(&mut self, lhs: usize, rhs: usize) -> Result<usize>
Dot product of two vectors of equal length; produces a scalar node.
Sourcepub fn sum(&mut self, input: usize, _axis: Option<usize>) -> Result<usize>
pub fn sum(&mut self, input: usize, _axis: Option<usize>) -> Result<usize>
Sum reduction over all elements
Sourcepub fn mean(&mut self, input: usize, _axis: Option<usize>) -> Result<usize>
pub fn mean(&mut self, input: usize, _axis: Option<usize>) -> Result<usize>
Mean reduction over all elements
Sourcepub fn reshape(&mut self, input: usize, newshape: &[usize]) -> Result<usize>
pub fn reshape(&mut self, input: usize, newshape: &[usize]) -> Result<usize>
Reshape operation (element count must be preserved)
Sourcepub fn forward(&mut self) -> Result<()>
pub fn forward(&mut self) -> Result<()>
Re-evaluate every non-leaf node of the tape from the current leaf values.
Nodes are visited in tape order, which is a valid topological order because every node’s index exceeds all of its inputs’ indices.
Sourcepub fn value_by_name(&self, name: &str) -> Result<&Array1<T>>
pub fn value_by_name(&self, name: &str) -> Result<&Array1<T>>
Forward value of a node, by variable name
Sourcepub fn scalar_value(&self, varid: usize) -> Result<T>
pub fn scalar_value(&self, varid: usize) -> Result<T>
Scalar value of a node that holds exactly one element
Sourcepub fn backward(
&mut self,
outputid: usize,
gradient: Option<Array1<T>>,
) -> Result<()>
pub fn backward( &mut self, outputid: usize, gradient: Option<Array1<T>>, ) -> Result<()>
Backward pass: compute gradients of outputid w.r.t. every leaf.
If gradient is None, the output is seeded with ones matching the
output’s length.
Sourcepub fn get_gradient(&self, varid: usize) -> Option<&Array1<T>>
pub fn get_gradient(&self, varid: usize) -> Option<&Array1<T>>
Get gradient for a variable
Sourcepub fn get_gradient_by_name(&self, name: &str) -> Option<&Array1<T>>
pub fn get_gradient_by_name(&self, name: &str) -> Option<&Array1<T>>
Get gradient by variable name
Sourcepub fn zero_gradients(&mut self)
pub fn zero_gradients(&mut self)
Zero all gradients
Sourcepub fn get_all_gradients(&self) -> HashMap<String, Array1<T>>
pub fn get_all_gradients(&self) -> HashMap<String, Array1<T>>
Get all named variable gradients
Sourcepub fn get_tape_stats(&self) -> ReverseModeStats
pub fn get_tape_stats(&self) -> ReverseModeStats
Get tape statistics
Trait Implementations§
Source§impl<T: ReverseScalar> Default for ReverseModeEngine<T>
impl<T: ReverseScalar> Default for ReverseModeEngine<T>
Auto Trait Implementations§
impl<T> Freeze for ReverseModeEngine<T>
impl<T> RefUnwindSafe for ReverseModeEngine<T>
impl<T> Send for ReverseModeEngine<T>
impl<T> Sync for ReverseModeEngine<T>
impl<T> Unpin for ReverseModeEngine<T>
impl<T> UnsafeUnpin for ReverseModeEngine<T>
impl<T> UnwindSafe for ReverseModeEngine<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.