Current: Week 3

0%

Week 3

Week 3 — Neural networks: from one neuron to multiple layers

Key questionHow do we extend one wx+b calculation into a multilayer network?

Learning objectives

  • Understand and apply Week 3 — Neural Networks: from One Neuron to Multiple Layers

190 min estimated reading time

Week progress: 0 of 75 sections (0%)Course progress: 0 of 321 sections (0%)

Last week, you learned to train a straight line. This week, keep the learning principle and ask a new question: if the target pattern is not a straight line, how should the prediction function change? Work through four units: one neuron → nonlinearity → a two-layer network → code and task-specific outputs.

Recall y_hat=wx+b: the input is the value supplied for this example, while w and b are the rule that training changes. A neural network connects multiple multiply-and-add units like this. The key new ingredient is not the terminology but the nonlinearity between layers. First run ReLU, then examine XOR, and finally calculate and implement a 2→2→1 network.

Scroll horizontally to view all columns.

Course data table
This week's experimentsFixed conditionsWhat to observe
Two-layer forward passInput [1,2]; two hidden units; a linear outputHidden values [1.4,1.1], prediction 1.3, and loss 0.49 when the target is 2.
Negative-output comparisonChange only the output weights to [−1,0] and bias to 0The prediction should be −1.4; a generic layer function must not silently clip it to 0.
XORTwo manually specified ReLU branchesAll four inputs can be calculated by hand. This demonstrates representational capacity, not a successful training run.

Run course_examples/week03_neuron.py; the separate XOR experiment is course_examples/week03_xor.py. By the end, explain parameter counts, hidden-layer activations, and the choice of output layer. You do not yet need to derive every gradient from scratch. Week 4 tackles how each parameter affects the final error.

Unit 1: from linear prediction to one neuron

1. First, why do we need neural networks?

In Week 2, we studied linear regression:

y^=wx+b\hat{y} = wx + b

For example, predicting house prices:

Price=w×Area+b\mathrm{Price} = w \times \mathrm{Area} + b

Suppose:

w = 5000

b = 100000

Then, for a 100 m² house:

Price=5000×100+100000\mathrm{Price} = 5000 \times 100 + 100000

We obtain:

$600,000\text{\$600,000}

This is a simple relationship:

Floor area increases

Price increases at an approximately constant rate

This is an approximately linear relationship.

But real-world patterns are often more complicated.

2. Real-world patterns often do not form a straight line

Predicting house prices might involve:

Floor area

Number of bedrooms

Age of the property

Distance to the central business district (CBD)

School catchment

Local crime rate

Land area

Renovation condition

Interest rates

Market conditions

...

These factors may also interact.

For example:

A very large house

+

Far from the CBD

It might still command a relatively low price.

But:

A large house

+

A sought-after school catchment

+

Close to the CBD

The combination might command a much higher price.

In other words:

Many real-world patterns are nonlinear.

Neural networks offer one way to model such patterns.

3. Start from Week 2's linear regression

Week 2:

y^=wx+b\hat{y} = wx + b

Now suppose there are three features:

  • x₁: floor area
  • x₂: number of bedrooms
  • x₃: distance to the CBD

We can write the model as:

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

It looks a little more complicated.

But the basic operation is unchanged.

Previously:

wx+bwx + b

Now we have:

w1x1+w2x2+w3x3+bw_1x_1 + w_2x_2 + w_3x_3 + b

That is:

Each input has its own weight.

4. What does a weight mean?

Weights are central to neural networks.

Suppose:

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

If:

x1 = floor area

x2 = number of bedrooms

x3 = distance to the CBD

Training might produce:

w1 = 0.8

w2 = 0.4

w3 = -0.7

Within this illustrative model, we can read the signs as follows:

Floor area → a positive contribution

Number of bedrooms → a positive contribution

Distance to the CBD → a negative contribution

For this simple linear model, think of a weight as:

The change in the prediction per unit increase in that input, holding the other inputs fixed. Its sign gives the direction. Its magnitude depends on feature units and scale, so weights alone do not provide a universal ranking of importance or establish causation.

In a large neural network, an individual weight usually has no such direct human-readable interpretation.

5. This is already the core of a neuron

The following computation:

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

is a basic building block of a neural network.

We can draw it as:

x1 ──w1 ──┐

x2 ──w2 ──┼── SUM ── z

x3 ──w3 ──┘

+

b

This is the simplest form of an:

Artificial Neuron

Artificial neuron.

6. Why call it a neuron?

The idea was inspired by biological neurons, but the two should not be treated as equivalent.

This artificial neuron's computation is straightforward:

Receive inputs

Multiply by weights

Add the products

Add a bias

Produce a number

In mathematical notation:

z=iwixi+bz = \sum_i w_ix_i + b

The symbol:

\sum

is a compact way to say “add all the terms.”

For example:

i=13wixi\sum_{i=1}^{3} w_i x_i

It means:

