← COMP5541 全部讲次
Q06

Final 计算题|C 第 4–7 讲

非官方期末自拟计算题 10 道:MLP 参数与前向、反向传播一步、动量、L1/L2 更新、卷积与池化手算、RNN、LSTM、注意力,附英文解答与课件页码。

手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。

使用边界:期末复习,非官方材料。课程信息写期末闭卷、3 小时、选择题加数学计算题;日期、地点与范围尚未公布,以讲师通知和课堂公告为准。本页是按第 4–7 讲课件自拟的计算题,数值重新设计,方法与约定照课件。

复习资料:考试形式与范围地图 · 闭卷公式默写表 · 算法横向对照

先做题再看解析:A 计算:第 1–3 讲 · B 选择:第 1–3 讲 · C 计算:第 4–7 讲 · D 选择:第 4–7 讲 · E 计算:第 8–10 讲 · F 选择:第 8–10 讲

怎么做:闭卷、限时,先把每一步写在纸上,再展开对照;算错的题回到回查里的讲义页重做一遍。

C 计算题:第 4–7 讲

题面用英文,约定写在题面里。第 11、12 讲待补。

C1 · MLP parameters and forward pass

(a) A fully connected network has layer widths 6 → 5 → 4 → 3. Count the trainable parameters (i) without biases and (ii) with a bias on every non-input neuron.

(b) A network has 2 inputs, one hidden layer of 3 ReLU units and 1 linear output, no biases. Hidden weights: unit 1 (1, 1), unit 2 (0.5, 2), unit 3 (−1, 1); output weights (1, −2, 3). Compute the hidden pre-activations, hidden outputs and y for x = (2, 1).

C1 · 查看答案与解析

答案:(a) 不计偏置 6×5 + 5×4 + 4×3 = 62;含偏置 (6+1)×5 + (5+1)×4 + (4+1)×3 = 74,多出的正好是非输入神经元个数。(b) s = (3, 3, −1),ReLU 后 h = (3, 3, 0),y = 3 − 6 + 0 = −3,输出层线性,不再过激活。

English:(a) 6·5 + 5·4 + 4·3 = 62 without biases; (6+1)·5 + (5+1)·4 + (4+1)·3 = 74 with biases. (b) s = (3, 3, −1), ReLU gives h = (3, 3, 0), and the linear output is y = 3 − 6 + 0 = −3.

易错点:偏置按本层神经元个数加;第三个单元 s < 0,输出 0;不要对输出 −3 再做 ReLU。

回查:L04 p.12、p.13、p.18;L05 p.16。L04b 宽度、深度与输出层

C2 · One backpropagation step

Inputs x₁, x₂; s₁ = w₁x₁ + w₂x₂, s₂ = w₃x₁ + w₄x₂, h₁ = σ(s₁), h₂ = σ(s₂); linear output y = w₅h₁ + w₆h₂, no biases; loss ℓ = (y − ȳ)² (no ½). Given x = (1, 1), ȳ = 1, w₁ = 0.5, w₂ = −0.5, w₃ = 1, w₄ = −1, w₅ = 2, w₆ = 1.

(a) Forward pass: s₁, s₂, h₁, h₂, y, ℓ. (b) dℓ/dw₅, dℓ/dw₆, dℓ/dw₁, dℓ/dw₂, dℓ/dw₃, dℓ/dw₄. (c) One gradient-descent step with η = 0.1: new w₅ and new w₁.

C2 · 查看答案与解析

答案:

  • 前向:s₁ = s₂ = 0,h₁ = h₂ = σ(0) = 0.5,y = 1.5,ℓ = 0.25。
  • 原始误差 2(y − ȳ) = 1。输出层 dℓ/dw₅ = 1×h₁ = 0.5,dℓ/dw₆ = 0.5。
  • s₁ 处累积误差 1 × w₅ × σ′(s₁) = 2 × 0.25 = 0.5,dℓ/dw₁ = 0.5×x₁ = 0.5,dℓ/dw₂ = 0.5。
  • s₂ 处累积误差 1 × w₆ × 0.25 = 0.25,dℓ/dw₃ = dℓ/dw₄ = 0.25。
  • 更新:w₅ = 2 − 0.1×0.5 = 1.95,w₁ = 0.5 − 0.1×0.5 = 0.45。

