Deep Learning Specializations

Foundations of Deep Learning

Deep Learning Fundamentals & Building Neural Networks — Complete Beginner Guide
Deep Learning · Complete Guide

Deep Learning Fundamentals & Building Neural Networks

A detailed beginner-friendly English guide covering 9 sections from zero: from “What is AI?” to “How do I build my first model with PyTorch?” Every concept comes with plain explanations, real-life analogies, and step-by-step numeric examples — with no assumed prior background.

✍ Abdurrahman Al-Rifai
9 sections
Beginners
Examples + code
01 Section One

Introduction to Artificial Intelligence

Before we build a neural network, we need to understand where deep learning sits in the broader world of artificial intelligence — and why it is almost everywhere today.

AI vs ML vs Deep Learning

Three terms used constantly — and often confused. Picture them as nested circles:

Artificial Intelligence (AI) Any system mimicking intelligence: rules, search, ML, DL Machine Learning (ML) Learning from data without hand-coding every rule Deep Learning (DL) Multi-layer neural networks
Every DL is ML, and practically every ML is AI — but not the other way around

Artificial Intelligence (AI)

The broadest umbrella: any technology that makes machines “smart.” Includes chess rules, expert systems, ML, and DL.

Machine Learning (ML)

The model learns from examples. Includes Decision Trees, SVM, Random Forest, and neural networks.

Deep Learning (DL)

ML using deep neural networks (3+ hidden layers). Strongest on images, text, and audio.

Everyday example

Spam filtering:

  • Traditional AI: manual rules — “if the subject contains FREE or WIN → spam.”
  • ML: Naive Bayes learns from thousands of spam/ham messages.
  • DL: an LSTM or Transformer understands the full context — “this offer is free but from my trusted bank.”

All three are AI — but DL wins when data is abundant and patterns are complex.

History of Deep Learning — from idea to revolution

YearEventWhy it matters
1943McCulloch & Pitts — first mathematical neuron modelFoundation: neuron = sum inputs + threshold
1958Perceptron — Frank RosenblattFirst trainable network — then criticized for its limits
1969Minsky & Papert bookProved Perceptron limits (XOR problem) → “AI winter”
1986Backpropagation — Rumelhart, HintonMethod to train multi-layer networks
2006Hinton — Deep Belief NetworksRenewed interest in deep networks
2012AlexNet — ImageNetRevolution: CNN + GPU beat rivals by 10%+
2016AlphaGoDL + Reinforcement Learning beats Go champion
2017TransformerFoundation of GPT and every modern LLM
2022–2026ChatGPT, GPT-4, multimodal modelsDL accessible to everyone
Why was 2012 a turning point?

ImageNet competition: 1.2 million images, 1000 categories. AlexNet achieved 15.3% error — second place: 26%. A huge gap. Companies realized: massive data + GPU + deep networks = the future of AI.

Major applications today

DomainApplicationTechnique
Translation & chatGoogle Translate, ChatGPT, ClaudeTransformer / LLM
Computer visionFace ID, object detection, self-driving carsCNN
AudioSiri, Alexa, speech-to-textRNN / Transformer
MedicineDiagnosis from scans, drug discoveryCNN, GNN
Creative generationDALL-E, Midjourney, Stable DiffusionDiffusion / GAN
RecommendationsNetflix, YouTube, AmazonEmbeddings + Deep Models
GamesAlphaGo, AlphaStarDL + Reinforcement Learning

Why did Deep Learning succeed?

DL failed in the 1970s and 1980s — then surged after 2012. The reason: three factors came together:

1. Big Data

The internet, phones, cameras — millions of images, texts, and recordings. DL needs lots of data to learn.

2. Compute power (GPU)

NVIDIA GPUs run millions of operations in parallel — training on GPU is 10–100× faster than CPU.

3. Better algorithms

ReLU, Dropout, BatchNorm, Adam, ResNet — solved vanishing gradient and overfitting.

Simple analogy

Learning to cook: you need many recipes (data), a well-equipped kitchen (GPU), and refined techniques (algorithms). Without any of the three — cooking (training) fails or takes years.

What distinguishes DL from classic ML?

