Skip to main content

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.

The cmd/dot tool generates a Graphviz DOT description of a computation graph. This is useful for understanding how a function’s graph changes as you compute higher-order derivatives.

Prerequisites

Install Graphviz to render DOT files as images:
# macOS
brew install graphviz

# Ubuntu / Debian
sudo apt-get install graphviz

Generating a graph

Run cmd/dot/main.go to produce a DOT file, then pipe it to Graphviz:
go run cmd/dot/main.go -func tanh -order 2 -verbose > sample.dot
dot sample.dot -T png -o sample.png
This generates the second-order derivative graph for tanh and writes it to sample.png.

Available flags

FlagTypeDefaultDescription
-funcstringtanhThe function to visualize. See the table below for valid values.
-orderint1The derivative order to visualize. 1 shows the first derivative graph, 2 the second, and so on. Must be ≥ 1.
-xfloat641.0The input value at which the graph is evaluated.
-verboseboolfalseInclude variable values as node labels in the graph.

Available functions

-func valueMathematical function
sinsin(x)
coscos(x)
tanhtanh(x)
expexp(x)
loglog(x)
pow
square
neg-x

Examples

First-order derivative of tanh

go run cmd/dot/main.go -func tanh -order 1 > tanh1.dot
dot tanh1.dot -T png -o tanh1.png

Second-order derivative of tanh

go run cmd/dot/main.go -func tanh -order 2 > tanh2.dot
dot tanh2.dot -T png -o tanh2.png

Third-order derivative with values shown

go run cmd/dot/main.go -func tanh -order 3 -verbose > tanh3.dot
dot tanh3.dot -T png -o tanh3.png

Sine function at a different input value

go run cmd/dot/main.go -func sin -order 2 -x 0.5 -verbose > sin2.dot
dot sin2.dot -T png -o sin2.png

Output formats

Graphviz supports many output formats via the -T flag:
dot sample.dot -T png -o sample.png   # PNG image
dot sample.dot -T svg -o sample.svg   # SVG (scalable)
dot sample.dot -T pdf -o sample.pdf   # PDF document
dot sample.dot -T dot -o sample.dot   # Canonical DOT (for further processing)

How it works

The tool:
1

Evaluates the function

Creates an input variable x with the given -x value, applies the chosen function, and names the output y.
2

Runs backward with CreateGraph

Calls y.Backward(variable.Opts{CreateGraph: true}) to retain the computation graph through the backward pass, making the gradient itself differentiable.
3

Iterates to the requested order

For each additional order, calls gx.Backward(variable.Opts{CreateGraph: true}) on the previous gradient, clearing x.Grad before each step.
4

Generates DOT output

Passes the final gradient variable to dot.Graph(), which walks the computation graph and produces DOT-format text printed to stdout.
The resulting DOT file describes nodes (variables and functions) and directed edges (data flow), which Graphviz renders into a graph image.
Higher-order derivative graphs grow quickly in node count. For -order 4 or above, consider using -T svg so the image remains crisp when zoomed in, or use a layout engine with better handling of large graphs such as neato or fdp instead of the default dot.
# Use the 'neato' layout for large graphs
neato sample.dot -T svg -o sample.svg

Next steps

Higher-Order Gradients

Learn how CreateGraph enables the multi-order differentiation that the dot tool visualizes.

Autograd Concepts

Understand computation graph construction and traversal.

Functions API

Browse all available differentiable functions.

Variable API

Reference for variable.Opts and Backward options.