micrograd 0.1.0
Small C implementation of micrograd
Loading...
Searching...
No Matches
Value API

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_graphmg_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_valuemg_scalar (mg_graph *g, float data)
 Create a new scalar value.
 
mg_valuemg_add (mg_graph *g, mg_value *a, mg_value *b)
 Add two values.
 
mg_valuemg_sub (mg_graph *g, mg_value *a, mg_value *b)
 Subtract one value from another.
 
mg_valuemg_mul (mg_graph *g, mg_value *a, mg_value *b)
 Multiply two values.
 
mg_valuemg_div (mg_graph *g, mg_value *a, mg_value *b)
 Divide one value by another.
 
mg_valuemg_pow (mg_graph *g, mg_value *a, mg_value *b)
 Raise one value to the power of another.
 
mg_valuemg_neg (mg_graph *g, mg_value *a)
 Negate a value.
 
mg_valuemg_square (mg_graph *g, mg_value *a)
 Square a value.
 
mg_valuemg_relu (mg_graph *g, mg_value *a)
 Apply the ReLU activation function to a value.
 
mg_valuemg_tanh (mg_graph *g, mg_value *a)
 Apply the tanh activation function to a value.
 
mg_valuemg_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.
 

Detailed Description

Scalar values and automatic differentiation graph operations.

Typedef Documentation

◆ mg_graph

typedef struct mg_graph mg_graph

Opaque owner of a linked list of mg_value nodes.

A graph frees all values it owns when passed to mg_graph_free.

◆ mg_value

typedef struct mg_value mg_value

Opaque scalar node in a computation graph.

A value stores scalar data, its gradient, and the operation and parent values that produced it.

Function Documentation

◆ mg_add()

mg_value * mg_add ( mg_graph g,
mg_value a,
mg_value b 
)

Add two values.

Parameters
gGraph that owns the result.
aLeft operand.
bRight operand.
Returns
The sum, or NULL if allocation fails.

◆ mg_backward()

bool mg_backward ( mg_graph g,
mg_value out 
)

Compute gradients for all values that contribute to an output value.

Parameters
gGraph containing the values.
outOutput value to differentiate from.
Returns
true on success, false if allocation fails.

◆ mg_data()

float mg_data ( const mg_value v)

Get the scalar data of a value.

Parameters
vValue to inspect.
Returns
The scalar data stored in v.

◆ mg_div()

mg_value * mg_div ( mg_graph g,
mg_value a,
mg_value b 
)

Divide one value by another.

Parameters
gGraph that owns the result.
aDividend.
bDivisor.
Returns
The quotient, or NULL if allocation fails.

◆ mg_exp()

mg_value * mg_exp ( mg_graph g,
mg_value a 
)

Apply the exponential function to a value.

Parameters
gGraph that owns the result.
aInput value.
Returns
The exponential value, or NULL if allocation fails.

◆ mg_grad()

float mg_grad ( const mg_value v)

Get the gradient of a value.

Parameters
vValue to inspect.
Returns
The gradient stored in v.

◆ mg_graph_free()

void mg_graph_free ( mg_graph g)

Free a graph and all values it owns.

Parameters
gGraph to free. Passing NULL is allowed.

◆ mg_graph_new()

mg_graph * mg_graph_new ( void  )

Create a new graph.

Returns
A new graph, or NULL if allocation fails.

◆ mg_graph_restore()

void mg_graph_restore ( mg_graph g,
mg_graph_checkpoint  checkpoint 
)

Restore the graph to a previous checkpoint.

Parameters
gGraph to restore.
checkpointCheckpoint returned by mg_graph_save.

◆ mg_graph_save()

mg_graph_checkpoint mg_graph_save ( const mg_graph g)

Create a snapshot of the graph's current allocation state.

Parameters
gGraph to snapshot.
Returns
A checkpoint that can be passed to mg_graph_restore.

◆ mg_mul()

mg_value * mg_mul ( mg_graph g,
mg_value a,
mg_value b 
)

Multiply two values.

Parameters
gGraph that owns the result.
aLeft operand.
bRight operand.
Returns
The product, or NULL if allocation fails.

◆ mg_neg()

mg_value * mg_neg ( mg_graph g,
mg_value a 
)

Negate a value.

Parameters
gGraph that owns the result.
aValue to negate.
Returns
The negated value, or NULL if allocation fails.

◆ mg_pow()

mg_value * mg_pow ( mg_graph g,
mg_value a,
mg_value b 
)

Raise one value to the power of another.

Parameters
gGraph that owns the result.
aBase.
bExponent.
Returns
The power result, or NULL if allocation fails.

◆ mg_relu()

mg_value * mg_relu ( mg_graph g,
mg_value a 
)

Apply the ReLU activation function to a value.

Parameters
gGraph that owns the result.
aInput value.
Returns
The activated value, or NULL if allocation fails.

◆ mg_scalar()

mg_value * mg_scalar ( mg_graph g,
float  data 
)

Create a new scalar value.

Parameters
gGraph that owns the new value.
dataInitial scalar data.
Returns
The new value, or NULL if allocation fails.

◆ mg_set_data()

void mg_set_data ( mg_value v,
float  data 
)

Set the scalar data of a value.

Parameters
vValue to update.
dataNew scalar data.

◆ mg_set_grad()

void mg_set_grad ( mg_value v,
float  grad 
)

Set the gradient of a value.

Parameters
vValue to update.
gradNew gradient.

◆ mg_square()

mg_value * mg_square ( mg_graph g,
mg_value a 
)

Square a value.

Parameters
gGraph that owns the result.
aValue to square.
Returns
The squared value, or NULL if allocation fails.

◆ mg_sub()

mg_value * mg_sub ( mg_graph g,
mg_value a,
mg_value b 
)

Subtract one value from another.

Parameters
gGraph that owns the result.
aMinuend.
bSubtrahend.
Returns
The difference, or NULL if allocation fails.

◆ mg_tanh()

mg_value * mg_tanh ( mg_graph g,
mg_value a 
)

Apply the tanh activation function to a value.

Parameters
gGraph that owns the result.
aInput value.
Returns
The activated value, or NULL if allocation fails.

◆ mg_zero_grad()

void mg_zero_grad ( mg_graph g)

Set all mg_value gradients in a graph to zero.

Parameters
gGraph whose values should be reset.