PyTorch (source code)
= PyTorch
{c}
\b[PyTorch] is a software framework for numerical <tensor> operations and <machine learning>, including <automatic differentiation> and tools for training <neural networks>. Its array operations provide the calculations; its differentiation machinery records how a result depends on selected inputs. A computed <gradient> is distinct from the later <optimization> step that uses it.<Automatic Differentiation with torch.autograd in the PyTorch tutorials>[Tensors, Functions and Computational graph; Computing Gradients]{ref}
For example, this small Python calculation differentiates $y=x^2+3x$ at $x=2$:
``
import torch
x = torch.tensor(2.0, requires_grad=True)
y = x * x + 3 * x
y.backward()
print(x.grad) # tensor(7.)
``
The result agrees with $dy/dx=2x+3$. The call to `backward()` computes and accumulates derivatives; it does not itself change `x` to minimize `y`. In a training loop, parameter updates and clearing old <gradients> are separate operations.<Optimizing Model Parameters in the PyTorch tutorials>[Optimizing with torch.optim; zero_grad, backward and step]{ref}
A useful consequence of its dynamically constructed computation is that executed control flow determines the recorded graph. Recording a new graph on the next execution makes it possible to differentiate the computation that actually ran. Disabling gradient tracking or detaching a value changes that connection and must be deliberate.<Automatic Differentiation with torch.autograd in the PyTorch tutorials>[More on Computational Graphs; Disabling Gradient Tracking]{ref}
Back to article page