In traditional ML, an expert hand-picks features. In DL, the network discovers features on its own from raw data — edges → shapes → parts → whole object. This “Representation Learning” is the secret of DL’s power.

02 Section Two

Essential Math — in the Simplest Form Possible

You do not need a math degree to start deep learning — but understanding the core ideas makes everything clearer. This section explains what you actually need, with examples tied to neural networks.

Linear Algebra

Vectors

A vector is an ordered list of numbers. Think of it as an arrow in space, or a row of data.

Example: student data

A student has 3 attributes: GPA=3.5, study hours=25, attendance=90%. We represent them as a vector:

x = [3.5, 25, 90]

In a neural network, each input sample = one vector. An MNIST image 28×28 = a vector of length 784 (one number per pixel).

Matrices

A matrix is a table of numbers (rows × columns). In DL, weights are stored as matrices.

Example: layer weights

A layer with 3 inputs and 2 neurons. Weights form a 3×2 matrix:

W = [[0.5, 0.2], [0.3, -0.1], [0.8, 0.4]]

Row i = weights of neuron i from all inputs. Column j = weights of all neurons from input j.

Matrix Multiplication

This operation is the heart of forward propagation. When you multiply a weight matrix W by an input vector x, you get the layer outputs.

z = W · x + b
Step-by-step numeric example

Input x = [2, 1], weight w = [0.5, 0.3], bias b = 0.1:

z = (0.5 × 2) + (0.3 × 1) + 0.1 = 1.0 + 0.3 + 0.1 = 1.4

This is exactly what one neuron does: a linear combination of weighted inputs.

Why matrix multiplication?

Instead of computing each neuron separately, we multiply one matrix by an entire batch — GPUs do this in parallel. 32 images × 784 pixels = a 32×784 matrix multiplied by W — massive speed.

Eigenvalues — simplified idea

A slightly advanced concept, but useful for some techniques. When you multiply matrix A by vector v, the result is usually a new vector. But sometimes v stays in the same direction — only its length changes. That v is an eigenvector, and the scaling factor is the eigenvalue.

Where is this used in AI?
  • PCA (Principal Component Analysis): dimensionality reduction — keep the most important directions.
  • Understanding training: Hessian matrix and optimization stability analysis.
  • Graph Neural Networks: analyzing network structure.

As a beginner: know they are “special directions” in data — you do not need to compute them by hand; libraries handle that.

Calculus Basics

Partial Derivatives

A derivative is the “rate of change.” If you change x slightly, how much does y change? A partial derivative is the same idea when a function has multiple variables — differentiate with respect to one variable while holding the others fixed.

Example

Loss function depending on two weights: L(w₁, w₂) = (w₁ - 3)² + (w₂ - 5)²

∂L/∂w₁ = 2(w₁ - 3) — at w₁=3, the derivative = 0 (minimum point).

∂L/∂w₂ = 2(w₂ - 5) — at w₂=5, the derivative = 0.

During training: we compute ∂Loss/∂w for every weight — “in which direction should I adjust this weight to reduce error?”

The Gradient

The gradient is a vector collecting all partial derivatives. It points in the direction of steepest ascent. To descend (reduce Loss) we go opposite the gradient.

∇L = [∂L/∂w₁, ∂L/∂w₂, ..., ∂L/∂wₙ]
Analogy: descending a foggy mountain

You are on a mountain and cannot see the bottom (minimum Loss). The gradient tells you: “descend in this direction.” You take a small step opposite the gradient — repeat until you reach the bottom. That is exactly Gradient Descent.

The Chain Rule

If y depends on z, and z depends on x — how does y change when x changes?

dy/dx = (dy/dz) × (dz/dx)
Simple example

y = z², z = 3x. So y = (3x)² = 9x².

dy/dx = dy/dz × dz/dx = 2z × 3 = 2(3x) × 3 = 18x ✓

Why Chain Rule = foundation of Backpropagation

The network is a chain: Input → layer1 → layer2 → ... → Loss. Error flows backward through each layer by multiplying derivatives — Chain Rule. Without it, we cannot know how to update weights in early layers of a deep network.

03 Section Three

Neural Networks

