TogetherIntermediate

How Neural Networks Learn

Dive deeper into the world of AI! Discover how neural networks are inspired by the human brain and learn to recognize patterns.

Neural NetworksDeep LearningPatterns

How Neural Networks Learn 🧠

Have you ever wondered how your brain recognizes your best friend's face, even if they're wearing a silly hat? Your brain has about 86 billion tiny cells called neurons that work together to recognize patterns. Artificial neural networks are inspired by this amazing biological system!

The Building Block: The Artificial Neuron

At the heart of every neural network is a simple mathematical unit inspired by biological neurons. Just like a real neuron receives signals from other neurons, processes them, and decides whether to "fire," an artificial neuron does something similar with numbers.

The Math Behind a Neuron

A single neuron performs this calculation:

z=w1x1+w2x2+w3x3+bz = w_1x_1 + w_2x_2 + w_3x_3 + b

Where:

  • x1,x2,x3x_1, x_2, x_3 are the inputs (like pixel values from an image)
  • w1,w2,w3w_1, w_2, w_3 are the weights (how important each input is)
  • bb is the bias (a threshold for activation)
  • zz is the output before applying an activation function

The weights are what the network learns during training!

💡 Think of it this way: If you're deciding whether to go outside, you might consider:

  • Temperature (x1x_1) — very important (high weight)
  • Day of the week (x2x_2) — not very important (low weight)
  • Whether it's raining (x3x_3) — super important (very high weight!)

Building a Network

When we connect many neurons together in layers, we get a neural network. Here's a simple Python example using PyTorch:

import torch
import torch.nn as nn

# A simple neural network for recognizing handwritten digits
class SimpleNeuralNetwork(nn.Module):
    def __init__(self):
        super().__init__()
        # Layer 1: 784 inputs (28x28 pixels) -> 128 neurons
        self.layer1 = nn.Linear(784, 128)
        # Layer 2: 128 neurons -> 64 neurons
        self.layer2 = nn.Linear(128, 64)
        # Output layer: 64 neurons -> 10 digits (0-9)
        self.output = nn.Linear(64, 10)
        # Activation function
        self.relu = nn.ReLU()
    
    def forward(self, x):
        x = self.relu(self.layer1(x))
        x = self.relu(self.layer2(x))
        x = self.output(x)
        return x

# Create the network
model = SimpleNeuralNetwork()
print(f"Total parameters: {sum(p.numel() for p in model.parameters())}")

The Learning Process: Gradient Descent

Neural networks learn by making mistakes and adjusting. Imagine you're blindfolded on a hillside trying to find the bottom:

  1. You feel the ground around you to see which way is down
  2. You take a small step in that direction
  3. You repeat until you can't go any lower

In math terms, we calculate the gradient (slope) of the error and adjust the weights:

wnew=woldηLww_{new} = w_{old} - \eta \cdot \frac{\partial L}{\partial w}

Where:

  • η\eta (eta) is the learning rate (how big your steps are)
  • Lw\frac{\partial L}{\partial w} is how much the loss changes when you change the weight

⚠️ Too Big Learning Rate: You might overshoot the minimum and bounce around

Too Small Learning Rate: You'll take forever to get there!

Activation Functions: Adding Non-Linearity

Without activation functions, neural networks would just be fancy linear equations. Activation functions introduce non-linearity, allowing networks to learn complex patterns.

ReLU (Rectified Linear Unit)

The most popular activation function is beautifully simple:

ReLU(x)=max(0,x)\text{ReLU}(x) = \max(0, x)

If the input is positive, pass it through. If negative, output zero. That's it!

Sigmoid

The sigmoid function squashes any number to a value between 0 and 1:

σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}}

This is useful when you want probabilities (like "80% chance this is a cat").

A Simple Training Example

Let's train a tiny network to learn the XOR problem:

import numpy as np

# XOR inputs and outputs
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

# Initialize weights randomly
np.random.seed(42)
w1 = np.random.randn(2, 4)  # 2 inputs -> 4 hidden
w2 = np.random.randn(4, 1)  # 4 hidden -> 1 output

# Training loop
learning_rate = 0.5
for epoch in range(10000):
    # Forward pass
    hidden = 1 / (1 + np.exp(-X @ w1))  # Sigmoid
    output = 1 / (1 + np.exp(-hidden @ w2))
    
    # Calculate error
    error = y - output
    
    # Backpropagation (simplified)
    d_output = error * output * (1 - output)
    d_hidden = d_output @ w2.T * hidden * (1 - hidden)
    
    # Update weights
    w2 += learning_rate * hidden.T @ d_output
    w1 += learning_rate * X.T @ d_hidden
    
    if epoch % 2000 == 0:
        loss = np.mean(error ** 2)
        print(f"Epoch {epoch}: Loss = {loss:.4f}")

print("\nFinal predictions:")
print(output.round(2))

Why Deep Learning Works

You might wonder: why do deeper networks (more layers) work better? Each layer learns increasingly complex features:

| Layer | What It Learns | Example (Face Recognition) | |-------|---------------|---------------------------| | Layer 1 | Simple edges and lines | Vertical lines, horizontal lines | | Layer 2 | Simple shapes | Circles, corners, curves | | Layer 3 | Complex patterns | Eyes, noses, mouths | | Layer 4 | Complete features | Faces, expressions |

🎯 Activity: Look at the objects around you. Can you break them down into simple shapes like circles, rectangles, and lines? That's what early neural network layers do!

Common Challenges

Overfitting

When a network memorizes the training data instead of learning general patterns. It's like memorizing answers for a test without understanding the concepts.

Solutions:

  • More training data
  • Dropout (randomly turning off neurons during training)
  • Regularization

Vanishing Gradients

In very deep networks, gradients can become so small that early layers stop learning.

Solutions:

  • Skip connections (ResNet)
  • Better initialization
  • Batch normalization

What's Next?

Neural networks are the foundation of modern AI. From ChatGPT to self-driving cars, they power the most exciting technologies of our time.

In the next lesson, we'll explore convolutional neural networks — specialized networks that excel at understanding images!


"Neural networks are not magic. They're just math, beautifully arranged."