MicrogradEx.Gradients (MicrogradEx v0.1.0)

Copy Markdown View Source

The immutable result of a reverse-mode automatic differentiation pass.

Python micrograd writes gradients into mutable Value.grad fields. In Elixir, the more natural shape is to return the gradients as data:

gradients = MicrogradEx.Value.backward(loss)
dloss_dw = MicrogradEx.Gradients.get(gradients, weight)

The table is keyed by the stable ids of the Value nodes that participated in the forward expression.

Summary

Functions

Builds the gradient table for an output value.

Fetches the gradient for a value or node id.

Returns the raw gradient map.

Returns graph node ids in forward topological order.

Types

t()

@type t() :: %MicrogradEx.Gradients{
  output_id: pos_integer(),
  values: %{required(pos_integer()) => float()}
}

Functions

backward(output)

Builds the gradient table for an output value.

The output starts with gradient 1.0 because d(output)/d(output) = 1. Then each node sends that gradient to its parents through the local derivative edges recorded during the forward pass.

get(gradients, id)

Fetches the gradient for a value or node id.

Missing entries return 0.0, which is the correct derivative for values that are independent of the chosen output.

to_map(gradients)

Returns the raw gradient map.

This is mostly useful for assertions, debugging, or building custom optimizers.

topological_ids(output)

Returns graph node ids in forward topological order.

Leaves appear before the output. This order is educational and useful for debugging; backward/1 internally uses the reverse order so gradients flow from outputs back to inputs.