w1x1+w2x2+w3x3w_1x_1 + w_2x_2 + w_3x_3

Σ is simply shorthand for a sum.

7. Calculate one neuron by hand

Suppose we create an illustrative score for whether a house is worth considering.

Three inputs:

x1 = 0.8, a location feature

x2 = 0.6, a property-condition feature

x3 = 0.2, a distance-related feature

Weights:

w1 = 0.7

w2 = 0.5

w3 = -0.4

Bias:

b = 0.1

Calculate:

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

Substitute:

z=(0.7)(0.8)+(0.5)(0.6)+(0.4)(0.2)+0.1z = (0.7)(0.8) + (0.5)(0.6) + (-0.4)(0.2) + 0.1

Step by step:

0.7×0.8=0.560.7 \times 0.8 = 0.56
0.5×0.6=0.300.5 \times 0.6 = 0.30
0.4×0.2=0.08-0.4 \times 0.2 = -0.08

Therefore:

z=0.56+0.300.08+0.1z = 0.56 + 0.30 - 0.08 + 0.1
z=0.88z = 0.88

So far, this is just multiplication and addition.

It means:

z = x1*w1 + x2*w2 + x3*w3 + b

8. But there is an important limitation

If a neuron only computes:

z=wx+bz = wx + b

then even if we stack many layers:

Input

Linear

Linear

Linear

Output

the overall input–output function is still affine—a linear model with a bias.

This is a key point in understanding neural networks.

9. Why are stacked linear layers still linear?

Suppose the first layer is:

h=w1x+b1h = w_1x + b_1

The second layer is:

y=w2h+b2y = w_2h + b_2

Substitute the first layer:

y=w2(w1x+b1)+b2y = w_2(w_1x + b_1) + b_2

Expand:

y=w2w1x+w2b1+b2y = w_2w_1x + w_2b_1 + b_2

Treat:

w2w1w_2w_1

as a new weight:

WW

Treat:

w2b1+b2w_2b_1 + b_2

as a new bias:

BB

Finally:

y=Wx+By = Wx + B

We find that:

Two consecutive affine layers can be combined into one affine layer.

The same holds for 10 such layers.

And for 100 such layers.

Stacking wx+b operations alone does not add nonlinear representational capacity.

We need another ingredient.

The precise statement is about the input–output function: consecutive linear or affine layers can be collapsed into one, so they do not add nonlinear representational capacity. Splitting them into layers can change the parameterization and optimization dynamics, but depth alone cannot produce a nonlinear decision boundary in this case.

This example has one input and one output. Its combined W and B are scalars, not the later weight matrix W or batch size B. Scalar multiplication allows Wx=xW. With multi-feature matrices, multiplication order must match the shapes.

Unit 2: observe ReLU, then understand nonlinearity

10. Activation Function

That ingredient is:

Activation Function

An activation function.

A neuron no longer computes only:

z=wx+bz = wx + b

Instead:

a=f(z)a = f(z)

The full calculation:

a=f(wx+b)a = f(wx + b)

Here:

  • z: the result of the affine multiply-and-add calculation
  • f:Activation Function
  • a:activation/output

12. A simple activation: ReLU

A widely used activation in neural networks:

ReLU

Full name:

Rectified Linear Unit

Formula:

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

It may look like abstract notation.

But translated into code, it is:

python
def relu(x):
    return max(0, x)

The operation itself is this small.

13. What does ReLU do?

If:

x = 5

Then:

ReLU(5)=5\operatorname{ReLU}(5) = 5

If:

x = -3

Then:

ReLU(3)=0\operatorname{ReLU}(-3) = 0

If:

x = 0.7

Then:

ReLU(0.7)=0.7\operatorname{ReLU}(0.7) = 0.7

Therefore:

negative → 0

positive → keep it

Sketch of its graph:

y

/

/

/

/

│___/____________ x

0

11. Why do activation functions matter?

Because a nonlinear activation introduces:

Non-linearity

Nonlinearity.

The model is no longer restricted to a single affine input–output relationship.

For example, a relationship might have this shape:

*

* *

*

*

*

*

*

*

Or:

________

\

\

\_______

Or a much more complicated boundary.

Nonlinear components are necessary for a neural network to represent these nonlinear relationships.

14. A complete neuron with an activation

Before:

Inputs

Weights

Sum

Bias

Output

Now:

Inputs

Weights

Weighted Sum

Bias

z = wx+b

Activation Function

a = ReLU(z)

Output

Mathematically:

a=ReLU(wx+b)a = \operatorname{ReLU}(wx + b)

15. Calculate a neuron with ReLU

Suppose:

x1=2x_1 = 2
x2=3x_2 = 3

Weights:

w1=0.5w_1 = 0.5
w2=0.8w_2 = -0.8

Bias:

b=0.2b = 0.2

First calculate:

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

Therefore:

z=(0.5)(2)+(0.8)(3)+0.2z = (0.5)(2) + (-0.8)(3) + 0.2
=12.4+0.2= 1 - 2.4 + 0.2
=1.2= -1.2

Then apply the activation:

a=ReLU(1.2)a = \operatorname{ReLU}(-1.2)

We obtain:

a=0a = 0

This neuron produces zero for the current input.

16. What if we change a weight?

For example, replace:

w2=0.8w_2 = -0.8

with:

w2=0.2w_2 = 0.2

Recalculate:

z=(0.5)(2)+(0.2)(3)+0.2z = (0.5)(2) + (0.2)(3) + 0.2
=1+0.6+0.2= 1 + 0.6 + 0.2
=1.8= 1.8

ReLU:

ReLU(1.8)=1.8\operatorname{ReLU}(1.8) = 1.8

Therefore:

a=1.8a = 1.8

This illustrates an important point:

Weights determine how the neuron responds to inputs.

Training adjusts those weights using data and the chosen loss.

The learning principle is the same as in Week 2.

17. What can one neuron do?

A single neuron still has limited representational capacity.

Greater flexibility comes from:

Multiple neurons working together.

For example:

x1 ─┬── Neuron 1

├── Neuron 2

└── Neuron 3

x2 ─┬── Neuron 1

├── Neuron 2

└── Neuron 3

Such a group of neurons forms a:

Layer

Layer

Unit 3: calculate a complete two-layer network

18. What is a layer?

Suppose the input is:

x=[x1,x2]x = [x_1,x_2]

The hidden layer has three neurons:

┌─Neuron 1

x1 ─────┼─Neuron 2

└─Neuron 3

x2 ───────┘

Each neuron has its own weights.

Neuron 1:

z1=w11x1+w12x2+b1z_1 = w_{11}x_1 + w_{12}x_2 + b_1

Neuron 2:

z2=w21x1+w22x2+b2z_2 = w_{21}x_1 + w_{22}x_2 + b_2

Neuron 3:

z3=w31x1+w32x2+b3z_3 = w_{31}x_1 + w_{32}x_2 + b_3

Each result then passes through ReLU:

a1=ReLU(z1)a_1 = \operatorname{ReLU}(z_1)
a2=ReLU(z2)a_2 = \operatorname{ReLU}(z_2)
a3=ReLU(z3)a_3 = \operatorname{ReLU}(z_3)

The layer therefore outputs:

[a1  a2  a3][a_1\; a_2\; a_3]

Here, the Python weights store one output neuron's input weights in each row, giving [outputs,inputs]. Week 1's XW+b stores the same numbers with each output in a column of W, giving [inputs,outputs]. The later nn.Linear example uses the first storage convention too. The two weight arrays are transposes of each other.

19. Why put multiple neurons in a layer?

Different neurons can learn different responses or patterns.

To build intuition, consider image recognition.

Some neurons might become responsive to:

Vertical lines

in a particular arrangement.

Others might respond to:

Horizontal lines

Others might respond to:

Edges

Later layers may combine earlier responses into patterns associated with:

Eyes

Ears

Outlines

Further combinations might be associated with:

A cat's face

This is not a programmer explicitly writing:

python
if has_whiskers and has_ears:
    return "cat"

Rather, training adjusts weights to reduce the chosen loss. This feature hierarchy is an illustrative possibility, not a guaranteed interpretation of every trained neuron.

20. What is a hidden layer?

A typical neural network:

Input Layer

Hidden Layer 1

Hidden Layer 2

Output Layer

Why “hidden”?

These layers are neither the original input nor the final answer.

They contain the model's internal:

intermediate representations

Intermediate representations.

21. Feature Learning

Learning intermediate representations is an important feature of neural networks.

For example, suppose the input is an image of a cat.

The raw data may simply be:

pixel

pixel

pixel

pixel

...

The model transforms these values layer by layer:

Pixels

Edges

Shapes

Parts

Object concepts

Cat

This introduces an important AI concept:

“This neuron might detect an ear” is an analogy for combining features, not a promise that every trained unit has a clear human-readable label. Representations are often distributed across many units. A single weight or activation is not enough to prove that the model has learned a real-world concept.

Representation

The model transforms data into representations that can be more useful for the task.

When you later study:

  • Embedding
  • Attention
  • Transformer

you will encounter this idea repeatedly.

22. What does “deep” mean in deep learning?

Suppose:

Input

Hidden

Output

A small number of layers.

If:

Input

Layer 1

Layer 2

Layer 3

Layer 4

Layer 5

...

Output

Many layers.

This is called:

Deep Neural Network

Therefore:

In deep learning, “deep” mainly refers to having multiple layers of learned transformations.

It does not mean “the AI is thinking deeply.”

23. Calculate a small neural network by hand

We will build a very small network:

x1 ───── h1 ───┐

\

\

├── output

/

/

x2 ───── h2 ────┘

Two inputs:

x1=1x_1 = 1
x2=2x_2 = 2

