Zhanbo Chen
Back to home

CS546 Learning Notes: From Softmax to MLP, Finally Understood Backpropagation

Algorithm/Learning Notes
2026-09-12
17 min read
0 reads
AIMachine Learning

In brief

Last time I learned about Linear Regression, Gradient Descent, and SGD. This time continuing further: if instead of predicting house prices, the model predicts three categories—positive, negative, and neutral—how does the model output? After adding a hidden layer, how does the loss affect the parameters before it? The most rewarding part today was breaking down the formulas into steps I could actually compute, especially the gradient of cross-entropy, the transpose in backpropagation, and the difference between "computing gradients to the hidden layer" and "updating parameters." This note is organized following the order of our actual questions and calculations, with the complete formulas together later.

1. Why Output Three Numbers for Three-Class Classification?

Previously we predicted a single number using y^=w⊤x+b\hat y=\mathbf w^\top\mathbf x+b. Now, to classify comments as positive, negative, or neutral, we give each category its own set of weights and bias, computing one score for each:

zi=wi⊤x+biz_i=\mathbf w_i^\top\mathbf x+b_i

Writing them together:

z=Wx+b\mathbf z=W\mathbf x+\mathbf b

z\mathbf z is the logits, meaning the raw scores before conversion to probabilities.

Is z equal to W * x + b?

Yes. Here zz is the score, and pp later is the probability. We need to keep them separate.

If a sample has dd features and the task has CC classes, we write single-sample input uniformly as a column vector:

z⏟C×1=W⏟C×dx⏟d×1+b⏟C×1\underbrace{\mathbf z}_{C\times1} =\underbrace{W}_{C\times d} \underbrace{\mathbf x}_{d\times1} +\underbrace{\mathbf b}_{C\times1}

In this notation, each row of WW is responsible for one class, and each column corresponds to one input feature. The number of rows in WW equals the number of classes.

Suppose there are three classes (positive, negative, neutral), then WW has three rows.

So how is the number of input features determined?

It's determined by how we represent samples numerically. For example, if we represent comments using "number of positive words, number of negative words," the input is two numbers; if we add "number of exclamation marks," the input becomes three numbers. These are handcrafted features just for convenience. Later we'll learn word embeddings.

With three classes and three input features, WW is 3×33\times3. The two 3's happen to be the same, but their meanings differ—one is the number of rows, the other the number of columns.

2. Why Can't Raw Scores Be Used Directly as Probabilities?

Suppose the three logits are:

z=(2,−1,0)⊤\mathbf z=(2,-1,0)^\top

I think they can't be used as probabilities because they don't sum to one hundred percent.

Let me check the arithmetic: 2−1+02-1+0 happens to equal 11. The real problem is that it contains negative numbers and numbers greater than 11.

Class probabilities must satisfy two requirements: each value is in [0,1][0,1], and the sum equals 11. Softmax performs exactly this conversion:

pi=ezi∑j=1Cezjp_i=\frac{e^{z_i}}{\sum_{j=1}^{C}e^{z_j}}

First we take the exponential to make every term positive; then divide by the sum to make the final sum equal 11:

∑ipi=∑iezi∑jezj=1\sum_i p_i=\frac{\sum_i e^{z_i}}{\sum_j e^{z_j}}=1

Substituting the scores from earlier:

ClassLogitExponential (approx.)Probability (approx.)
Positive27.3890.8438
Negative−10.36790.0420
Neutral010.1142

The class with the highest score still has the highest probability. However, softmax reflects the relative relationship between scores. If all logits increase by the same constant cc:

ezi+c∑jezj+c=eceziec∑jezj=pi\frac{e^{z_i+c}}{\sum_j e^{z_j+c}} =\frac{e^c e^{z_i}}{e^c\sum_j e^{z_j}}=p_i

The probabilities don't change at all. So we cannot look at a single logit in isolation; we must consider how it changes relative to other classes.

3. How Do We Calculate Loss for Classification?

For cases where each sample has exactly one correct class, cross-entropy can be written as:

