Intro to Deep Learning: lesson 2 of 4

Intro to Deep Learning

PATH 02MODULE 09LESSON 02 OF 04Next: NLP and Computer Vision: What AI Models Solve

Neural Networks and Deep Learning Intuition

Explain what neural networks learn and when depth changes the approach.

Intermediate17 min readdeep-learningneural-networksbackpropagationgradient-descent

Concept

A neural network is a mathematical model that transforms inputs through layers of learned calculations. Like earlier models, it receives features, produces a prediction, compares that prediction with a target during training, and adjusts learned parameters to reduce loss. Its distinguishing feature is that several nonlinear layers can learn useful intermediate representations.

A Simple Neuron

For a churn example, inputs might be tenure, monthly_charge, and support_tickets. One unit combines them as:

z = w1*x1 + w2*x2 + w3*x3 + b
output = activation(z)

The weights (w) determine how strongly the inputs contribute to this unit. The bias (b) shifts the calculation. An activation function transforms the result. These are learned mathematical parameters, not miniature biological brains.

Layers and Nonlinearity

input features
      ↓
hidden layer(s)
      ↓
output prediction

The input layer receives the representation of a row. Hidden layers calculate intermediate values. The output layer produces a number, probability-like score, or class-related output depending on the task.

Stacking only linear calculations would still behave like one larger linear transformation. Nonlinear activations let a network represent more complex relationships. ReLU is a common simple activation that keeps positive values and turns negative values into zero. A sigmoid-style output can be used when a binary probability-like output is needed. The architecture must fit the task; there is no universally best activation or number of layers.

Forward Pass, Loss, and Learning

The forward pass is input data flowing through the current layers to create a prediction. During supervised training, the prediction is compared with the known target using a loss: a quantity that indicates how wrong the current parameters are. This connects directly to residual and loss intuition from Regression and Classification.

The training loop is:

  1. Run a forward pass.
  2. Calculate loss against known targets.
  3. Compute how parameters contributed to that loss.
  4. Update parameters slightly.
  5. Repeat over training data.

Backpropagation efficiently computes the gradients: information about how changing each weight or bias would change loss. It is not itself the optimizer. Gradient descent uses those gradients to update parameters toward lower loss.

Learning Rate, Batches, and Epochs

The learning rate is the update step size. If it is too large, training can overshoot or become unstable; if too small, progress can be very slow. A batch is a subset of training examples processed before an update. An epoch is one pass through the training dataset. Training normally uses many batches across multiple epochs.

These settings affect learning behavior, but a decreasing training loss alone is not proof of a useful model. Validation evidence is still needed to detect overfitting and assess generalization.

Training vs Inference

During training, the network uses feature rows and known targets to learn weights. During inference, its learned weights are normally fixed: new feature rows pass forward through the network to produce predictions. Inference does not usually update weights. This is the same separation between fit() and predict() established in Module 01, expressed for neural networks.

Deep Dive

Deep Dive: Read Training as Evidence

One training step is iterative adjustment, not one-step memorization: inputs move through the network in a forward pass, the network produces predictions, loss measures the error, backpropagation computes how each parameter contributed to that loss, and an optimizer updates parameters in a direction intended to reduce it. A gradient is local directional information: if this weight changes slightly, how would loss change? The optimizer uses many such signals, but neural-network optimization is non-convex and does not guarantee a global minimum.

The learning rate controls update size. If it is too small, loss may improve so slowly that training wastes many epochs. If it is too large, updates can overshoot useful regions and loss may oscillate or diverge. For example, a smooth run such as 0.90 -> 0.72 -> 0.61 -> 0.54 is different from 1.10 -> 0.65 -> 1.40 -> 0.80 -> 2.30, which may suggest an overly aggressive learning rate or another optimization, numerical, or data problem. Training curves are evidence, not perfect diagnostic tests.

A batch estimates the gradient from a subset of examples. Smaller batches produce noisier gradient estimates and more updates per pass through the data; larger batches give smoother estimates with different memory and compute trade-offs. Batch size can change training behavior, so it is another hypothesis to test deliberately rather than a magic setting.

Diagnose Training and Validation Curves

Observed patternPossible interpretationWhat to investigate
Training loss falls and validation loss fallsLearning and generalization are improving so far.Continue monitoring on a valid validation set.
Training loss falls while validation loss risesPossible overfitting.Early stopping, regularization, representative data, capacity, and validation design.
Both losses stay high and nearly flatUnderfitting, optimization, input, label, or data issue.Learning rate, training duration, capacity, input representation, labels, and regularization strength.
Training loss oscillates or divergesPossible optimization instability.Learning rate, numerical/data issues, loss setup, and batch behavior.
Training performance is much better than validationGeneralization, split, leakage, or distribution-mismatch problem.Data quality, split design, class balance, duplicates, and model complexity.

Underfitting means the model fails to capture enough useful structure even on training data. Causes can include limited capacity, weak inputs, excessive regularization, early stopping, or unsuccessful optimization. Do not automatically make the network larger. Overfitting means training fit keeps improving while unseen-data behavior worsens. Early stopping uses validation behavior to stop when further training no longer helps generalization, but it cannot substitute for a valid validation set.