A neural network is a group of small “computers” (neurons) connected together. Each neuron takes inputs, computes, and passes the result to the next.

The Biological Neuron

In your brain, a neuron receives electrical signals through dendrites, sums them in the cell body, and if they exceed a threshold it fires a pulse through the axon to the next neurons.

Analogy with AI

Inputs ≈ Dendrites | Summation ≈ Cell Body | Threshold ≈ Bias | Fire/don't fire ≈ Activation | Output ≈ Axon | Weights ≈ strength of each synapse connection

The Artificial Neuron

A simplified mathematical version: takes numbers, multiplies each by a weight, sums them, adds bias, and passes through an activation function.

y = activation(w₁x₁ + w₂x₂ + ... + wₙxₙ + b)
x₁ x₂ x₃ Σ + b Activation y
Artificial neuron: inputs × weights → sum + bias → activation → output

Perceptron — the simplest network

Perceptron (1958) = one neuron. It classifies points into two categories with a separating line.

Example: student admission

Two inputs: GPA (0–4) and study hours (0–40). A Perceptron learns weights like w₁=0.6, w₂=0.3, b=-2.5.

Student GPA=3.5, study=25: 0.6×3.5 + 0.3×25 + (-2.5) = 2.1+7.5-2.5 = 7.1 → activation → “accepted.”

Perceptron limit: cannot solve XOR (data not linearly separable). Solution: multiple layers — MLP.

Multi-Layer Perceptron (MLP)

A network of layers: input → hidden layers → output. Each layer is fully connected (every neuron connects to every neuron in the next layer).

Input layer

Receives raw data — 784 neurons for an MNIST image. Does not “learn” — just passes data through.

Hidden layers

Where the “depth” lives. They extract features: edges → shapes → parts.

Output layer

The result: 10 neurons for 10 digits, 2 for cat/dog, or 1 for regression.

Weights, Bias, and Parameters

  • Weights (w): importance of each input — “how much does this pixel affect the decision?” Learned from data.
  • Bias (b): shifts the activation threshold — lets a neuron fire even without strong inputs. Like a “default tendency to approve.”
  • Parameters: all weights + all biases. A 784→128→10 network ≈ 101,000 parameters.
Counting parameters

Layer 784 → 128: weights = 784×128 = 100,352 + 128 bias = 100,480

Layer 128 → 10: 128×10 + 10 = 1,290

Total ≈ 101,770 numbers the model learns!

04 Section Four

Forward Propagation

When you feed data into a trained network, it flows from input to output — that is forward propagation. Three steps per layer: linear combination → activation → pass to next layer.

Linear Combination

Each neuron computes: z = w·x + b — multiply each input by its weight, sum, add bias.

Example: 3 inputs → one neuron

x = [2, -1, 3], w = [0.5, 0.8, -0.2], b = 0.1

z = (0.5×2) + (0.8×(-1)) + (-0.2×3) + 0.1 = 1.0 - 0.8 - 0.6 + 0.1 = -0.3

This z is before activation — the “raw” result.

For a full layer: do this for every neuron — or efficiently: z = W·x + b (matrix multiplication).

Activation

We pass z through a nonlinear activation function: a = activation(z). Without it, 100 layers collapse to one straight line — useless.

Continuing the example with ReLU

z = -0.3 → ReLU(-0.3) = max(0, -0.3) = 0

If z = 2.5 → ReLU(2.5) = 2.5

Prediction

After passing through all layers, the output layer gives the final prediction:

  • Classification: Softmax converts numbers to probabilities — highest = prediction.
  • Regression: one neuron outputs a number — house price, temperature.
  • Binary: Sigmoid outputs a 0–1 probability — spam or not.
Full example: MNIST digit classification
  1. Input: 28×28 image = 784 pixels (values 0–1 after normalization).
  2. Layer 1: 784 → 128 neurons. z₁ = W₁·x + b₁, a₁ = ReLU(z₁).
  3. Layer 2: 128 → 64. z₂ = W₂·a₁ + b₂, a₂ = ReLU(z₂).
  4. Output layer: 64 → 10. z₃ = W₃·a₂ + b₃.
  5. Softmax: [0.01, 0.02, 0.85, 0.03, 0.02, 0.01, 0.02, 0.02, 0.01, 0.01]
  6. Prediction: digit 2 with 85% probability.

