pub struct AutogradGraph {
pub nodes: HashMap<NodeId, AutogradNode>,
/* private fields */
}Expand description
Reverse-mode automatic differentiation graph.
Build a computational graph by calling AutogradGraph::input,
AutogradGraph::add, AutogradGraph::mul, etc., then call
AutogradGraph::backward on the scalar output node to populate
.grad on every node that has requires_grad = true.
Fields§
§nodes: HashMap<NodeId, AutogradNode>All nodes keyed by their NodeId.
Implementations§
Source§impl AutogradGraph
impl AutogradGraph
Sourcepub fn input(&mut self, value: f64) -> NodeId
pub fn input(&mut self, value: f64) -> NodeId
Create a leaf input node with requires_grad = true.
Sourcepub fn constant(&mut self, value: f64) -> NodeId
pub fn constant(&mut self, value: f64) -> NodeId
Create a constant leaf node with requires_grad = false.
Gradients do not flow through constants.
Sourcepub fn pow(&mut self, base: NodeId, exponent: f64) -> NodeId
pub fn pow(&mut self, base: NodeId, exponent: f64) -> NodeId
z = base^exponent where exponent is a scalar constant.
Sourcepub fn backward(&mut self, output_id: NodeId)
pub fn backward(&mut self, output_id: NodeId)
Run reverse-mode backpropagation starting from output_id.
Sets nodes[output_id].grad = 1.0, performs a topological sort
(reverse post-order DFS), and propagates gradients backwards through
the graph. Only nodes with requires_grad = true accumulate gradients.
Sourcepub fn grad(&self, node_id: NodeId) -> Option<f64>
pub fn grad(&self, node_id: NodeId) -> Option<f64>
Return the gradient of node_id if it exists and has
requires_grad = true.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AutogradGraph
impl RefUnwindSafe for AutogradGraph
impl Send for AutogradGraph
impl Sync for AutogradGraph
impl Unpin for AutogradGraph
impl UnsafeUnpin for AutogradGraph
impl UnwindSafe for AutogradGraph
Blanket Implementations§
impl<T> Allocation for T
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 more