Two neurons in the hidden layer.

24. Hidden Neuron 1

Weights:

w11=0.5w_{11} = 0.5
w12=0.4w_{12} = 0.4

Bias:

b1=0.1b_1 = 0.1

Calculate:

z1=0.5(1)+0.4(2)+0.1z_1 = 0.5(1) + 0.4(2) + 0.1
=0.5+0.8+0.1= 0.5 + 0.8 + 0.1
=1.4= 1.4

After ReLU:

h1=ReLU(1.4)h_1 = \operatorname{ReLU}(1.4)

Therefore:

h1=1.4h_1 = 1.4

25. Hidden Neuron 2

Weights:

w21=0.3w_{21} = -0.3
w22=0.8w_{22} = 0.8

Bias:

b2=0.2b_2 = -0.2

Calculate:

z2=(0.3)(1)+(0.8)(2)0.2z_2 = (-0.3)(1) + (0.8)(2) - 0.2
=0.3+1.60.2= -0.3 + 1.6 - 0.2
=1.1= 1.1

ReLU:

h2=ReLU(1.1)h_2 = \operatorname{ReLU}(1.1)

Therefore:

h2=1.1h_2 = 1.1

Hidden-layer output:

[1.4  1.1][1.4\; 1.1]

26. Notice what changed

Original input:

[1  2][1\; 2]

After the hidden layer:

[1.4  1.1][1.4\; 1.1]

In other words:

Original representation

[1, 2]

↓Neural Network

New representation

[1.4, 1.1]

This is the idea of:

Representation Transformation

Transformers also repeatedly transform intermediate representations.

27. Output Layer

The output neuron now receives:

h1 = 1.4

h2 = 1.1

Suppose:

v1=0.7v_1 = 0.7
v2=0.2v_2 = 0.2

Bias:

b=0.1b = 0.1

Calculate:

y=0.7h1+0.2h2+0.1y' = 0.7h_1 + 0.2h_2 + 0.1

Substitute:

=0.7(1.4)+0.2(1.1)+0.1= 0.7(1.4) + 0.2(1.1) + 0.1
=0.98+0.22+0.1= 0.98 + 0.22 + 0.1
y=1.30y' = 1.30

This completes one:

Knowledge check

If the target can be −2, what limitation would ReLU impose on the final layer?

Forward Pass

Forward Pass

28. The complete forward pass

What we just did was:

x1 = 1

x2 = 2

┌──────────────┐

│Hidden Layer │

│h1 = 1.4

│h2 = 1.1

└──────────────┘

┌──────────────┐

│Output Layer │

│y' = 1.30

└──────────────┘

That ingredient is:

Concept sequence
  1. Input
  2. Calculate one layer after another
  3. Prediction

29. We have a prediction—what comes next?

Now reconnect this to Week 2.

Suppose the correct answer is:

y=2y = 2

The model predicts:

y^=1.3\hat{y} = 1.3

Error:

1.32=0.71.3 - 2 = -0.7

Using squared error:

Loss=(1.32)2\mathrm{Loss} = (1.3 - 2)^2
=(0.7)2= (-0.7)^2
0.490.49

What comes next?

The same learning sequence as in Week 2:

Prediction

Loss

Gradient

Update Weights

Except that the parameters are no longer just:

w

b

Instead:

w11

w12

w21

w22

b1

b2

v1

v2

b3

...

30. This is neural-network training

The complete process:

INPUT

Hidden Layer

Hidden Layer

Output

Prediction

Loss

Gradients

Update ALL Weights

└──────────────┐

Next iteration

The central principle from Week 2 has not changed:

Prediction  Loss  Gradient  Update\text{Prediction } \rightarrow \text{ Loss } \rightarrow \text{ Gradient } \rightarrow \text{ Update}

31. How do we get gradients for so many weights?

Week 2:

y^=wx+b\hat{y} = wx + b

With only:

w

b

differentiation is relatively manageable.

But in a neural network:

x

w1

Neuron

w2

Neuron

w3

Output

Loss

we need to calculate:

Lossw1\frac{\partial\,\mathrm{Loss}}{\partial w_1}
Lossw2\frac{\partial\,\mathrm{Loss}}{\partial w_2}
Lossw3\frac{\partial\,\mathrm{Loss}}{\partial w_3}

Potentially for millions or billions of parameters.

How can we do that?

The answer is:

Backpropagation

This is the focus of Week 4.

32. Before backpropagation, understand the computational graph

Suppose:

x=2x = 2
w=3w = 3

First:

z=xwz = xw

Then:

y=z+1y = z + 1

Code:

python
z = x * w
y = z + 1

We can draw it as:

x ──┐

├──× ── z ── +1 ── y

w ──┘

This is called a:

Computational Graph

Computational graph.

A neural network can be represented as a large computational graph of dependent operations.

33. Why is the computational graph useful?

During training:

Forward

Following dependencies forward:

Input

Calculation

