What are Neural Networks?
Neural Networks are computational models inspired by the human brain. They consist of interconnected nodes (neurons) organized in layers that learn to recognize patterns in data through training. Neural networks form the backbone of modern AI systems, from image recognition to large language models.
π‘ Key Insight: A neural network learns by adjusting the weights of connections between neurons through a process called backpropagation, gradually improving its ability to make accurate predictions.
Deep Learning
Many layers
CNNs
Images
RNNs
Sequences
Transformers
Attention
Basic Architecture
Input Layer
Receives raw data (pixels, text embeddings, numbers). Each node represents one feature.
Hidden Layers
Learn intermediate representations. More layers = deeper network = more complex patterns.
Output Layer
Produces predictions (classification, regression, generation). Size depends on task.
Key Concepts
Activation Functions
Non-linear functions (ReLU, Sigmoid, Tanh) that enable networks to learn complex patterns.
Backpropagation
Algorithm that calculates gradients and updates weights by propagating error backwards.
Loss Function
Measures how wrong predictions are. Training minimizes this function (MSE, Cross-Entropy).
Gradient Descent
Optimization algorithm that adjusts weights in the direction that reduces loss.
Types of Neural Networks
| Type | Best For | Examples |
|---|---|---|
| Feedforward (MLP) | Tabular data, classification | Fraud detection, pricing |
| CNN | Images, spatial patterns | ResNet, VGG, EfficientNet |
| RNN / LSTM | Sequential data, time series | Speech recognition |
| Transformer | Language, attention-based | GPT, BERT, T5 |
| GAN | Generation, adversarial | StyleGAN, DALL-E |
Activation Functions (Deep Dive)
Activation functions introduce non-linearity into neural networks, allowing them to learn complex patterns. Without activation functions, a neural network would be equivalent to a single linear transformation.
π‘ Why Non-Linearity Matters: Real-world data has complex, non-linear relationships. Linear functions can only draw straight lines/planes to separate data, but activation functions enable networks to learn curved, intricate decision boundaries.
ReLU (Rectified Linear Unit)
The most widely used activation function in deep learning. Simple yet effective.
Formula:
f(x) = max(0, x)
- β Computationally efficient
- β Reduces vanishing gradient problem
- β Sparse activation (neurons can be "off")
- β οΈ "Dying ReLU" problem (neurons stuck at 0)
Sigmoid
Squashes any input to a value between 0 and 1. Historically important, now mainly used for binary outputs.
Formula:
Ο(x) = 1 / (1 + eβ»Λ£)
- β Output interpretable as probability
- β Smooth gradient
- β οΈ Vanishing gradients for extreme values
- β οΈ Not zero-centered
Tanh (Hyperbolic Tangent)
Similar to Sigmoid but outputs values between -1 and 1. Zero-centered, which helps training.
Formula:
tanh(x) = (eΛ£ - eβ»Λ£) / (eΛ£ + eβ»Λ£)
- β Zero-centered output
- β Stronger gradients than Sigmoid
- β Good for RNNs/LSTMs
- β οΈ Still has vanishing gradient issue
Softmax
Converts a vector of raw scores into a probability distribution. Essential for multi-class classification.
Formula:
softmax(xα΅’) = eΛ£β± / Ξ£β±Ό eΛ£Κ²
- β Outputs sum to 1 (probability)
- β Differentiable for backprop
- β Standard for classification output
- π― Used with Cross-Entropy loss
Example: [2.0, 1.0, 0.1]
0.659
Cat
0.242
Dog
0.099
Bird
Sum = 1.0 β
# PyTorch activation functions
import torch import torch.nn.functional as F x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]) relu_out = F.relu(x) # [0, 0, 0, 1, 2] sigmoid_out = torch.sigmoid(x) # [0.12, 0.27, 0.5, 0.73, 0.88] tanh_out = torch.tanh(x) # [-0.96, -0.76, 0, 0.76, 0.96] # Softmax on a batch of logits logits = torch.tensor([2.0, 1.0, 0.1]) probs = F.softmax(logits, dim=0) # [0.659, 0.242, 0.099]
Training Process (Deep Dive)
Training a neural network is an iterative optimization process. The network makes predictions, measures errors, and adjusts its parameters to improve over time.
1οΈβ£ Forward Pass
Data flows from input through each layer to produce an output prediction.
# For each layer: z = W Β· x + b # Linear transformation a = activation(z) # Apply non-linearity x = a # Output becomes next input # Example with 2 layers: z1 = W1 Β· input + b1 β a1 = ReLU(z1) z2 = W2 Β· a1 + b2 β output = softmax(z2)
2οΈβ£ Loss Calculation
Quantifies how wrong the prediction is compared to the true label.
MSE (Regression)
L = (1/n) Ξ£(y - Ε·)Β²
Cross-Entropy (Classification)
L = -Ξ£ yα΅’ log(Ε·α΅’)
3οΈβ£ Backward Pass (Backpropagation)
Uses the chain rule of calculus to compute gradients of the loss with respect to each weight.
# Chain rule propagates error backwards: βL/βW2 = βL/βoutput Β· βoutput/βz2 Β· βz2/βW2 βL/βW1 = βL/βoutput Β· βoutput/βa1 Β· βa1/βz1 Β· βz1/βW1 # Each layer receives gradient from next layer # and passes modified gradient to previous layer
4οΈβ£ Update Weights (Gradient Descent)
Adjust weights in the direction that reduces the loss. The learning rate (Ξ·) controls step size.
W_new = W_old - Ξ· Β· βL/βW
SGD
Basic gradient descent
Adam
Adaptive learning rate
AdamW
Adam + weight decay
# Complete PyTorch training loop
import torch
import torch.nn as nn
import torch.optim as optim
model = nn.Sequential(
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
for epoch in range(num_epochs):
for inputs, labels in dataloader:
# 1. Forward pass
outputs = model(inputs)
# 2. Calculate loss
loss = criterion(outputs, labels)
# 3. Backward pass
optimizer.zero_grad() # Clear previous gradients
loss.backward() # Compute gradients
# 4. Update weights
optimizer.step() # Apply gradients
print(f'Epoch {epoch+1}, Loss: {loss.item():.4f}')
Popular Frameworks
Related Topics
Test Your Knowledge
Score 8/10 or higher to pass
You need to be logged in to take this quiz.
Login to Continue