A complete training loop for a multilayer network
Writing the training and evaluation loop by hand on synthetic data.
Goal: Understanding how PyTorch computes derivatives automatically.
import torch
x = torch.tensor(2.0, requires_grad=True)
y = x ** 2 + 3 * x
y.backward()
print("y =", y.item())
print("dy/dx =", x.grad.item())
a = torch.arange(6, dtype=torch.float32).reshape(2, 3)
print("Tensor shape:", tuple(a.shape), "| sum:", a.sum().item())
print("Available device:", "cuda" if torch.cuda.is_available() else "cpu")
With requires_grad=True PyTorch records operations in a computation graph, and after backward() the derivative is in .grad. Here y = x² + 3x, so the derivative is 2x + 3, which equals 7 at x=2.
y = 10.0 dy/dx = 7.0 Tensor shape: (2, 3) | sum: 15.0 Available device: cpu
This example was actually run and tested on 21 November 2026 on a CPU — torch 2.15.0, python 3.13.18
Writing the training and evaluation loop by hand on synthetic data.
Building a data pipeline in the standard PyTorch style and training a convolutional network.
Implementing the squash function and the routing-by-agreement algorithm between capsules as in the paper by Sabour et al. (2017).
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.