Calculation

Prediction

Loss

Backward

Working backward from loss:

Loss

gradient

gradient

gradient

weights

Using what we already saw in Week 2:

Chain Rule

we calculate gradients through the graph.

That ingredient is:

Backpropagation applies the chain rule backward through a computational graph, propagating and combining gradient contributions.

In Week 4, we will calculate this by hand.

34. Why introduce matrices?

So far, we have written:

w1x1+w2x2+w3x3w_1x_1 + w_2x_2 + w_3x_3

But what if there are:

1000 inputs

1000 neurons

Would we write:

neuron1 = x1*w11 + x2*w12 + ...

neuron2 = x1*w21 + x2*w22 + ...

...

No—we can organize the values.

Write the inputs as:

XX

Write the weights as:

WW

The whole layer can then be calculated as:

Z=XW+bZ = XW + b

Then apply the activation:

A=ReLU(Z)A = \operatorname{ReLU}(Z)

This is the kind of operation you will see in PyTorch.

35. Matrices organize the same arithmetic

For example:

X=[1  2]X = [1\; 2]

Weights:

W=[0.50.30.40.8]W = \begin{bmatrix} 0.5 & -0.3 \\ 0.4 & 0.8 \end{bmatrix}

The operation simply takes what we just calculated:

Neuron 1:

1×0.5 + 2×0.4

Neuron 2:

1×(-0.3) + 2×0.8

and computes many such results together.

Matrix multiplication is not an extra complication invented for neural networks.

It gives us a way to:

Compute many neurons in one operation.

36. Matrices from a programmer's perspective

For now, you can read:

XWXW

as an efficiently implemented version of:

python
for neuron in neurons:
    result = 0
    for input_value, weight in zip(inputs, neuron.weights):
        result += input_value * weight

GPUs are well suited to large, parallel matrix multiplications.

This is one reason:

GPUs are important in AI.

Unit 4: code, output choices, and independent practice

37. Implement one neuron in plain Python

Without PyTorch:

python
def relu(x):
    return max(0, x)


def neuron(inputs, weights, bias, activation=relu):
    if len(inputs) != len(weights):
        raise ValueError("Each input must have one corresponding weight")
    total = sum(x * w for x, w in zip(inputs, weights)) + bias
    return total if activation is None else activation(total)

inputs = [1, 2]
weights = [0.5, 0.4]
bias = 0.1
output = neuron(inputs, weights, bias)
print(output)

We obtain:

1.4000000000000001

This matches the earlier hand calculation.

activation=relu uses ReLU when the argument is omitted; activation=None returns the affine result directly. This function argument selects the computation—it is not a learned parameter like weights or bias. The printed 1.4000000000000001 is a floating-point approximation; 1.4 is sufficient for the hand calculation.

38. Implement a layer

The complete definitions below can be copied together. Run the subsequent hidden-layer example underneath them. Every output neuron receives the same inputs but has its own weight row and bias.

Now use several neurons:

python
def relu(x):
    return max(0, x)


def neuron(inputs, weights, bias, activation=relu):
    if len(inputs) != len(weights):
        raise ValueError("Each input must have one corresponding weight")
    total = sum(x * w for x, w in zip(inputs, weights)) + bias
    return total if activation is None else activation(total)


def layer(inputs, weights, biases, activation=relu):
    if len(weights) != len(biases):
        raise ValueError("Each output neuron must have one bias")
    return [neuron(inputs, row, bias, activation)
            for row, bias in zip(weights, biases)]

Define two neurons:

python
inputs = [1, 2]
weights = [
    [0.5, 0.4],
    [-0.3, 0.8],
]
biases = [0.1, -0.2]

hidden = layer(inputs, weights, biases)
print(hidden)

We obtain:

[1.4000000000000001, 1.1]

This reproduces our earlier hand calculation.

39. Connect the output layer

python
# Continue after Section 38; hidden is approximately [1.4, 1.1].
output_weights = [[0.7, 0.2]]
output_biases = [0.1]
prediction = layer(hidden, output_weights, output_biases, activation=None)
print(prediction)  # approximately [1.3]

# Counterexample: this regression output must allow negative values.
negative_prediction = layer(hidden, [[-1.0, 0.0]], [0.0], activation=None)
print(negative_prediction)  # approximately [-1.4], not [0]

Conceptually:

[1, 2]

Hidden Layer

[1.4, 1.1]

Output Layer

Prediction

We have implemented a very small:

The first parameters give 1.4×0.7+1.1×0.2+0.1=1.3. Because this is positive, mistakenly adding ReLU would not expose the bug. The second set deliberately gives −1.4, revealing whether the output is incorrectly clipped to 0. See course_examples/week03_neuron.py for the complete version.

Neural Network

It does not yet perform training.

Week 4 supplies the gradient calculations needed for training.

40. What does a neural network actually learn?

This distinction is essential.

We might say:

The AI learned to recognize a cat.