All of this happens in fractions of a second on a GPU.

784 pixels 128 + ReLU 64 + ReLU 10 + Softmax 2
Forward pass path for handwritten digit classification
05 Section Five

Activation Functions

Activation functions add nonlinearity — without them the network cannot learn complex patterns. Each function has strengths and weaknesses.

Sigmoid

σ(x) = 1 / (1 + e⁻ˣ)

Maps any number to a value between 0 and 1 — like a probability.

Examples

σ(0) = 0.5 | σ(2) ≈ 0.88 | σ(-2) ≈ 0.12

Use: binary classification (single output layer), gates in LSTM.

Drawback: vanishing gradient in deep networks — gradient shrinks exponentially.

Tanh

tanh(x) = (eˣ - e⁻ˣ) / (eˣ + e⁻ˣ)

Outputs between -1 and +1 — centered around zero (sometimes better than Sigmoid).

Examples

tanh(0) = 0 | tanh(1) ≈ 0.76 | tanh(-1) ≈ -0.76

Use: RNNs, some older hidden layers.

ReLU (Rectified Linear Unit)

ReLU(x) = max(0, x)

If negative → 0. If positive → unchanged. Most common in hidden layers.

Examples

ReLU(-3) = 0 | ReLU(0) = 0 | ReLU(5) = 5

Advantage: fast, gradient = 1 for positive values — fixes vanishing gradient.

Drawback: “Dead ReLU” — neurons stuck at 0 forever if gradient is always 0.

Leaky ReLU

LeakyReLU(x) = x if x > 0, else α·x (α ≈ 0.01)

Instead of zeroing negatives, pass a small fraction — the neuron does not “die.”

Example (α = 0.01)

LeakyReLU(-10) = -0.1 | LeakyReLU(5) = 5

ELU (Exponential Linear Unit)

ELU(x) = x if x > 0, else α(eˣ - 1)

Smooth negative values (not linear like Leaky). Mean outputs closer to zero — sometimes faster training.

GELU (Gaussian Error Linear Unit)

GELU(x) ≈ x · Φ(x)

Smooth function used in Transformers (BERT, GPT). Blends ReLU with probabilistic properties.

Why GELU in LLMs?

Transformers are sensitive to fine details in representations. GELU is smoother than ReLU — small input changes affect the output gradually, which helps with text.

Softmax

softmax(xᵢ) = eˣⁱ / Σⱼ eˣʲ

Converts n numbers into n probabilities that sum to 1 — for multi-class classification.

Example: cat, dog, bird

Raw outputs: [2.0, 1.0, 0.1]

Softmax: [0.659, 0.242, 0.099] → prediction: cat (65.9%)

Raw outputs: [0.5, 3.0, 0.2] → Softmax: [0.06, 0.90, 0.04] → dog (90%)

FunctionRangeWhere used
ReLU / Leaky / ELU0 to ∞ (approx.)Hidden layers in CNN and MLP
Sigmoid0 to 1Binary classification, LSTM gates
Tanh-1 to 1RNN, hidden layers
GELUapprox. -0.17 to ∞Transformers (BERT, GPT)
Softmax0 to 1 (sum = 1)Output layer — multi-class classification
06 Section Six

Loss Functions

A loss function measures “how wrong were we?” — a single number. Training = minimizing this number. The choice of loss depends on the task type.

MSE — Mean Squared Error

MSE = (1/n) Σ (y_pred - y_true)²

Average of the squared difference between prediction and truth. For regression (predicting a number).

Example: predicting house price

Truth: $200,000. Predictions: [195,000, 210,000, 198,000]

Errors: [-5000, +10000, -2000] → squares: [25M, 100M, 4M]

MSE = (25+100+4)/3 = 43 million — large errors are penalized heavily (because they are squared).

MAE — Mean Absolute Error

MAE = (1/n) Σ |y_pred - y_true|

Average of the absolute error — less sensitive to outliers than MSE.

Same example

MAE = (5000 + 10000 + 2000) / 3 = $5,667 — easier to interpret: “on average we are off by $5,667.”