Weight decay, dropout, early stopping, and data augmentation can all reduce reliance on overly specific training patterns. Too much regularization can also cause underfitting. Diagnose the curve and data before choosing a response.

Decision Lab

Decision Lab: Training Improves, Generalization Does Not

A neural-network classifier reports:

EpochTraining lossValidation loss
10.720.75
50.460.51
100.290.48
200.140.61

At epoch 20, training accuracy is 98% and validation accuracy is 84%. The validation loss improved through about epoch 10, then worsened while training loss continued to fall. This is consistent with overfitting; higher training accuracy does not mean the model became more useful. More epochs are unlikely to repair this pattern by themselves.

Keep the best validation checkpoint, consider early stopping or appropriate regularization, and inspect representative data, labels, class balance, leakage, duplicates, and validation design before changing architecture. A simpler network, more representative data, or an adjusted training process may be better hypotheses than immediately adding depth.

Sanity Checks Before Architecture Changes

Start with the data and training setup: verify labels align with inputs, inspect missing or corrupt values, scaling, class balance, duplicates, and train-validation mismatch. Compare with a sensible baseline such as a majority-class rule, linear/logistic model, tree-based model, or naive forecast. A complex network that barely beats a baseline has not earned its complexity.

A useful debugging check is whether a sufficiently flexible network can fit a very small training subset. If it cannot, investigate optimization, model wiring, labels, preprocessing, or loss configuration. Fitting that tiny subset is a debugging test, not the final goal. Change one important variable at a time; if learning rate, architecture, dropout, optimizer, and batch size all change together, an improved result does not reveal why.

Parameter initialization, batch ordering, and stochastic optimization can create run-to-run variation. One successful run is weak evidence. When the decision matters, repeat experiments or control randomness before drawing conclusions.

Failure Signals

Training Warning Signs

Investigate loss that never decreases, oscillates, or diverges; falling training loss with worsening validation loss; both losses remaining poor; a network that cannot beat a simple baseline or fit a tiny debugging subset; dramatic run-to-run variation; suspiciously perfect performance; or predictions collapsing to one class or value. Each signal can have several causes, so follow evidence rather than a one-parameter recipe.

Check Your Reasoning

Check Your Reasoning

Question: A neural-network demand model has steadily falling training loss, unchanged validation loss for 15 epochs, and a Random Forest baseline that performs better on validation data. The team proposes doubling network depth. What should be investigated first?

Reasoning: First determine whether the pattern is underfitting, an optimization issue, weak input representation, excessive regularization, poor validation quality, or data/label trouble. Inspect learning rate and curves, data scaling and features, the baseline comparison, and whether the current network can learn a tiny subset. Extra architecture is justified only if these checks support a capacity limitation and held-out evidence improves.

Practice and Build

Practice Connection

Use Training Loss Falls, Validation Loss Rises for the direct early-stopping diagnosis and Diagnose an Overfitting Model for a broader train-validation investigation. The current Build catalog has no deep-learning-specific project, so this module has Practice reinforcement but no dedicated Build extension.

Why Depth Can Help, and What It Costs

Multiple layers can build richer representations from complex inputs. For images, local visual patterns can be combined into more useful internal features. For text, learned representations can help capture context and patterns. More depth and parameters can also require more data, compute, tuning, memory, and careful validation.

Large flexible networks can overfit. Introductory responses include validation monitoring, early stopping, regularization, and dropout, but the important principle is familiar: capacity must be justified by unseen-data performance.

Failure Signals

Common Mistakes

  1. Saying backpropagation is the same thing as gradient descent.
  2. Treating a lower training loss as enough evidence of generalization.
  3. Assuming inference keeps learning from each new input.
  4. Believing deeper means automatically better.
  5. Treating weights as simple causal effects of individual features.

Best Practices

Keep the task, target, split strategy, and evaluation criteria clear before selecting a network. Track validation behavior, use reproducible experiments, and compare against sensible classical baselines. This lesson is intentionally framework-independent: understanding the training loop matters before an API.

Interview Perspective

Question: What does backpropagation do?
Answer: It computes how model parameters affect loss so an optimizer such as gradient descent can update them.
What the interviewer is testing: whether you distinguish gradients from the update procedure.
Follow-up: What can happen when the learning rate is too large?

Practice Questions

  1. Label the weights, bias, and activation in the neuron equation.
  2. Why are nonlinear activations needed between layers?
  3. Put forward pass, loss calculation, backpropagation, and parameter update in order.
  4. Is a new customer prediction training or inference? Why?
  5. A network has falling training loss and rising validation loss. What risk does this suggest?

Quick Quiz

  1. What is an epoch? Answer: One pass through the training dataset.
  2. What is a batch? Answer: A subset processed before an update.
  3. Does backpropagation directly choose the step size? Answer: No; it computes gradients.

Key Takeaway

Key Takeaways

Neural networks learn weights and biases through forward passes, loss, backpropagation, and gradient-based updates. Depth and nonlinear activations can learn richer representations, but validation and practical constraints still determine whether the model is useful.

Next Lesson

Next, see how learned representations support language and image tasks without turning this module into an NLP or computer-vision implementation course.

Finish this lesson on your terms

Mark it complete when you have worked through the material and are ready to move on.