This can be a useful high-level description of its behavior.

But what actually changes underneath?

The quantities changed during this training are:

WW

and:

bb

That is, many:

Parameters

For example:

0.12531

-0.88211

0.00341

1.27342

...

A model can contain billions of these numbers.

When we say:

The model has learned knowledge

at the implementation level, much of what we mean is:

Training has adjusted a collection of parameter values that determine the model's behavior.

41. Why can more parameters increase capacity?

Linear Regression:

2 parameters

w

b

A small neural network might have:

Hundreds

Or thousands of parameters

Large deep-learning models:

Millions

Billions

Within a suitable architecture, more parameters can provide additional capacity to represent complex relationships.

But remember:

More parameters do not automatically mean better performance or greater intelligence.

Results also depend on:

  • Sufficient, suitable data
  • An appropriate architecture
  • Effective training
  • optimization
  • regularization
  • compute

and other choices.

42. How do we count parameters?

This is a useful practical calculation.

Suppose a layer has:

Input size = 3

Neuron count = 4

For each neuron:

3 weights

1 bias

Therefore:

4 parameters per neuron

In total:

4 neurons × 4

= 16 parameters

Formula:

Parameters=(Inputs×Neurons)+Neurons\text{Parameters}=(\text{Inputs}\times\text{Neurons})+\text{Neurons}

That is:

3×4+4=163\times4+4=16

The +4 accounts for four biases.

43. Another parameter-count example

Suppose:

Input features = 100

Hidden neurons = 256

Weights:

100×256=25,600100\times256=25{,}600

Bias:

256256

Total parameters:

25,600+25625{,}600+256
25,85625{,}856

This single layer has more than twenty thousand parameters.

Manually choosing all the weights is not a practical training method at this scale.

We need to:

Loss

Gradient

Optimizer

Automatic update

44. How neural networks relate to linear regression

Compare Week 2 and Week 3 side by side.

Linear Regression

y^=wx+b\hat y=wx+b

Input

Linear

Prediction

Neural Network

h=ReLU(xW1+b1)h=\operatorname{ReLU}(xW_1+b_1)

Then apply the activation:

y^=hW2+b2\hat y=hW_2+b_2

That is:

Input

Linear

Activation

Linear

Prediction

With more structure:

Input

Linear

ReLU

Linear

ReLU

Linear

Output

No new magic operation has appeared.

The network combines many:

wx+bwx+b

and inserts nonlinear:

Activation

45. Write a layer as a formula: each symbol matches the loop

Z=XW+b,A=f(Z)Z=XW+b,\qquad A=f(Z)

Scroll horizontally to view all columns.

Course data table
SymbolshapeWhat it does in code
X[B,in]B samples; each row contains inputs in a fixed feature order.
W[in,out]Column j assigns the input weights for output j.
b[out]One bias per output, reused across the batch.
Z、A[B,out]Z is the pre-activation result; A is the result after applying the function.

Our matrix convention uses one row per sample. PyTorch Linear stores weight as [out,in], so X @ layer.weight.T + layer.bias corresponds to this formula. Transposing translates between storage arrangements; it is not a different learning algorithm.

Multiplication plus a bias is an affine transformation; a linear transformation is the zero-bias special case. Frameworks commonly call the layer Linear even when it includes a bias. A hidden layer can use ReLU, while a regression output can return Z directly. The symbol f does not require every layer to use the same activation.

46. ReLU is not the only activation

You will also encounter:

Sigmoid

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

Output range:

010\sim1

This range is useful for representing a probability, although the range alone does not guarantee a calibrated prediction.

Tanh

Output:

11-1\sim1

ReLU

max(0,x)\max(0,x)

GELU

Common in Transformer models.

You will encounter it or related variants in GPT-style architectures.

You do not need to memorize the formula yet.

For now, remember:

One central role of an activation function is to introduce nonlinearity.

47. Why does sigmoid have this shape?

Formula:

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

Focus on its behavior before memorizing its formula.

Compare inputs and outputs:

A very negative x → output close to 0

x = 0 → 0.5

A very positive x → output close to 1

Graph:

1 |

______

|

__/

|

/

.5|--------*

|

/

| __/

0 |___

+---------------- x

This makes it useful for representing:

0 → unlikely

1 → likely

For example, a binary classifier's predicted probability.

48. Classification versus regression

Week 2 focused on:

Scroll horizontally to view all columns.

Course data table
TaskFinal-layer outputLoss used in this course
Predict any real-valued quantityA real value without ReLU clippingMSE
Choose one of V mutually exclusive classesV raw logitsCross-entropy, explained in Week 6
Display probabilities or sample a resultApply Softmax to the logitsThis reads the model's output; do not pass the probabilities back into a loss interface that expects logits

We begin with single-label classification. Multiple labels being true simultaneously is a different task; do not automatically copy the mutually exclusive Softmax rule into that setting.

Regression

Predicting numerical values:

House price = $812,532