L=−ln⁡pcorrect classL=-\ln p_{\text{correct class}}

The higher the probability of the correct class, the lower the loss:

Probability of Correct ClassLoss (approx.)
0.90.105
0.50.693
0.12.303

When the probability is 11, −ln⁡1=0-\ln1=0; this is the ideal boundary value. With finite real-valued logits, ordinary softmax probabilities are strictly less than 11 and can only approach it.

Suppose the correct label is "negative," but the model assigns negative a probability of about 0.0420.042:

L=−ln⁡(0.042)≈3.17L=-\ln(0.042)\approx3.17

We take the probability corresponding to the correct label, not the model's highest predicted probability. Even if the model chooses the wrong class, we can still compute the loss.

4. So with Three Classes, Do We Calculate Loss Three Times?

When I saw the complete formula, I got a bit confused:

L=−∑i=1Cyiln⁡piL=-\sum_{i=1}^{C}y_i\ln p_i

Following the order positive, negative, neutral, if the correct answer is negative, the one-hot label is:

y=(0,1,0)⊤\mathbf y=(0,1,0)^\top

Expanding:

L=−[0ln⁡p1+1ln⁡p2+0ln⁡p3]=−ln⁡p2L=-[0\ln p_1+1\ln p_2+0\ln p_3]=-\ln p_2

So it's the sum of three cross-entropies.

Let me clarify: it's three class terms added within one cross-entropy, not three independent cross-entropy losses. The one-hot label zeros out the terms for incorrect classes, keeping only the term for the correct class.

One sample, three classes: output three probabilities, get one loss. Two samples, each with three classes: get one loss for each, then average over the batch.

Using different subscripts to distinguish classes and samples:

Lˉ=−1B∑n=1B∑i=1Cyi(n)ln⁡pi(n)\bar L=-\frac1B\sum_{n=1}^{B}\sum_{i=1}^{C}y_i^{(n)}\ln p_i^{(n)}

BB is the batch size, CC is the number of classes. The rest of this article derives for single samples first, with labels as a fixed one-hot vector, and without regularization terms.

5. Why Is the Gradient of Softmax + Cross-entropy Simply p − y?

This part deserves careful explanation. Simply memorizing the result is easy to forget; deriving it by substitution is actually straightforward.

Still assume the second class is the correct answer:

p2=ez2ez1+ez2+ez3,L=−ln⁡p2p_2=\frac{e^{z_2}}{e^{z_1}+e^{z_2}+e^{z_3}},\qquad L=-\ln p_2

Substituting and using ln⁡(a/b)=ln⁡a−ln⁡b\ln(a/b)=\ln a-\ln b:

L=−ln⁡ez2ez1+ez2+ez3=−z2+ln⁡(ez1+ez2+ez3)L=-\ln\frac{e^{z_2}}{e^{z_1}+e^{z_2}+e^{z_3}} =-z_2+\ln(e^{z_1}+e^{z_2}+e^{z_3})

Taking the partial derivative with respect to z2z_2 (the score of the correct class):

∂L∂z2=−1+1ez1+ez2+ez3⋅ez2=p2−1\frac{\partial L}{\partial z_2} =-1+\frac{1}{e^{z_1}+e^{z_2}+e^{z_3}}\cdot e^{z_2} =p_2-1

The second part uses the chain rule: the derivative of ln⁡u\ln u is 1/u1/u, multiplied by the derivative of the inside with respect to z2z_2, which is ez2e^{z_2}.

For the incorrect class z1z_1, the derivative of −z2-z_2 is zero:

∂L∂z1=0+ez1ez1+ez2+ez3=p1\frac{\partial L}{\partial z_1} =0+\frac{e^{z_1}}{e^{z_1}+e^{z_2}+e^{z_3}}=p_1

Similarly for z3z_3, we get p3p_3. Combining all three results:

∂L∂z=[p1p2−1p3]=p−y\frac{\partial L}{\partial\mathbf z} =\begin{bmatrix}p_1\\p_2-1\\p_3\end{bmatrix} =\mathbf p-\mathbf y

Therefore:

∂L∂zi=pi−yi\boxed{\frac{\partial L}{\partial z_i}=p_i-y_i}

Although the loss ultimately reduces to −ln⁡p2-\ln p_2, p2p_2's denominator contains all logits, so other classes also have gradients.

6. From Gradient of Logits to Gradient of Parameters

This distinction is important: pi−yip_i-y_i is the gradient with respect to ziz_i, not yet the gradient with respect to weights.

Because:

zi=∑jWijxj+biz_i=\sum_j W_{ij}x_j+b_i

Applying the chain rule:

∂L∂Wij=(pi−yi)xj,∂L∂bi=pi−yi\frac{\partial L}{\partial W_{ij}}=(p_i-y_i)x_j, \qquad \frac{\partial L}{\partial b_i}=p_i-y_i

Working through today's exercise: with only one input feature x=2x=2, the correct class is negative, and the negative probability is about 0.0420.042:

∂L∂znegative=0.042−1=−0.958\frac{\partial L}{\partial z_{\text{negative}}}=0.042-1=-0.958 ∂L∂wnegative=−0.958×2=−1.916,∂L∂bnegative=−0.958\frac{\partial L}{\partial w_{\text{negative}}}=-0.958\times2=-1.916, \qquad \frac{\partial L}{\partial b_{\text{negative}}}=-0.958

With learning rate 0.10.1:

wnew=wold+0.1916,bnew=bold+0.0958w_{\text{new}}=w_{\text{old}}+0.1916, \qquad b_{\text{new}}=b_{\text{old}}+0.0958

So both w and b increase, the negative logit increases, and then the softmax probability increases?

In this single-sample, single-layer classifier update, yes. Here x>0x>0, so increasing both parameters raises the negative logit. The gradients for the other two classes are positive, so their logits decrease, raising the probability of the correct class.

Supplement: After Updating Parameters, How Much Does the Logit Actually Change?

What does this formula mean?

First define the change: Δzi=zi′−zi\Delta z_i=z_i'-z_i, where primes indicate values after the update. This isn't a new update rule, but rather substituting the parameter update back into the logit formula to compute the score change.

With just one input feature, the original score is:

zi=wix+biz_i=w_ix+b_i

We already know the single-sample gradient descent update:

wi′=wi−η(pi−yi)x,bi′=bi−η(pi−yi)w_i'=w_i-\eta(p_i-y_i)x, \qquad b_i'=b_i-\eta(p_i-y_i)

The probabilities and gradients are all computed before the update. For the same input xx, substituting into the updated score:

zi′=wi′x+bi′=[wi−η(pi−yi)x]x+[bi−η(pi−yi)]=wix+bi⏟zi−η(pi−yi)x2−η(pi−yi)\begin{aligned} z_i'&=w_i'x+b_i'\\ &=\big[w_i-\eta(p_i-y_i)x\big]x +\big[b_i-\eta(p_i-y_i)\big]\\ &=\underbrace{w_ix+b_i}_{z_i} -\eta(p_i-y_i)x^2-\eta(p_i-y_i) \end{aligned}

Subtracting the old score and combining the two terms:

Δzi=−η(pi−yi)(x2+1)\boxed{\Delta z_i=-\eta(p_i-y_i)(x^2+1)}

The x2x^2 comes from the contribution of weight change, and the 11 comes from the contribution of bias change. The weight update is multiplied by the input once; when substituted into wixw_ix, it's multiplied again, producing the square.

Verifying with the negative class from before: x=2x=2, η=0.1\eta=0.1, pi−yi=−0.958p_i-y_i=-0.958, thus:

Δznegative=−0.1×(−0.958)×(22+1)=0.479\Delta z_{\text{negative}} =-0.1\times(-0.958)\times(2^2+1)=0.479

That is, the negative logit increases by 0.4790.479. Using the parameter changes calculated earlier directly also gives the same result:

Δznegative=0.1916⏟Δw×2+0.0958⏟Δb=0.479\Delta z_{\text{negative}} =\underbrace{0.1916}_{\Delta w}\times2 +\underbrace{0.0958}_{\Delta b}=0.479

