Deep learning from scratch
Tensors, layers and the architectures built from them — from ResNets and U-Nets to a framework written in NumPy, transformers, diffusion and a vision-language model.
A deep network is a stack of a few simple operations on tensors: multiply by a matrix, slide a small kernel, look up a row, normalise, add. This course takes those operations one at a time, shows exactly what each does to the shape and the numbers of a tensor, and then assembles them into the two architectures that appear everywhere in vision: the ResNet and the U-Net.
The course draws on two repositories. Lessons 1–8 come from the Tensors folder of the main practice repository linked below. Lessons 9–17 come from Deep-Learning-Problems: they rebuild a small deep learning framework in plain NumPy — fully connected, convolutional and recurrent layers with hand-written backpropagation — then return to PyTorch for a classification project, attention and the transformer, Stable Diffusion and the PaliGemma vision-language model.
Most lessons have a live animation, and the code is checked the hard way — by counting parameters, pushing a tensor through every layer, and comparing hand-written gradients with numerical ones. Several models crash or train incorrectly in ways their scripts never reveal; finding and fixing those bugs is the most instructive part of the course.
Prerequisites
- The linear algebra course: matrix–vector products, composition, and why a product of matrices is still one matrix.
- Basic Python. Lesson 8 needs a C++17 compiler and CMake; lessons 9–12 need only NumPy (and SciPy for the convolution layer).
Running the code
cd Tensors
pip install torch torchvision matplotlib
jupyter notebook 00_tensor_fundamentals_pytorch.ipynb
python deep_learning_practice/deep_learning_in_python_cuda/resnet18/resnet_18.py
For the second repository:
git clone https://github.com/prajolshrestha/Deep-Learning-Problems.git
cd Deep-Learning-Problems/01_ANN/src
pip install numpy scipy scikit-learn tabulate matplotlib
python NeuralNetworkTests.py
Everything runs on a CPU; a CUDA GPU is used when one is available. The C++ examples under deep_learning_in_cpp_cuda/ need LibTorch, the C++ distribution of PyTorch.
A habit worth keeping
Printing a model shows its layers; it does not run them. Before training anything, push one dummy batch through and check the output shape:
x = torch.randn(1, 3, 224, 224)
print(model(x).shape)
That single line would have caught both of the architecture bugs this course finds.
Lessons
- 1 Tensors, shapes and viewsA tensor is a strip of numbers plus a shape. Reshaping re-reads the strip; transposing changes the walk.
- 2 Linear layers and MLPsWhy stacking linear layers buys nothing, and how one ReLU between them changes everything.
- 3 Convolution layersA small kernel slides over the image. Kernel size, stride, padding and dilation decide how big the output is and how far each pixel sees.
- 4 Embeddings and patch embeddingsTurning words into vectors by looking up a row, and images into tokens by cutting them into patches.
- 5 Normalisation layersBatchNorm, LayerNorm, InstanceNorm, GroupNorm and RMSNorm all compute the same thing — they differ only in which numbers they average.
- 6 Residual networksThe skip connection that lets networks grow to a hundred layers — and a careful audit of the ResNet-18, -50 and -110 scripts.
- 7 U-NetAn encoder that sees what, a decoder that recovers where, and skip connections that carry the detail across — plus a decoder bug to find and fix.
- 8 PyTorch in C++ with LibTorchThe same training loop in C++ on the GPU, and why its loss falls in a perfectly straight line.
- 9 NumPy patterns and a batch generatorDrawing images without a single loop, and feeding a network one shuffled, augmented batch at a time.
- 10 A neural network in pure NumPyEvery layer is a forward and a backward method. Write four of them and backpropagation falls out.
- 11 Convolution, pooling and optimizers in NumPyThe backward passes of convolution and max pooling, sensible weight initialisation, and SGD with momentum and Adam.
- 12 Recurrent networks in NumPyA hidden state carried through time, backpropagation through time, and why long sequences make gradients vanish.
- 13 Training a classifier in PyTorchCracks in solar cells — a ResNet, a data pipeline, a trainer with validation and checkpoints, and what the loss curves say.
- 14 Regression, L1 loss and AdamFitting a straight line in PyTorch, and why the parameters never quite settle.
- 15 Attention and the transformerTranslation from English to Italian with a transformer built from its parts — attention, masks, positional encodings and pre-norm residual blocks.
- 16 Diffusion modelsStable Diffusion's parts rebuilt — CLIP, a VAE, a UNet with attention, and the DDPM sampler that turns noise into an image.
- 17 A vision-language model — PaliGemmaImages become tokens, tokens join the prompt, and a decoder-only language model answers — with rotary positions, RMSNorm and a KV cache.