Temperature = 27.3

Sales = 1052

Neural networks can also perform:

Classification

For example:

Cat

Dog

Bird

Or:

Spam

Not Spam

Or:

Fraud

Not Fraud

Much of the architecture can remain similar.

One important change is:

The output representation and loss function.

49. The loss function can change too

In Week 2, we used:

MSE

It is a common choice for numerical regression, though not the only one.

For classification, a common choice is:

Cross Entropy Loss

We will explain it in detail later.

For now, establish this connection:

Regression → often MSE

Classification → often Cross Entropy

50. A common misconception: is a neuron an if statement?

Not in the sense of a hand-written semantic rule.

A neuron does not directly contain a rule such as:

python
if image_has_ears:
    cat_score += 1

Instead, it calculates:

a=f(xW+b)a=f(xW+b)

Training adjusts W and b.

The combined network can then exhibit complex behavior.

It is not the same as a traditional:

Rule-based Programming

51. Traditional Programming vs Machine Learning

In a conventional hand-written program:

Data

+

Rules written by programmer

Output

For example:

python
if age >= 18:
    allow()

Machine Learning:

Data

+

Correct Answers

Training

Learn Parameters

Model

Then apply the activation:

New Data

+

Model

Prediction

The important distinction is whether the task-specific rule is explicitly written or fitted through parameters. The implementation can still contain conditionals, such as ReLU's clipping operation.

52. Does GPT also use these building blocks?

Its computation still makes extensive use of:

xW+bxW+b

The Transformer architecture adds more structure.

For example:

Token

Embedding

Attention

Linear

Activation

Linear

...

But it still contains many:

Matrix multiplication

Weights

Bias

Activation

You are not merely studying an obsolete small neural network.

You are learning:

Basic mathematical building blocks used inside GPT.

53. From regression to GPT: what stays, and what changes?

Scroll horizontally to view all columns.

Course data table
StageWhat staysThe new challenge
Linear regressionInputs, parameters, predictions, loss, and updatesOne affine rule can represent only a limited set of relationships.
Neural networkLoss guides parameter updatesHidden layers and nonlinearity provide more flexible representations.
Language modelThe same backpropagation and parameter-update principlesThe output scores vocabulary candidates rather than predicting one continuous quantity.
GPTA model built from many XW+b operations and other differentiable computationsCausal attention lets a position read its visible context: itself and earlier positions.

This week, you only need to explain the first two rows. Language modeling is not a wholly separate world from regression: the prediction function grows and its inputs and outputs change, but training still follows how parameter changes affect loss. Later weeks add or replace components step by step.

54. Close the book and explain these seven relationships

  • A neuron first calculates a weighted sum plus a bias; several neurons can receive the same input.
  • With row-wise samples, a layer calculates Z=XW+b and then A=f(Z).
  • Consecutive affine layers can still be collapsed into one affine layer, so stacking them alone cannot represent XOR. This does not mean changing the parameterization has no effect on optimization.
  • Nonlinearity lets different input regions produce different response behavior; ReLU is max(0,z).
  • Human-readable labels for hidden units are analogies, not guaranteed meanings of specific trained coordinates.
  • The output layer depends on the task: regression may require negative values; classification scores and losses arrive in Week 6.
  • A forward pass only computes results. Gradient calculation and an optimizer update are separate parts of learning. More parameters do not guarantee better generalization.

55. Three essential Week 3 formulas

➀Linear Calculation

z=xW+bz = xW + b

Meaning:

Input

×

Weights

+

Bias

➁Activation

a=f(z)a = f(z)

For example:

a=ReLU(z)a = \operatorname{ReLU}(z)

③ A neuron with its activation

Combined:

a=f(xW+b)a = f(xW+b)

If you can translate this formula into the following operations:

z = weighted_sum(inputs, weights) + bias

a = activation(z)

you have grasped much of Week 3's core computation.

56. Independent exercises: predict before revealing the answers

EX03-A: Use x=[2,3], w=[0.5,−0.2], and b=0.1. Calculate z, then a=ReLU(z). EX03-B: Change only the second weight to −1 and recalculate both. EX03-C: If this is a regression output that must allow negative predictions, should the second case return z or ReLU(z)?

python
def neuron(x, w, b, activation):
    z = sum(v * weight for v, weight in zip(x, w)) + b
    # Exercise fragment: complete the return behavior for activation.
    # None means a linear output; "relu" means clip negative values to zero.

Recall exercise EX03-D: Three inputs feed four neurons, each with a bias. How many parameters are there? Do the three input values count as model parameters?

Knowledge check

Write your results and reasoning for A–D before opening the reference answers.

57. Week 3 from a software engineer's perspective

Think of a neuron as:

python
class Neuron:
    def __init__(self):
        self.weights = ...
        self.bias = ...

    def forward(self, inputs):
        z = dot(inputs, self.weights) + self.bias
        return activation(z)

A layer as:

python
class Layer:
    neurons = [
        Neuron(),
        Neuron(),
        Neuron(),
    ]

A neural network as:

python
class NeuralNetwork:
    layers = [
        Layer(),
        Layer(),
        Layer(),
    ]

Forward:

python
x = input
for layer in layers:
    x = layer.forward(x)
prediction = x

Training:

python
for epoch in range(...):
    prediction = model.forward(input)
    loss = loss_function(prediction, target)
    gradients = backward(loss)
    update_parameters(gradients)

From this viewpoint:

A neural network is a parameterized computational graph.

Training means:

Adjusting parameters in that graph in an attempt to reduce the final loss.

58. Week 3 → Week 4: the key unresolved question

One major question remains.

Our network:

x1 ──w1──□

Hidden Neuron

x2 ──w2──□

│w3

Output Neuron

Prediction

Loss

If the final result is:

Loss = 10

we know the prediction is wrong.

But:

How much should w1 change?

Should w2 increase or decrease?

How sensitive is the final loss to w3?

In a network with 100 layers, how can we compute the effect of a weight near the beginning?

This is the focus of Week 4:

Backpropagation

It connects Week 2's derivatives, partial derivatives, chain rule, and gradient descent.

You will see how:

LossBackwardGradients for every weightOptimizer / Update\text{Loss} \rightarrow \text{Backward} \rightarrow \text{Gradients for every weight} \rightarrow \text{Optimizer / Update}

Week 2 introduced learning with a small number of parameters. Week 3 organized many parameters into a neural network. Week 4 explains how to calculate their gradients consistently so that the whole network can be trained.

Week 3 deeper connections: why nonlinear layers can represent more patterns

This supplement builds on the chapter's detailed explanations and addresses several important “why?” questions.

1. A neuron's inputs, parameters, and outputs

A neuron can be written as:

z=w1x1+w2x2++wnxn+bz=w_1x_1+w_2x_2+\cdots+w_nx_n+b

Here:

  • xᵢ is a feature supplied by the current example;
  • wᵢ is a trainable multiplier for that feature;
  • b shifts the affine score and, for ReLU, the location of the z=0 activation boundary;
  • z is the raw score before activation;
  • activation(z) is the neuron's response to that score; a nonlinear activation makes the response nonlinear.

A neuron combines several input values into one trainable response. With many neurons side by side, each can learn a different combination of inputs.

2. Why follow a weighted sum with an activation?

If the entire network contains only linear or affine operations:

(xW1+b1)W2+b2(xW_1+b_1)W_2+b_2

the input–output function can still be collapsed into one affine operation. More layers do not, by themselves, extend it beyond that function class.

A nonlinear activation breaks this general collapse into a single affine transformation. ReLU retains positive scores and clips negative ones to zero; sigmoid maps a real score into (0,1), a range useful for a probability prediction when paired with an appropriate task and loss.

3. Why are hidden layers useful?

A linear output score computed directly from raw features is limited to a simple affine relationship. Hidden layers first create intermediate features. For intuition, imagine responses associated with:

hidden 1 → “large area and nearby location”

hidden 2 → “many bedrooms with a price-related pattern”

These descriptions are illustrative labels, not prescribed or guaranteed meanings of particular units. Training changes weights, and the next layer combines the resulting intermediate signals. Nonlinear hidden layers can thereby represent more complex regions of the original input space.

4. Why is XOR a classic example?

XOR outputs 0 for (0,0) and (1,1), and 1 for (0,1) and (1,0). One affine score with a straight-line decision boundary cannot separate these two groups.

Two suitably chosen ReLU hidden units can respond on different sides of their activation boundaries; an output unit then combines their values, as in our four-row example. The point is not that GPT still solves XOR by hand. XOR is a small demonstration that nonlinearity and composition can change the functions a network can represent.

5. Why do classification outputs differ?

Regression often predicts a numerical value such as a house price. Binary classification commonly produces one logit, which sigmoid converts to a probability. Mutually exclusive multiclass classification produces one logit per class, and Softmax converts them into a probability distribution. A loss interface that expects logits receives the raw scores directly, not those already-converted probabilities.

6. Why does parameter count matter?

If a layer has n inputs and m neurons:

weights=n×m\mathit{weights}=n\times m

Add m biases. More parameters mean more adjustable values and greater storage requirements. They can increase representational capacity, but learning useful settings requires suitable data and training signals. Extra capacity may reduce underfitting or increase overfitting risk; neither outcome is guaranteed by parameter count alone.

7. Connect the forward pass to learning

The chapter's forward computation produces predictions but does not say how the weights should change. Week 4 works backward through the same network graph to calculate:

Loss

↓local derivatives + Chain Rule

A gradient for every weight and bias

↓optimizer

updated parameters

The statement “this network does not yet learn” matters: architecture defines the computation and its representational possibilities; backpropagation calculates gradients, and a separate optimizer uses them to update parameters from data.