Binary Cross Entropy — binary classification

BCE = -[y·log(p) + (1-y)·log(1-p)]

For two classes (spam/ham, sick/healthy). y = 0 or 1, p = predicted probability (from Sigmoid).

Example: spam detection

Truth y=1 (spam). Prediction p=0.9 (confident and correct): BCE = -log(0.9) ≈ 0.105 — small error.

Truth y=1. Prediction p=0.1 (confident but wrong!): BCE = -log(0.1) ≈ 2.30 — heavy penalty.

Categorical Cross Entropy — multi-class classification

CCE = -Σ yᵢ · log(pᵢ)

For 3+ classes (digits 0–9, animal types). y = one-hot [0,0,1,0,...], p = Softmax probabilities.

Example: cat/dog/bird classification

Truth: cat → y = [1, 0, 0]

Good prediction p = [0.8, 0.15, 0.05]: CCE = -log(0.8) ≈ 0.22

Bad prediction p = [0.1, 0.85, 0.05]: CCE = -log(0.1) ≈ 2.30

Hinge Loss — SVM and margin classification

Hinge = max(0, 1 - y · f(x)) where y ∈ {-1, +1}

Heavily penalizes wrong predictions, but if the prediction is correct with enough margin — Loss = 0.

Example

y=+1 (positive class), f(x)=0.8: Hinge = max(0, 1-0.8) = 0.2

y=+1, f(x)=2.0: Hinge = max(0, 1-2) = 0 — confident enough, no penalty.

Use: SVM, some classification models. Less common than Cross-Entropy in deep networks.

KL Divergence — measuring “distance between distributions”

KL(P || Q) = Σ P(x) · log(P(x) / Q(x))

Measures how much distribution P (truth) differs from distribution Q (model prediction). KL = 0 means perfect match.

Where is it used?
  • VAE (Variational Autoencoders): approximating latent distribution.
  • Knowledge Distillation: transferring knowledge from a large model to a small one.
  • Language Models: comparing word distributions.
Loss functionTaskFinal layer output
MSE / MAERegression (continuous number)Linear (no activation or ReLU)
Binary Cross EntropyTwo classesSigmoid
Categorical Cross Entropy3+ classesSoftmax
Hinge LossMargin classificationLinear
KL DivergenceComparing distributionsSoftmax / Logits
07 Section Seven

Gradient Descent

After computing Loss, we need to update weights to reduce it. Gradient Descent is the core algorithm — we walk opposite the gradient direction.

Cost Function

Cost Function = Loss but over all training data (or a batch). Goal: find weights that minimize Cost.

Loss vs Cost

Loss: error for one example or small batch. Cost: average Loss over a larger set. In practice they are used interchangeably — what matters is: a number we want to minimize.

Optimization

Optimization = finding the best parameter values. In DL, “best” = lowest Cost on validation data, not just training data.

w_new = w_old − learning_rate × ∂Cost/∂w

Learning Rate

Step size for each update. The most important hyperparameter of all.

Too large (lr = 1.0)

Jumps over the minimum — Loss oscillates or explodes (NaN). Training fails.

Too small (lr = 0.00001)

Very slow — may take days. But stable.

Just right (lr = 0.001)

Good balance — common starting point with Adam.

Numeric example

Weight w = 2.0, gradient ∂L/∂w = 4.0 (increasing w increases Loss), lr = 0.1

w_new = 2.0 - 0.1 × 4.0 = 1.6 — we decreased w because gradient is positive.

If gradient = -3.0: w_new = 2.0 - 0.1 × (-3) = 2.3 — we increased w because gradient is negative.

Batch Gradient Descent

Uses all training data in one update step.

  • Advantage: accurate, stable gradient.
  • Drawback: slow — 60,000 MNIST images in one step = huge memory and slowness.

Stochastic Gradient Descent (SGD)

Uses one example per update step.

  • Advantage: very fast, escapes local minima (noise helps).
  • Drawback: noisy gradient — Loss fluctuates heavily.

Mini-Batch Gradient Descent

The golden middle: a small group (32, 64, 128 examples) per step. Most used in DL.

