By the end
- Explain why stacked linear layers remain linear without an activation.
- Compare ReLU, sigmoid, and tanh output ranges.
- Build an MLP with `nn.Sequential`.
- Relate width and depth to parameter count and overfitting risk.
Week 7 · Hands-on foundations
What changes when a linear model gains hidden layers and nonlinear activations?
The current Week 7 material covers theory, from-scratch MLPs, concise implementations, deeper networks, activations, regression, and project applications in one large notebook. The two clean 2025 notebooks provide a better teaching spine: first understand hidden layers and activations, then implement one compact network.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Hold out validation data and compare ReLU, tanh, and a linear model using identical seeds and training budgets.
Core ideas
A hidden layer transforms raw features into learned intermediate features. These representations are useful only if the end-to-end task supplies evidence that they improve held-out behavior.
Without a nonlinear activation, several affine layers collapse into one affine transformation. Activations let the network bend and combine decision regions.
Width controls units per layer; depth controls stacked transformations. Both increase capacity and optimization choices, so a simple baseline remains important.
Mathematical intuition
The activation φ is the key difference from one linear transformation. Its position and range shape both representation and gradient flow.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
import math
x = -1.0
relu = max(0, x)
sigmoid = 1/(1+math.exp(-x))
tanh = math.tanh(x)
print(round(relu,3), round(sigmoid,3), round(tanh,3))Predict the result, then click Run.
inputs, hidden, outputs = 4, 8, 3
params = inputs*hidden + hidden + hidden*outputs + outputs
print(params)Predict the result, then click Run.
w1, b1, w2, b2, x = 2, 1, 3, -2, 4
stacked = w2*(w1*x+b1)+b2
collapsed = (w2*w1)*x + (w2*b1+b2)
print(stacked, collapsed)Predict the result, then click Run.
Failure checks
What goes wrong: Several layers still represent one linear map.
Check: Inspect the model sequence.
What goes wrong: A unit receives negative inputs and outputs zero throughout training.
Check: Inspect activation distributions or try a smaller rate/better initialization.
What goes wrong: Large inputs produce tiny gradients.
Check: Standardize inputs and compare activation ranges.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. The two clean 2025 notebooks provide a better teaching spine: first understand hidden layers and activations, then implement one compact network.
Standard answer: A hidden layer transforms raw features into learned intermediate features. These representations are useful only if the end-to-end task supplies evidence that they improve held-out behavior.
Standard answer: Without a nonlinear activation, several affine layers collapse into one affine transformation. Activations let the network bend and combine decision regions.
Standard answer: Width controls units per layer; depth controls stacked transformations. Both increase capacity and optimization choices, so a simple baseline remains important.
Standard answer: The activation φ is the key difference from one linear transformation. Its position and range shape both representation and gradient flow. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Keep the Week 6 split and metric contract unchanged.
Standard answer: Inspect the model sequence.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 8 turns MLP components into maintainable models: inspecting parameters, saving state, debugging shapes, and preparing a small project.
Terminology
Go further
Next: Week 8 turns MLP components into maintainable models: inspecting parameters, saving state, debugging shapes, and preparing a small project.