This guide walks you from a blank Go module to a working gradient computation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/autograd/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
- Go 1.23 or later
Install the package
go get pulls in only the autograd module itself.Import the packages
The two packages you need for most tasks are
variable and function:function re-exports everything from variable under a shorter alias. Importing it as F keeps call sites concise.Create variables
Wrap scalar or multi-element values with
variable.New:variable.New accepts one or more float64 values. The result is a *variable.Variable holding a tensor of that shape.Run a forward pass
Apply differentiable functions to build a computation:Each function call records itself on the output variable. The computation graph grows as you compose functions.
Composite function example
Real computations involve multiple inputs. Here is the Matyas function — a common test case for gradient-based optimizers:Backward() computes partial derivatives with respect to all leaf variables in a single pass. x.Grad holds ∂z/∂x and y.Grad holds ∂z/∂y.
Call
x.Cleargrad() before each forward pass when reusing variables across training iterations. Otherwise gradients accumulate across steps.Skipping graph construction with NoGrad
During inference or evaluation you do not need gradients. Wrap those sections withvariable.Nograd() to skip building the computation graph entirely:
variable.Nograd() returns a *Span. Calling .End() (via defer) re-enables graph construction when the function returns.
Higher-order gradients
Passvariable.Opts{CreateGraph: true} to make the backward pass itself differentiable. This is required for second-order methods and higher-order derivative analysis:
What’s next
Gradient descent
Use gradients to minimize a loss function iteratively.
Deep learning
Build and train neural networks with layers, models, and optimizers.
Higher-order gradients
Implement Newton’s method and other second-order techniques.
Variable API
Full reference for the
variable package.