MicrogradEx (MicrogradEx v0.1.0)

Copy Markdown View Source

Elixir-native micrograd: a tiny scalar reverse-mode automatic differentiation engine plus the small neural-network library from the original project.

The original Python micrograd stores data and grad on mutable objects. Elixir data is immutable, so this port keeps the forward graph in each MicrogradEx.Value and returns a separate MicrogradEx.Gradients table from backward/1. That one design difference is the main FP adaptation:

  • forward expressions create new values;
  • backward/1 creates a new gradient table;
  • neural-network training creates a new updated model.

Example

iex> x = MicrogradEx.value(3.0)
iex> y = MicrogradEx.pow(x, 2)
iex> gradients = MicrogradEx.backward(y)
iex> MicrogradEx.grad(x, gradients)
6.0

Summary

Functions

Adds two values or numbers.

Runs reverse-mode automatic differentiation from an output value.

Divides the first value or number by the second.

Looks up the gradient for a value in a gradient table.

Multiplies two values or numbers.

Raises a value or number to a scalar power.

Applies the rectified linear unit activation.

Subtracts the second value or number from the first.

Creates a scalar differentiable value.

Functions

add(left, right)

Adds two values or numbers.

Numbers are automatically promoted to leaf Value structs. This mirrors the Python implementation's other = Value(other) coercion, but it stays explicit and regular because Elixir has no operator overloading.

backward(output)

Runs reverse-mode automatic differentiation from an output value.

divide(left, right)

Divides the first value or number by the second.

grad(value, gradients)

Looks up the gradient for a value in a gradient table.

mul(left, right)

Multiplies two values or numbers.

pow(value, exponent)

Raises a value or number to a scalar power.

relu(value)

Applies the rectified linear unit activation.

sub(left, right)

Subtracts the second value or number from the first.

value(data, opts \\ [])

Creates a scalar differentiable value.

This is a convenience wrapper around MicrogradEx.Value.new/2; use the Value module directly when writing longer expressions.