English:Forward: s₁ = s₂ = 0, h₁ = h₂ = 0.5, y = 1.5, ℓ = 0.25. Backward: dℓ/dy = 2(y − ȳ) = 1; dℓ/dw₅ = dℓ/dw₆ = 0.5; δ₁ = 1·w₅·σ′(s₁) = 0.5, so dℓ/dw₁ = dℓ/dw₂ = 0.5; δ₂ = 1·w₆·0.25 = 0.25, so dℓ/dw₃ = dℓ/dw₄ = 0.25. Backpropagation gives the gradients; gradient descent then sets w₅ = 1.95 and w₁ = 0.45.

易错点:按 ½(y − ȳ)² 算出原始误差 0.5;漏乘 σ′ = 0.25;同一步里用更新后的 w₅ 去算 w₁ 的梯度。

回查:L04 p.24、p.26–31、p.36。L04c 链式法则与局部导数 · L04d 反向传播

C3 · Momentum, two steps

Minimise L(θ) = θ² with v ← αv − εg, θ ← θ + v, g = 2θ. Take θ₀ = 3, v₀ = 0, ε = 0.1, α = 0.5. Compute g, v, θ for two steps and compare with plain gradient descent (α = 0).

C3 · 查看答案与解析

答案:

  • 第 1 步:g = 6,v = 0.5×0 − 0.1×6 = −0.6,θ = 2.4。
  • 第 2 步:g = 2×2.4 = 4.8,v = 0.5×(−0.6) − 0.48 = −0.78,θ = 1.62。
  • 普通 GD:2.4,1.92。第 1 步两者相同,第 2 步动量多走的 0.3 就是 αv。

English:Step 1: g = 6, v = −0.6, θ = 2.4. Step 2: g = 4.8, v = 0.5·(−0.6) − 0.1·4.8 = −0.78, θ = 1.62. Plain GD gives 2.4 then 1.92; momentum moves further in step 2 because it keeps αv = −0.3.

易错点:第 2 步的 g 要用新 θ;α 是动量参数、ε 是学习率,别对调。

回查:L05 p.12。L05a mini-batch 与动量

C4 · L2 and L1 regularized update

A weight w = 0.8 has data-loss gradient dℓ/dw = −0.2; η = 0.5, α = 0.1. (a) L2, loss α‖W‖² + ℓ: gradient and new w. (b) L1, loss α·Σ|wᵢ| + ℓ: gradient and new w. (c) New w without regularization.

C4 · 查看答案与解析

答案:

  • (a) 梯度 2αw + dℓ/dw = 0.16 − 0.2 = −0.04,w = 0.8 + 0.02 = 0.82;等价于 (1 − 2ηα)w − η·dℓ/dw = 0.72 + 0.1。
  • (b) 梯度 α·sign(w) + dℓ/dw = 0.1 − 0.2 = −0.1,w = 0.85。
  • (c) w = 0.8 + 0.1 = 0.9。两种正则都把 w 往 0 拉回一点:L2 的拉力随 w 变,L1 的拉力恒为 α。

English:(a) L2 gradient = 2αw + dℓ/dw = −0.04, so w ← 0.8 − 0.5·(−0.04) = 0.82. (b) L1 gradient = α·sign(w) + dℓ/dw = −0.1, so w ← 0.85. (c) Without regularization w ← 0.9. Both penalties pull the weight towards zero.

易错点:漏掉 2αw 的 2,得到 0.86;L1 用 sign(w),不是 w。

回查:L05 p.19、p.20、p.21。L05c 正则化

C5 · Convolution by hand

Input (4×4):

1 2 0 1
0 1 3 1
2 0 1 2
1 1 0 3

Kernel [[1, 0, 1], [0, 1, 0], [1, 0, 1]]; slide and take the dot product (no flipping, no bias). (a) Stride 1, no padding: output size and values. (b) Output size with one pixel of zero padding per side, stride 1. (c) A 9×9 input, 3×3 kernel, stride 2, no padding: output size?

C5 · 查看答案与解析

