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.[ref]
Automatic Differentiation with torch.autograd in the PyTorch tutorials. Tensors, Functions and Computational graph; Computing Gradients
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 . 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.[ref]Optimizing Model Parameters in the PyTorch tutorials. Optimizing with torch.optim; zero_grad, backward and step
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.[ref]
Automatic Differentiation with torch.autograd in the PyTorch tutorials. More on Computational Graphs; Disabling Gradient Tracking