When there are multiple input features, the contribution of each weight change is summed:

Δzi=∑j=1dΔWijxj+Δbi=−η(pi−yi)(∑j=1dxj2+1)\Delta z_i=\sum_{j=1}^{d}\Delta W_{ij}x_j+\Delta b_i =-\eta(p_i-y_i)\left(\sum_{j=1}^{d}x_j^2+1\right)

And ∥x∥2=∑j=1dxj2\|\mathbf x\|^2=\sum_{j=1}^{d}x_j^2 is the sum of squares of all vector components, so it can be abbreviated as:

Δzi=−η(pi−yi)(∥x∥2+1)\Delta z_i=-\eta(p_i-y_i)(\|\mathbf x\|^2+1)

Since ∥x∥2+1\|\mathbf x\|^2+1 is always positive and the learning rate is also positive, the direction of change is determined by −(pi−yi)-(p_i-y_i): the logit of the correct class increases, and the logit of incorrect classes decreases.

The prerequisite here is: the input vector remains unchanged, using only the gradient from this single sample, updating the weights and biases of this layer with the same learning rate, and using ordinary gradient descent without momentum or regularization. Batch updates mix in gradients from other samples; when updating the entire MLP simultaneously, the hidden representation received by this layer may also change. Therefore, we cannot directly generalize to "each step of any batch or entire MLP is guaranteed to reduce loss for every sample."

7. Why discuss ReLU before adding hidden layers?

Suppose we only add another linear computation:

h=2x+1,z=3h+4h=2x+1,\qquad z=3h+4

Combining them:

z=3(2x+1)+4=6x+7z=3(2x+1)+4=6x+7

We computed two layers, but it's still the same form of weighted sum plus bias. Strictly speaking, with bias it's called an affine transformation; the course often calls such layers linear layers.

In matrix form we can also combine them:

W2(W1x+b1)+b2=(W2W1)x+(W2b1+b2)W_2(W_1\mathbf x+\mathbf b_1)+\mathbf b_2 =(W_2W_1)\mathbf x+(W_2\mathbf b_1+\mathbf b_2)

Therefore we need nonlinearity in between. ReLU is:

ReLU⁡(a)=max⁡(0,a)\operatorname{ReLU}(a)=\max(0,a)

Negative numbers become zero, positive numbers stay the same. After adding it:

h=ReLU⁡(2x+1),z=3h+4h=\operatorname{ReLU}(2x+1),\qquad z=3h+4

When x=−1x=-1 I get h=0h=0, when x=1x=1 I get h=3h=3. The entire function becomes:

z={4,x≤−0.56x+7,x>−0.5z=\begin{cases}4,&x\le-0.5\\6x+7,&x>-0.5\end{cases}

A kink point appears—it's no longer a single global straight line. This is the change brought by nonlinearity in this example.

8. Extending from one hidden unit to four

The complete architecture used today is: three input features, four ReLU hidden units, three classes.

a=W1x+b1\mathbf a=W_1\mathbf x+\mathbf b_1 h=ReLU⁡(a)\mathbf h=\operatorname{ReLU}(\mathbf a) z=W2h+b2,p=softmax⁡(z)\mathbf z=W_2\mathbf h+\mathbf b_2, \qquad \mathbf p=\operatorname{softmax}(\mathbf z)
QuantityShapeMeaning
x\mathbf x3×13\times1Input features
W1W_14×34\times3Each row computes weighted sum for one hidden unit
b1,a,h\mathbf b_1,\mathbf a,\mathbf h4×14\times1First layer bias, pre-activation, post-activation
W2W_23×43\times4Each row computes score for one class
b2,z,p,y\mathbf b_2,\mathbf z,\mathbf p,\mathbf y3×13\times1Output bias, scores, probabilities, labels

If we transpose h, wouldn't W2 be 3×4?

The shape of W2W_2 is correct, but we don't need to transpose h\mathbf h here:

(3×4)(4×1)⟶(3×1)(3\times4)(4\times1)\longrightarrow(3\times1)

If we transpose h\mathbf h, we couldn't multiply at all. First stick with the convention of using column vectors, then check that the inner dimensions match.

Without a hidden layer, classifying directly with softmax is usually called Softmax Regression; now with a ReLU hidden layer added, it's an MLP for multiclass classification. The number of hidden units is a structural choice we make, not equal to the number of classes.

9. Backpropagation: multiply along one path, add multiple paths

First, consider a single hidden unit:

a=w1x+b1,h=ReLU⁡(a),z=w2h+b2a=w_1x+b_1,\qquad h=\operatorname{ReLU}(a),\qquad z=w_2h+b_2

If we already know ∂L/∂z\partial L/\partial z coming back from later, then:

∂L∂w1=∂L∂z∂z∂h∂h∂a∂a∂w1\frac{\partial L}{\partial w_1} =\frac{\partial L}{\partial z} \frac{\partial z}{\partial h} \frac{\partial h}{\partial a} \frac{\partial a}{\partial w_1}

That is:

∂L∂w1=∂L∂z w2 ReLU⁡′(a) x\frac{\partial L}{\partial w_1} =\frac{\partial L}{\partial z}\,w_2\,\operatorname{ReLU}'(a)\,x

When a>0a>0, the ReLU derivative is 11; when a<0a<0 it is 00. At a=0a=0 it is not differentiable; implementations typically choose 00.

In today's local exercise, given ∂L/∂z=2,w2=3,x=1,a=3\partial L/\partial z=2,w_2=3,x=1,a=3, the result is 66. This is a local practice example of the chain rule with upstream gradient, not the softmax gradient from the three-class sample earlier.

If the same hh affects two outputs:

z1=v1h+b1,z2=v2h+b2z_1=v_1h+b_1,\qquad z_2=v_2h+b_2

Then:

∂L∂h=∂L∂z1v1+∂L∂z2v2\frac{\partial L}{\partial h} =\frac{\partial L}{\partial z_1}v_1 +\frac{\partial L}{\partial z_2}v_2

In the exercise, the two paths contribute 2×3=62\times3=6 and (−1)×4=−4(-1)\times4=-4 respectively, for a total gradient of 22. Different paths can partially cancel each other out.

Therefore, the two rules of backpropagation are: multiply along a path, add when multiple paths converge.

10. Today's most valuable insight: why use the transpose in backpropagation?

The forward pass is:

z=W2h+b2\mathbf z=W_2\mathbf h+\mathbf b_2

Let gz=∂L/∂z=p−y\mathbf g_z=\partial L/\partial\mathbf z=\mathbf p-\mathbf y. The gradient passed back to the hidden layer is:

gh=W2⊤gz\boxed{\mathbf g_h=W_2^\top\mathbf g_z}

Shape check:

W2⊤⏟4×3gz⏟3×1=gh⏟4×1\underbrace{W_2^\top}_{4\times3} \underbrace{\mathbf g_z}_{3\times1} =\underbrace{\mathbf g_h}_{4\times1}

But just saying "so the dimensions multiply" is not enough. The real reason comes from looking at a single hidden unit hjh_j:

∂L∂hj=∑i=13W2,ij∂L∂zi\frac{\partial L}{\partial h_j} =\sum_{i=1}^{3}W_{2,ij}\frac{\partial L}{\partial z_i}

hjh_j connects to three outputs; the weights of these three connections are in column jj of W2W_2. We need to multiply this column element-wise with the output gradients and sum them.

Taking the transpose converts this column into a row, so we can use matrix multiplication to simultaneously compute the aggregation for all hidden units at once.

In the forward pass, each row of weights collects inputs to compute one output; in the backward pass, each column of weights collects the gradient contributions from all outputs to the same input.

This is not inversion, nor is it working backward through the forward computation to recover the original input. The matrix is not even necessarily square. We are computing how sensitive the loss is to intermediate quantities.

11. Gradient through ReLU: element-wise multiplication

We also need to pass from h\mathbf h to the pre-activation a\mathbf a:

ga=gh⊙ReLU⁡′(a)\boxed{\mathbf g_a=\mathbf g_h\odot\operatorname{ReLU}'(\mathbf a)}

⊙\odot denotes element-wise multiplication, not matrix multiplication.

Another local exercise today gives:

a=[2−13−2],gh=[0.5−0.2−0.80.4]\mathbf a=\begin{bmatrix}2\\-1\\3\\-2\end{bmatrix}, \qquad \mathbf g_h=\begin{bmatrix}0.5\\-0.2\\-0.8\\0.4\end{bmatrix}

Thus:

ga=[0.5−0.2−0.80.4]⊙[1010]=[0.50−0.80]\mathbf g_a= \begin{bmatrix}0.5\\-0.2\\-0.8\\0.4\end{bmatrix} \odot\begin{bmatrix}1\\0\\1\\0\end{bmatrix} =\begin{bmatrix}0.5\\0\\-0.8\\0\end{bmatrix}

The second and fourth units have negative pre-activation values, so the gradient for this sample is zeroed out at these locations. This doesn't mean these parameters never change: other samples might activate them; updates with momentum, regularization, etc. cannot be judged by this term alone.

12. Finally, how do we get the weight gradients for the first layer?

Because:

ai=∑jW1,ijxj+b1,ia_i=\sum_j W_{1,ij}x_j+b_{1,i}

We have:

∂L∂W1,ij=ga,ixj,∂L∂b1,i=ga,i\frac{\partial L}{\partial W_{1,ij}}=g_{a,i}x_j, \qquad \frac{\partial L}{\partial b_{1,i}}=g_{a,i}

Given the input:

x=[21−1]\mathbf x=\begin{bmatrix}2\\1\\-1\end{bmatrix}

The weight gradient for the first hidden unit is 0.5[2,1,−1]=[1,0.5,−0.5]0.5[2,1,-1]=[1,0.5,-0.5].

Is the third one −1.6, −0.8, 0.8?

Yes, −0.8[2,1,−1]=[−1.6,−0.8,0.8]-0.8[2,1,-1]=[-1.6,-0.8,0.8]. Putting all four rows together:

∂L∂W1=gax⊤=[10.5−0.5000−1.6−0.80.8000]\boxed{\frac{\partial L}{\partial W_1}=\mathbf g_a\mathbf x^\top} =\begin{bmatrix} 1&0.5&-0.5\\ 0&0&0\\ -1.6&-0.8&0.8\\ 0&0&0 \end{bmatrix}

The shape is (4×1)(1×3)=4×3(4\times1)(1\times3)=4\times3, same as W1W_1. This operation of multiplying a column vector by a row vector is called an outer product.

The transpose here serves a different purpose than before:

  • W2⊤gzW_2^\top\mathbf g_z: Aggregates gradients across different output paths to obtain the gradient for the hidden output.
  • gax⊤\mathbf g_a\mathbf x^\top: Multiplies the gradient of each hidden unit by each input respectively to get the weight gradient for each connection.

13. Computing gradients is not the same as updating parameters

You also need to update parameters using the previous formula: 1 − learning rate × gradient.

The operation is understood correctly, but 11 is just the initial weight in the previous example. The general formula is:

θnew=θold−η∂L∂θ\theta_{\text{new}}=\theta_{\text{old}}-\eta\frac{\partial L}{\partial\theta}

There is also a key distinction: h\mathbf h and a\mathbf a are computed intermediate results, not directly learned parameters. After computing gh\mathbf g_h, you must continue to compute the gradients for W1,b1W_1,\mathbf b_1, and then update the parameters. The next forward pass will recompute the hidden layer outputs.

For the network here, what is learned is W1,b1,W2,b2W_1,\mathbf b_1,W_2,\mathbf b_2.

Backpropagation is responsible for computing gradients; gradient descent is responsible for using gradients to update parameters. A typical training step should first compute all gradients using the same set of forward parameters, then update them uniformly. Do not modify W2W_2 first and then use the new W2W_2 to compute gh\mathbf g_h for this step.

14. Complete formulas together; during review you can explain line by line

Here we complete the output layer parameter gradients. It follows the same "gradient times input" rule as the first layer; the input to the output layer is h\mathbf h.

Forward: compute predictions and loss

a=W1x+b1h=ReLU⁡(a)z=W2h+b2p=softmax⁡(z)L=−∑iyiln⁡pi\begin{aligned} \mathbf a&=W_1\mathbf x+\mathbf b_1\\ \mathbf h&=\operatorname{ReLU}(\mathbf a)\\ \mathbf z&=W_2\mathbf h+\mathbf b_2\\ \mathbf p&=\operatorname{softmax}(\mathbf z)\\ L&=-\sum_i y_i\ln p_i \end{aligned}

Backward: compute gradients of intermediate quantities and parameters

gz=p−y∇W2L=gzh⊤∇b2L=gzgh=W2⊤gzga=gh⊙ReLU⁡′(a)∇W1L=gax⊤∇b1L=ga\begin{aligned} \mathbf g_z&=\mathbf p-\mathbf y\\ \nabla_{W_2}L&=\mathbf g_z\mathbf h^\top\\ \nabla_{\mathbf b_2}L&=\mathbf g_z\\ \mathbf g_h&=W_2^\top\mathbf g_z\\ \mathbf g_a&=\mathbf g_h\odot\operatorname{ReLU}'(\mathbf a)\\ \nabla_{W_1}L&=\mathbf g_a\mathbf x^\top\\ \nabla_{\mathbf b_1}L&=\mathbf g_a \end{aligned}

Update: each parameter minus learning rate times its own gradient

Wℓ←Wℓ−η∇WℓL,bℓ←bℓ−η∇bℓL,ℓ∈{1,2}W_\ell\leftarrow W_\ell-\eta\nabla_{W_\ell}L, \qquad \mathbf b_\ell\leftarrow\mathbf b_\ell-\eta\nabla_{\mathbf b_\ell}L, \quad\ell\in\{1,2\}

For batch-averaged loss, average the gradients of each sample with respect to the same parameter, then update. All samples must be computed based on the parameters before this update.

15. Where I am in learning now, and how to review

What was initially worked through today is an "MLP with one ReLU hidden layer plus softmax multiclass output." I can manually compute local examples and understand gradient paths and shapes, but have not yet independently completed a full network numerical computation or code training.

During review, you can first cover the answers and ask yourself:

  1. Why is a single sample loss for three classes one loss rather than three separate losses?
  2. Can you derive p−y\mathbf p-\mathbf y from −ln⁡pcorrect class-\ln p_{\text{correct class}}?
  3. What does one column of W2W_2 represent, and why do we use it to aggregate in the backward pass?
  4. Which intermediate quantity's gradient does ReLU zero out?
  5. Why is the weight gradient "the incoming gradient times this layer's input"?
  6. After computing gh\mathbf g_h, why can't we directly say the parameters are already updated?

Explaining these clearly and then computing a complete example yourself will validate your understanding better than just knowing the formulas. Later when connecting word embeddings, you can continue asking: where do input vectors come from, and can they also be learned through gradients?

Corresponding slides

Source from course Lecture3_DeepLearning_Background.pdf, page numbers according to PDF reader counting, with cover page as page 1.

PagesCorresponding content
17–20Multiclass, one-hot, class scores, and softmax
22–24Nonlinearity and why activation functions are needed
27–38Multilayer networks, layer notation, and forward passing
39–42Loss, forward and backpropagation, chain rule
44–60Layered computation and gradient propagation

The ReLU numerical examples, softmax cross-entropy derivation, and W1,W2W_1,W_2 notation in this document come from today's supplementary explanations and are not verbatim transcriptions from the slides. The Lecture 5 uploaded today covers Sequence Modeling; we first consolidate the MLP fundamentals needed to understand it, and have not yet begun CNN, RNN, LSTM, and GRU.

ZB

Zhanbo Chen

Java Backend & AI Agent Developer

Back to home
Comments
CS546 Learning Notes: From Softmax to MLP, Finally Understood Backpropagation | Zhanbo