|
micrograd 0.1.0
Small C implementation of micrograd
|
Scalar values and automatic differentiation graph operations. More...
Data Structures | |
| struct | mg_graph_checkpoint |
| Snapshot of a graph's allocation state. More... | |
Typedefs | |
| typedef struct mg_graph | mg_graph |
Opaque owner of a linked list of mg_value nodes. | |
| typedef struct mg_value | mg_value |
| Opaque scalar node in a computation graph. | |
Functions | |
| mg_graph * | mg_graph_new (void) |
| Create a new graph. | |
| void | mg_graph_free (mg_graph *g) |
| Free a graph and all values it owns. | |
| void | mg_zero_grad (mg_graph *g) |
Set all mg_value gradients in a graph to zero. | |
| mg_graph_checkpoint | mg_graph_save (const mg_graph *g) |
| Create a snapshot of the graph's current allocation state. | |
| void | mg_graph_restore (mg_graph *g, mg_graph_checkpoint checkpoint) |
| Restore the graph to a previous checkpoint. | |
| float | mg_data (const mg_value *v) |
| Get the scalar data of a value. | |
| float | mg_grad (const mg_value *v) |
| Get the gradient of a value. | |
| void | mg_set_data (mg_value *v, float data) |
| Set the scalar data of a value. | |
| void | mg_set_grad (mg_value *v, float grad) |
| Set the gradient of a value. | |
| mg_value * | mg_scalar (mg_graph *g, float data) |
| Create a new scalar value. | |
| mg_value * | mg_add (mg_graph *g, mg_value *a, mg_value *b) |
| Add two values. | |
| mg_value * | mg_sub (mg_graph *g, mg_value *a, mg_value *b) |
| Subtract one value from another. | |
| mg_value * | mg_mul (mg_graph *g, mg_value *a, mg_value *b) |
| Multiply two values. | |
| mg_value * | mg_div (mg_graph *g, mg_value *a, mg_value *b) |
| Divide one value by another. | |
| mg_value * | mg_pow (mg_graph *g, mg_value *a, mg_value *b) |
| Raise one value to the power of another. | |
| mg_value * | mg_neg (mg_graph *g, mg_value *a) |
| Negate a value. | |
| mg_value * | mg_square (mg_graph *g, mg_value *a) |
| Square a value. | |
| mg_value * | mg_relu (mg_graph *g, mg_value *a) |
| Apply the ReLU activation function to a value. | |
| mg_value * | mg_tanh (mg_graph *g, mg_value *a) |
| Apply the tanh activation function to a value. | |
| mg_value * | mg_exp (mg_graph *g, mg_value *a) |
| Apply the exponential function to a value. | |
| bool | mg_backward (mg_graph *g, mg_value *out) |
| Compute gradients for all values that contribute to an output value. | |
Scalar values and automatic differentiation graph operations.
Opaque owner of a linked list of mg_value nodes.
A graph frees all values it owns when passed to mg_graph_free.
Opaque scalar node in a computation graph.
A value stores scalar data, its gradient, and the operation and parent values that produced it.
Add two values.
| g | Graph that owns the result. |
| a | Left operand. |
| b | Right operand. |
NULL if allocation fails. Compute gradients for all values that contribute to an output value.
| g | Graph containing the values. |
| out | Output value to differentiate from. |
true on success, false if allocation fails. | float mg_data | ( | const mg_value * | v | ) |
Get the scalar data of a value.
| v | Value to inspect. |
v. Divide one value by another.
| g | Graph that owns the result. |
| a | Dividend. |
| b | Divisor. |
NULL if allocation fails. Apply the exponential function to a value.
| g | Graph that owns the result. |
| a | Input value. |
NULL if allocation fails. | float mg_grad | ( | const mg_value * | v | ) |
Get the gradient of a value.
| v | Value to inspect. |
v. | void mg_graph_free | ( | mg_graph * | g | ) |
Free a graph and all values it owns.
| g | Graph to free. Passing NULL is allowed. |
| mg_graph * mg_graph_new | ( | void | ) |
Create a new graph.
NULL if allocation fails. | void mg_graph_restore | ( | mg_graph * | g, |
| mg_graph_checkpoint | checkpoint | ||
| ) |
Restore the graph to a previous checkpoint.
| g | Graph to restore. |
| checkpoint | Checkpoint returned by mg_graph_save. |
| mg_graph_checkpoint mg_graph_save | ( | const mg_graph * | g | ) |
Create a snapshot of the graph's current allocation state.
| g | Graph to snapshot. |
mg_graph_restore. Multiply two values.
| g | Graph that owns the result. |
| a | Left operand. |
| b | Right operand. |
NULL if allocation fails. Negate a value.
| g | Graph that owns the result. |
| a | Value to negate. |
NULL if allocation fails. Raise one value to the power of another.
| g | Graph that owns the result. |
| a | Base. |
| b | Exponent. |
NULL if allocation fails. Apply the ReLU activation function to a value.
| g | Graph that owns the result. |
| a | Input value. |
NULL if allocation fails. Create a new scalar value.
| g | Graph that owns the new value. |
| data | Initial scalar data. |
NULL if allocation fails. | void mg_set_data | ( | mg_value * | v, |
| float | data | ||
| ) |
Set the scalar data of a value.
| v | Value to update. |
| data | New scalar data. |
| void mg_set_grad | ( | mg_value * | v, |
| float | grad | ||
| ) |
Set the gradient of a value.
| v | Value to update. |
| grad | New gradient. |
Square a value.
| g | Graph that owns the result. |
| a | Value to square. |
NULL if allocation fails. Subtract one value from another.
| g | Graph that owns the result. |
| a | Minuend. |
| b | Subtrahend. |
NULL if allocation fails. Apply the tanh activation function to a value.
| g | Graph that owns the result. |
| a | Input value. |
NULL if allocation fails. | void mg_zero_grad | ( | mg_graph * | g | ) |
Set all mg_value gradients in a graph to zero.
| g | Graph whose values should be reset. |