Example: MNIST

60,000 training images, batch size = 128:

Iterations per epoch = 60,000 / 128 ≈ 469 update steps.

15 epochs = 469 × 15 ≈ 7,035 total update steps.

TypeData per stepSpeedStability
Batch GDAll dataSlowVery stable
SGD1 exampleVery fastFluctuating
Mini-Batch32–256Fast + GPU-friendlyExcellent balance ✓
Advanced optimizers (complementary)

Adam (most popular): combines Momentum + adaptive learning rate per weight. Start with lr=0.001. SGD + Momentum and AdamW (for Transformers) are also common choices.

08 Section Eight

Backpropagation

A network with 100,000 weights — how do we know which weight to adjust and by how much? Backpropagation (1986) is the answer: it computes the gradient for every weight efficiently using the Chain Rule.

Chain Rule in Backprop

The network is a chain of functions: x → layer1 → layer2 → ... → Loss. To adjust a weight in layer 1, we need: how much does this weight affect Loss? = multiply derivatives across the chain.

∂Loss/∂w₁ = (∂Loss/∂a) × (∂a/∂z) × (∂z/∂w₁)

Computing Gradients

Simplified example — small network

Forward:

  1. Input x = 2.0
  2. Hidden layer: z = w₁·x + b₁ = 0.5×2 + 0.1 = 1.1
  3. Activation: a = ReLU(1.1) = 1.1
  4. Output: y_pred = w₂·a + b₂ = 0.8×1.1 + 0.2 = 1.08
  5. Truth y_true = 1.5. Loss = (1.08 - 1.5)² = 0.176

Backward:

  1. ∂Loss/∂y_pred = 2(1.08 - 1.5) = -0.84
  2. ∂Loss/∂w₂ = ∂Loss/∂y_pred × a = -0.84 × 1.1 = -0.924
  3. ∂Loss/∂a = ∂Loss/∂y_pred × w₂ = -0.84 × 0.8 = -0.672
  4. ∂Loss/∂w₁ = ∂Loss/∂a × x = -0.672 × 2 = -1.344 (because ReLU is active)

Each weight now knows: “increase me” (negative gradient) or “decrease me” (positive gradient).

Weight Updates

w = w − lr × gradient
Continuing the example (lr = 0.1)

w₂_new = 0.8 - 0.1 × (-0.924) = 0.8 + 0.092 = 0.892

w₁_new = 0.5 - 0.1 × (-1.344) = 0.5 + 0.134 = 0.634

Next time, the prediction will be closer to 1.5 — Loss smaller.

Automatic Differentiation

Computing gradients by hand for a large network is impossible. Autodiff in PyTorch and TensorFlow does it automatically:

loss = criterion(output, target) # compute Loss
loss.backward() # automatic Backprop — computes all gradients
optimizer.step() # update weights
optimizer.zero_grad() # clear gradients for next step
The full training loop
  1. Forward: data → prediction
  2. Loss: compare to truth
  3. Backward: loss.backward() — gradients for every weight
  4. Update: optimizer.step() — adjust weights
  5. Repeat for every batch and every epoch
← Backprop: gradient flows backward Input Hidden Hidden Output Loss Forward: Input → ... → Output → Loss Backward: Loss → gradient per layer → weight updates
Forward (left to right) and Backprop (right to left) — the heart of network training
Vanishing Gradient: in old deep networks, gradient shrinks → early layers do not learn. Fix: ReLU, BatchNorm, ResNet.
Exploding Gradient: gradient grows → NaN. Fix: Gradient Clipping, smaller lr.
09 Section Nine

Building Your First Model — TensorFlow, Keras, PyTorch

Now we connect everything we learned to real code. We will build a simple model to classify MNIST digits — the classic beginner project.

TensorFlow and Keras

TensorFlow = Google's deep learning library. Keras = high-level interface on top of TensorFlow — simple for beginners.

import tensorflow as tf
from tensorflow import keras

model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])

model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train / 255.0 # normalize to 0-1