答案:

  • (a) (4 − 3)/1 + 1 = 2。核为 1 的是四角和中心:左上 1+0+2+1+1 = 5,右上 2+1+0+2+3 = 8,左下 0+3+1+0+0 = 4,右下 1+1+1+3+1 = 7,得 [[5, 8], [4, 7]]。
  • (b) p = (k − 1)/2 = 1,(4 + 2 − 3)/1 + 1 = 4,尺寸不变。
  • (c) (9 − 3)/2 + 1 = 4。

English:(a) Output size (4 − 3)/1 + 1 = 2; each value is the sum of the four corners and the centre of its window, giving [[5, 8], [4, 7]]. (b) (4 + 2·1 − 3)/1 + 1 = 4, the size is kept. (c) (9 − 3)/2 + 1 = 4.

易错点:右上窗口是第 2–4 列;按课件不翻转核;带 padding 时漏了 2p。

回查:L06 p.12、p.13、p.18;T02 p.24。L06a 卷积运算 · L06b 通道、参数与 padding

C6 · Multi-channel convolution: shapes and parameters

Input 64×64×3. Conv1: 16 kernels 5×5, stride 1, no padding. Conv2: 32 kernels 3×3, stride 1, padding 1. Then 2×2 max-pooling, stride 2. Give each output shape and each layer’s parameters, without and with biases (one per output channel).

C6 · 查看答案与解析

答案:

  • Conv1:(64 − 5)/1 + 1 = 60,输出 60×60×16;权重 5×5×3×16 = 1200,加 16 个偏置 1216。
  • Conv2:(60 + 2 − 3)/1 + 1 = 60,输出 60×60×32;核深度等于输入通道 16,权重 3×3×16×32 = 4608,加 32 个偏置 4640。
  • pooling:(60 − 2)/2 + 1 = 30,输出 30×30×32,无参数,通道数不变。
  • 含偏置合计 5856,与输入长宽无关。

English:Conv1: output 60×60×16, 5·5·3·16 = 1200 weights, 1216 with biases. Conv2: output 60×60×32; each kernel has depth 16, so 3·3·16·32 = 4608 weights, 4640 with biases. Max-pooling: 30×30×32, no parameters. Total 5856 with biases.

易错点:Conv2 的核深度写成 3;给 pooling 算参数;把长宽乘进参数量。

回查:L06 p.14–15、p.17–18、p.20;T02 p.20、p.22。L06b 通道、参数与 padding

C7 · Max-pooling and ReLU order

Input (4×4):

3 −1 0 2
−2 4 1 −3
−1 −2 5 −6
−4 −3 0 1

(a) 2×2 max-pooling, stride 2. (b) ReLU after the pooling. (c) ReLU first, then the same pooling. Compare (b) and (c).

C7 · 查看答案与解析

答案:

  • (a) 四个窗口最大值 4、2、−1、5,得 [[4, 2], [−1, 5]]。
  • (b) ReLU 后 [[4, 2], [0, 5]]。
  • (c) 先 ReLU,左下窗口全变 0,max 为 0;其余不变,结果同 (b)。ReLU 单调不减,所以先后顺序不影响结果,先 pool 再 ReLU 计算量更小。

English:(a) [[4, 2], [−1, 5]]. (b) [[4, 2], [0, 5]]. (c) Also [[4, 2], [0, 5]]: ReLU is non-decreasing, so max of ReLU equals ReLU of max, and pooling first is cheaper.

易错点:全负窗口的 max 是 −1,pooling 本身不截断;stride 当成 1 会得到 3×3。

回查:L06 p.20、p.22。L06c max-pooling

C8 · Vanilla RNN forward and parameters

(a) One-dimensional vanilla RNN: h_t = tanh(W_hh h_{t−1} + W_hx x_t), y_t = W_yh h_t, no biases; W_hh = 0.5, W_hx = 1, W_yh = 2, h₀ = 0, x = (1, −1). Compute h₁, y₁, h₂, y₂ (4 decimals). (b) Input dimension 4, hidden 6, output 3, no biases: how many parameters, and does the count change for longer sequences?

C8 · 查看答案与解析

