AI: Introduction to Neural Networks

Neural networks are the computational architecture behind modern AI. Built from layers of interconnected neurons — input, hidden, and output — they learn by adjusting weights through backpropagation. Understanding architecture, weights, loss functions, learning rate, and overfitting explains how neural networks turn raw data into intelligent predictions.

Neural networks are the foundation of deep learning, and deep learning is the foundation of the modern AI revolution. Every major breakthrough in AI over the last decade — image recognition, language models, game-playing agents — is built on neural networks.

Understanding how they work, from the architecture to the training process, demystifies what would otherwise appear to be pure black-box magic.


What Is a Neural Network?

A neural network is a computational system loosely modeled on the biological brain. It consists of layers of interconnected neurons (nodes), where each neuron takes some inputs, performs a mathematical operation, and passes its output to the next layer.

md
Neural Network Architecture

Input Layer   Hidden Layer(s)   Output Layer

  [X1] ---\
           +--> [H1] --\
  [X2] ---/             +--> [H3] --\
           +--> [H2] --/             +--> [Output]
  [X3] ---/

Every neuron in one layer connects to every neuron in the next layer.

A neural network contains three types of layers:

  1. Input layer — where data enters
  2. Hidden layers — where the computation happens
  3. Output layer — where predictions are produced

The Input Layer

The input layer is where the neural network receives data, represented as numbers.

Each input neuron represents a single feature — a characteristic of the data.

md
Image Classification Example

Input layer for a 4-pixel image:

Pixel 1 brightness  --> [Input Neuron 1]
Pixel 2 brightness  --> [Input Neuron 2]
Pixel 3 brightness  --> [Input Neuron 3]
Pixel 4 brightness  --> [Input Neuron 4]

All data must be numerical.
Text, images, audio -- all converted to numbers first.

Real-world inputs can be:

  • Pixel values in an image (flattened to a vector)
  • Word embeddings in text
  • Sensor readings in time series
  • Features extracted from structured data

The Hidden Layer

Once the input layer has data, each input neuron sends its value to every neuron in the first hidden layer.

Each hidden layer neuron:

  1. Receives all inputs from the previous layer
  2. Multiplies each input by a weight (its learned importance)
  3. Sums all the weighted inputs
  4. Applies an activation function to produce its output
  5. Sends that output to every neuron in the next layer

md
Single Hidden Neuron Math

Inputs:  x1 = 0.5,  x2 = 0.3,  x3 = 0.8
Weights: w1 = 0.4,  w2 = 0.7,  w3 = 0.2

Weighted sum = (x1 * w1) + (x2 * w2) + (x3 * w3)
             = (0.5 * 0.4) + (0.3 * 0.7) + (0.8 * 0.2)
             = 0.20 + 0.21 + 0.16
             = 0.57

Apply activation function to 0.57 --> output value

The goal of each hidden layer neuron is to measure whether the input data has a certain component — an edge in an image, a grammatical pattern in text, a correlation between features.


The Output Layer

The output layer is where the final hidden layer outputs are combined to produce the answer.

The structure of the output layer depends on the problem:

Problem TypeOutput Layer Structure
Binary classification (dog or not dog)1 neuron (0 or 1)
Multi-class classification (cat, dog, car)N neurons, one per class
Regression (predict a price)1 neuron (continuous value)

md
Multi-Class Output Layer Example (Image Classification)

Last Hidden Layer
       |
       v
+----------+  +----------+  +----------+
|  Cat     |  |  Dog     |  |  Car     |
| 0.15     |  | 0.72     |  | 0.13     |
+----------+  +----------+  +----------+
           
The network picks the highest probability:
Dog (0.72) --> Prediction: "Dog"

The network makes its decision based on which output neuron has the highest activation (probability).


Deep Neural Networks

A deep neural network is a network with multiple hidden layers. The field of using deep neural networks is called deep learning.

md
Shallow vs Deep Network

Shallow:
Input -> [Hidden] -> Output

Deep:
Input -> [H1] -> [H2] -> [H3] -> [H4] -> Output
         Basic  Edges  Shapes  Objects
         patterns

Each additional layer allows the network to combine the outputs of the previous layer into increasingly complex representations:

  • Layer 1 might detect edges in an image
  • Layer 2 combines edges into shapes
  • Layer 3 combines shapes into object parts
  • Layer 4 combines object parts into recognizable objects

Deeper networks can solve more complex problems — but they also require:

  • More training data
  • More computing power
  • More careful training to avoid problems like overfitting

Two Components of Every Neural Network

Every neural network has two essential parts:

ComponentDescription
ArchitectureThe neurons and their connections — how many layers, how many neurons per layer, how they connect
WeightsNumbers that fine-tune how each neuron does its math to get an output

The architecture defines the structure. The weights encode the learned knowledge — they are what change during training and what make the network intelligent.


Backpropagation: Learning from Mistakes

When a neural network makes a wrong prediction, all the neurons that contributed to the error need their weights adjusted. Backpropagation is the algorithm that does this.

md
Backpropagation Flow

Forward Pass:
Input --> Hidden Layers --> Output --> Prediction

Check:
Prediction vs. Correct Answer --> Calculate Error

Backward Pass:
Error flows BACKWARDS through the network.
Each neuron gets "blamed" proportionally.
Weights are adjusted to reduce the error.

Repeat thousands/millions of times.

The basic goal: assign blame to neurons in previous layers for the error. Some neurons may have contributed more to the error than others — their weights will be adjusted more. This information flows backwards through the network, adjusting weights layer by layer.


Optimization: Finding the Best Weights

The task of finding the best weights for a neural network architecture is called optimization.

Linear Regression

One foundational optimization technique is linear regression:

  1. Start with a random line (or hyperplane) through the data
  2. Calculate the total distance between the line and all data points (the error)
  3. Adjust the line to reduce the error
  4. Repeat until the error is minimized

md
Linear Regression Optimization

  Data points:  * * * * *
  Initial line: -----
  
  Error = sum of distances from line to each point
  
  Goal: Find the "line of best fit" -- minimize error

        * *
      *     *
    *---------*---  <-- Line of best fit
  *

The line that minimizes the total error is called the line of best fit.


The Loss Function

When there are multiple input features (and thus multiple output factors), the error cannot be represented by a single number. In these cases, the error is captured by a loss function.

The loss function measures the difference between the predicted answer and the correct answer across all dimensions.

md
Loss Function Purpose

Correct output: [0.1, 0.8, 0.1]  (true label: "Dog")
Predicted output: [0.3, 0.5, 0.2]

Loss = measure of how far off the prediction is
     = function that returns a single number representing total error

Goal: minimize the loss function through weight adjustments

Common loss functions:

Loss FunctionUse Case
Mean Squared Error (MSE)Regression problems
Cross-Entropy LossClassification problems
Binary Cross-EntropyBinary classification

Learning Rate

The learning rate controls how much the neuron weights get adjusted every time backpropagation happens.

md
Learning Rate Effect

High learning rate: Large weight updates
  + Fast initial learning
  - Risk of overshooting the optimal weights (oscillation)

Low learning rate: Small weight updates
  + Stable, precise convergence
  - Very slow training; may get stuck in local minima

Optimal learning rate: Balances speed and stability

Setting the learning rate correctly is one of the most important hyperparameter choices in training a neural network.


Overfitting: The Danger of Too Much Memorization

Over time, backpropagation adjusts neuron weights so the network's output matches the training data. But this can go too far.

Overfitting occurs when the network memorizes the training data instead of learning general patterns. When new data is presented that does not match the training distribution exactly, the network produces strange errors.

md
Overfitting vs. Good Fit

Underfitting:
  Model is too simple, misses the pattern.
  High error on both training and new data.

Good Fit:
  Model captures the real pattern.
  Low error on training data and new data.

Overfitting:
  Model memorizes training data (including noise).
  Low error on training data.
  High error on new data.

How to prevent overfitting:

  • Keep the neural network simple (fewer layers or neurons)
  • Use more training data
  • Apply dropout (randomly disable neurons during training)
  • Use regularization techniques (penalize overly complex models)
  • Split data into training and validation sets and monitor both

Summary of the Training Process

md
Neural Network Training Loop

1. Initialize random weights
2. Forward pass: feed data through the network
3. Calculate loss (error between prediction and truth)
4. Backward pass: backpropagate error through the network
5. Update weights using the learning rate
6. Repeat for thousands/millions of examples
7. Monitor validation loss to catch overfitting
8. Stop when the network performs well on unseen data


Final Thoughts

Neural networks are not mysterious — they are function approximators trained through millions of iterations of forward pass, error calculation, and weight adjustment.

The key concepts to internalize:

  • Architecture defines structure; weights encode knowledge
  • Backpropagation teaches the network by propagating errors backward
  • Loss functions measure how wrong the network is
  • Learning rate controls how fast the network learns
  • Overfitting happens when the network memorizes instead of generalizing

Understanding these concepts gives you the foundation to engage meaningfully with any neural network — whether you are reading research papers, using ML frameworks like PyTorch or TensorFlow, or debugging model behavior.

The network learns one small adjustment at a time. Your job is to set up the conditions for it to learn the right things.