model.fit(x_train, y_train, epochs=15, batch_size=128, validation_split=0.1)
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.2%}")
What does each line do?
  • Flatten: converts 28×28 to 784.
  • Dense(128, relu): hidden layer with 128 neurons + ReLU.
  • Dropout(0.2): randomly turns off 20% of neurons — fights overfitting.
  • Dense(10, softmax): 10 classes (digits 0–9).
  • compile: sets optimizer (Adam), Loss, and metric (accuracy).
  • fit: training — 15 epochs, batch 128.
  • evaluate: test on data the model has never seen.

PyTorch

PyTorch = Facebook/Meta library — most popular in research and education. More “Pythonic” and flexible than TensorFlow.

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms

class MNISTNet(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.Sequential(
nn.Flatten(),
nn.Linear(784, 128),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 10)
)
def forward(self, x):
return self.layers(x)

model = MNISTNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

transform = transforms.Compose([transforms.ToTensor()])
train_data = datasets.MNIST('./data', train=True, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_data, batch_size=128, shuffle=True)

for epoch in range(15):
for images, labels in train_loader:
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
FrameworkStrengthsWhen to use
TensorFlow + KerasEasy, production-ready, TFLite for mobileProduction projects, Google Cloud, mobile
PyTorchFlexible, Pythonic, research-friendlyBeginners, research, LLMs

Training

Practical training steps:

  1. Load data: MNIST, CIFAR, or your own dataset.
  2. Preprocessing: normalize (0–1), resize, augmentation.
  3. Split: train (80%) / validation (10%) / test (10%).
  4. Build model: layers, activation, dropout.
  5. Train: watch train loss and validation loss.
  6. Early Stopping: stop if validation loss rises (overfitting).
What to watch during training
  • Train Loss ↓: model is learning — good.
  • Val Loss ↓: generalizing — excellent.
  • Train ↓ but Val ↑: overfitting — add Dropout or more data.
  • Both not decreasing: underfitting — add layers or increase lr.

Evaluation

After training, evaluate on test data the model has never seen:

Accuracy

Percentage of correct classifications. MNIST: 97–99% is excellent for a simple MLP.

Precision / Recall

Important when classes are imbalanced — medicine, fraud detection.

Confusion Matrix

Table showing which classes get confused — reveals weak spots.

Test Loss

Should be close to validation loss — if much higher = overfitting.

Your first project — 7-day plan
  1. Day 1–2: install Python + PyTorch. Run MNIST from the official tutorial.
  2. Day 3: change layer count and neuron count — observe the effect.
  3. Day 4: try different learning rates (0.1, 0.01, 0.001).
  4. Day 5: add Dropout — compare overfitting.
  5. Day 6: plot the Loss curve (train vs val).
  6. Day 7: write a README on GitHub — document what you learned.
Next step in your journey

After MNIST: try CIFAR-10 (color images) with a CNN — you will learn Conv layers. Then explore Part 3 on Transformers and LLMs. The Math for AI and Linear Algebra guides deepen your understanding.

FAQ Questions

Common Beginner Questions

Do I need advanced math to start deep learning?
Not to begin — this guide is conceptual. For practical work you will gradually learn: linear algebra (matrices), calculus (derivatives, Chain Rule), and probability. Libraries compute gradients automatically — but understanding helps when debugging.
PyTorch or TensorFlow for beginners?
PyTorch — clearer for beginners, larger community in research and education. TensorFlow is strong for production and mobile (TFLite). The concepts are identical — if you learn one, the other is easy.
How many layers do I need for my first project?
Start with 2–3 hidden layers. MNIST: two layers are enough. Do not start with 50 layers — overfitting and training difficulty await. Increase complexity gradually.
Loss is not decreasing — what should I do?
(1) Reduce learning rate 10×. (2) Make sure data is normalized. (3) Verify labels are correct. (4) Try a simpler architecture. (5) Confirm loss.backward() and optimizer.step() are actually running.
What is the difference between epoch, batch, and iteration?
Epoch = one full pass over all training data. Batch = a small group (128 examples). Iteration = one update step = one batch. 60K data, batch 128 → 469 iterations/epoch.
Do I need a GPU?
For learning: no — MNIST runs on CPU in minutes. Google Colab gives you a free GPU. For large projects (high-resolution images, LLMs) a GPU is essential.