答案:

  • t = 1:h₁ = tanh(1) ≈ 0.7616,y₁ ≈ 1.5232。
  • t = 2:h₂ = tanh(0.5×0.7616 − 1) = tanh(−0.6192) ≈ −0.5506,y₂ ≈ −1.1011。
  • (b) W_hh 6×6 = 36,W_hx 6×4 = 24,W_yh 3×6 = 18,共 78;三个矩阵在所有时间步共用,不随序列长度变。

English:(a) h₁ = tanh(1) ≈ 0.7616, y₁ ≈ 1.5232; h₂ = tanh(0.5·0.7616 − 1) ≈ −0.5506, y₂ ≈ −1.1011. (b) 36 + 24 + 18 = 78 parameters, shared across time steps, so the count does not depend on sequence length.

易错点:第 2 步忘了带入 h₁;y_t 没有激活,不要再 tanh。

回查:L07 p.13。L07b vanilla RNN

C9 · One LSTM step and parameter count

(a) One-dimensional LSTM, c_{t−1} = 0.4. Gate pre-activations: forget 2, input 0, output 1, candidate 0.5. With f, i, o = σ(·), g = tanh(·), c_t = f_t c_{t−1} + i_t g_t, h_t = o_t tanh(c_t), compute f_t, i_t, o_t, g_t, c_t, h_t (4 decimals). (b) Input dimension 2, hidden 5, no biases: parameters of the LSTM cell, and of the core W_hh + W_hx of a vanilla RNN of the same sizes.

C9 · 查看答案与解析

答案:

  • f = σ(2) ≈ 0.8808,i = σ(0) = 0.5,o = σ(1) ≈ 0.7311,g = tanh(0.5) ≈ 0.4621。
  • c_t ≈ 0.3523 + 0.2311 = 0.5834;tanh(c_t) ≈ 0.5252,h_t ≈ 0.3839。
  • (b) 每个门 5×2 加 5×5,共 35;四个门 140,是 vanilla RNN 核心 35 的 4 倍。

English:(a) f ≈ 0.8808, i = 0.5, o ≈ 0.7311, g ≈ 0.4621; c_t = 0.8808·0.4 + 0.5·0.4621 ≈ 0.5834; h_t = 0.7311·tanh(0.5834) ≈ 0.3839. (b) Each of the four gates has a 5×2 and a 5×5 matrix (35 weights), so 4·35 = 140, four times the vanilla RNN core.

易错点:g 用 tanh;h_t 里是 tanh(c_t);只数三个门会得到 105。

回查:L07 p.15。L07c LSTM

C10 · Scaled dot-product attention for one query

q = (1, 1, 1, 1); keys k₁ = (2, 2, 1, 1), k₂ = (1, 0, 1, 0), k₃ = (0, 1, 0, 1); values v₁ = (1, 0), v₂ = (0, 1), v₃ = (2, 2). With softmax(QKᵀ/√d_k)V and d_k = 4, give (a) raw scores, (b) scaled scores, (c) weights (3 decimals), (d) the output, (e) the weights without scaling.

C10 · 查看答案与解析

答案:

  • (a) q·k = (6, 2, 2)。(b) 除以 √d_k = 2,得 (3, 1, 1)。
  • (c) e³ ≈ 20.0855,e ≈ 2.7183,和 ≈ 25.5221,权重 ≈ (0.787, 0.107, 0.107),和为 1。
  • (d) 输出 = 0.787×(1, 0) + 0.107×(0, 1) + 0.107×(2, 2) ≈ (1.000, 0.320)。
  • (e) 不缩放 softmax(6, 2, 2) ≈ (0.965, 0.018, 0.018),几乎全给 k₁;缩放后分布更平缓。

English:Scores (6, 2, 2), scaled by √d_k = 2 to (3, 1, 1); softmax gives about (0.787, 0.107, 0.107); the output is the weighted sum of the values, about (1.000, 0.320). Without scaling the weights would be about (0.965, 0.018, 0.018), which is much more peaked.

易错点:除以 d_k 而不是 √d_k;输出是 value 的加权和,不是 key。

回查:L07 p.21、p.23;T03 p.8。L07e Transformer 自注意力 · T03a 注意力与多头代码

下一页 →
Q07 Final 选择题|D 第 4–7 讲

个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。