Final 计算题|E 第 8–10 讲
非官方期末自拟计算题 10 道:GCN 一层手算与参数量、DiffPool、自编码器误差、转置卷积、胶囊 squash 与动态路由、VAE KL 与重参数化、GAN 损失、扩散前向,附英文解答与课件页码。
手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。
使用边界:期末复习,非官方材料。课程信息写期末闭卷、3 小时、选择题加数学计算题;日期、地点与范围尚未公布,以讲师通知和课堂公告为准。本页是按第 8–10 讲课件自拟的计算题,数值重新设计,方法与约定照课件。
复习资料:考试形式与范围地图 · 闭卷公式默写表 · 算法横向对照
先做题再看解析:A 计算:第 1–3 讲 · B 选择:第 1–3 讲 · C 计算:第 4–7 讲 · D 选择:第 4–7 讲 · E 计算:第 8–10 讲 · F 选择:第 8–10 讲
怎么做:闭卷、限时,先把每一步写在纸上,再展开对照;算错的题回到回查里的讲义页重做一遍。
E 计算题:第 8–10 讲
题面用英文,约定写在题面里;log 一律取自然对数。E1–E3 第 8 讲,E4–E7 第 9 讲,E8–E10 第 10 讲。数值与 R02–R04 的练习题不重复。第 11、12 讲待补。
E1 · One GCN layer by hand
An undirected graph has 4 nodes A, B, C, D and edges A–B, A–C, B–C, C–D. Node features (rows of X): A (2, 0), B (0, 1), C (1, 1), D (0, 2). W = [[1, −1], [1, 1]] (row k belongs to input feature k). Use H = ReLU(ÂXW) with  = D̃^-1/2 à D̃^-1/2, à = A + I, D̃ = D + I, no bias. (a) Give D and D̃. (b) Compute Â. (c) Compute H.
E1 · 查看答案与解析
答案:(a) D = diag(2, 2, 3, 1),加自环后 D̃ = diag(3, 3, 4, 2)。(b) à 中为 1 的位置填 1/√(d̃_i d̃_j):A-A、A-B、B-B 为 1/3 ≈ 0.3333;A-C、B-C 为 1/√12 ≈ 0.2887;C-C 为 0.25;C-D 为 1/√8 ≈ 0.3536;D-D 为 0.5。行和约 0.9553、0.9553、1.1809、0.8536,不等于 1。(c) ÂX 各行:(0.9553, 0.6220)、(0.9553, 0.6220)、(0.8274, 1.2458)、(0.3536, 1.3536)。乘 W 把 (a, b) 变成 (a + b, −a + b):(1.5774, −0.3333)、(1.5774, −0.3333)、(2.0731, 0.4184)、(1.7071, 1)。ReLU 后 H 各行 (1.5774, 0)、(1.5774, 0)、(2.0731, 0.4184)、(1.7071, 1)。A、B 的邻域 {A, B, C} 与 d̃ 都相同, 两行相同,输出也相同。
English:D̃ = diag(3, 3, 4, 2) and Â_ij = 1/√(d̃_i d̃_j), e.g. Â_AC = 1/√12 ≈ 0.289, Â_CD = 1/√8 ≈ 0.354. ÂXW has rows (1.577, −0.333), (1.577, −0.333), (2.073, 0.418), (1.707, 1.000); ReLU sets the negatives to 0, so H = [[1.577, 0], [1.577, 0], [2.073, 0.418], [1.707, 1.000]].
易错点:用 D 代替 D̃;把 Â 写成按行平均的 1/d̃_i;以为 Â 行和为 1。
回查:L08 p.15、p.20、p.21。L08b 邻居聚合与归一化 · L08c GCN 层
E2 · Parameters of a two-layer GCN
A two-layer GCN y = sigmoid(Â ReLU(ÂXW⁰) W¹) classifies the nodes of a graph with 100 nodes. Each node has 16 input features, the hidden layer has 32 features and each node gets one output. No bias. (a) Give the shapes of Â, W⁰, ÂXW⁰ and y. (b) Count the trainable parameters. (c) Does the count change for a graph with 1000 nodes?
E2 · 查看答案与解析
答案:(a) Â 100×100,W⁰ 16×32,ÂXW⁰ 100×32,W¹ 32×1,y 100×1。(b) 只学 W⁰、W¹:16·32 + 32·1 = 544。(c) 不变,仍是 544:W 由所有节点共用,形状只看特征长度;Â 由图算出,没有可学参数。若每层加偏置(课件公式没有)是 577。
English:W⁰ is 16 × 32 and W¹ is 32 × 1, giving 512 + 32 = 544 trainable parameters. Â is computed from the graph and has no parameters, and the weights are shared by all nodes, so a 1000-node graph still uses 544 parameters.
易错点:把 Â 的元素算进参数;以为每个节点一套 W;y 是每个节点一个输出。
回查:L08 p.20、p.21。L08c GCN 层
E3 · DiffPool with a soft assignment
A path graph A–B–C has A = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]. DiffPool pools it into 2 clusters with S = [[1, 0], [0.5, 0.5], [0, 1]] and Z = [[2, 0], [1, 1], [0, 4]]. (a) Check S. (b) Compute SᵀZ. (c) Compute SᵀAS and explain its entries. (d) Give the shapes.
E3 · 查看答案与解析
答案:(a) 每行非负且和为 1,符合在新簇上按行做的 softmax。(b) c1 = (2, 0) + 0.5·(1, 1) = (2.5, 0.5),c2 = 0.5·(1, 1) + (0, 4) = (0.5, 4.5)。(c) AS 各行 (0.5, 0.5)、(1, 1)、(0.5, 0.5),再左乘 Sᵀ 得 SᵀAS = [[1, 1], [1, 1]]。非对角 1 = 边 A–B 的一半加边 B–C 的一半跨在两簇之间;对角 1 是簇内半条边按两个方向各算 0.5。(d) 都是 2×2,3 个节点缩成 2 个簇。
English:Each row of S sums to 1. Xⁱ⁺¹ = SᵀZ = [[2.5, 0.5], [0.5, 4.5]] and Aⁱ⁺¹ = SᵀAS = [[1, 1], [1, 1]]: the off-diagonal 1 is half of edge A–B plus half of edge B–C crossing the clusters. Both are 2 × 2.
易错点:以为 S 每列和为 1;写成 SASᵀ,得到 3×3;把 SᵀZ 当平均。
回查:L08 p.24、p.25。L08d 图池化与 DiffPool
E4 · Autoencoder error and size
(a) An autoencoder maps x = (0.2, 0.8, 0.5, 1.0) to x’ = (0.4, 0.5, 0.5, 0.6). Compute ℓ = ||x’ − x|| and the squared error. (b) A fully connected autoencoder has widths 6-4-2-4-6 with a bias on every non-input neuron. Count its parameters and give the length of h.
E4 · 查看答案与解析
答案:(a) x’ − x = (0.2, −0.3, 0, −0.4),平方和 0.04 + 0.09 + 0 + 0.16 = 0.29,ℓ = √0.29 ≈ 0.539,平方误差 0.29。(b) 7·4 + 5·2 + 3·4 + 5·6 = 28 + 10 + 12 + 30 = 80;不计偏置 64。h 是中间瓶颈层,长度 2。
English:ℓ = √(0.04 + 0.09 + 0 + 0.16) = √0.29 ≈ 0.539; the squared error is 0.29. The network has 7·4 + 5·2 + 3·4 + 5·6 = 80 parameters (64 without biases), and h has length 2.
易错点:课件的 ℓ 不平方,要开根号;漏掉偏置的 +1;自编码器只比较 x’ 和 x,不用标签。
回查:L09 p.11;L04 p.12–13。L09a 无监督学习与自编码器 · L04b 宽度、深度与输出层
E5 · Transposed convolution
(a) A transposed convolution takes a 5×5 input with a 3×3 kernel, stride 2, padding 1. Give the output size (use (H_in − 1)·s − 2p + k), and the size after an ordinary convolution with the same settings. (b) As in the lecture’s animation, pad the 2×2 input [[2, 1], [0, 3]] with 2 rings of zeros and slide K = [[0, 1, 0], [1, 2, 1], [0, 1, 0]] with stride 1. Compute the 4×4 output.
E5 · 查看答案与解析
答案:(a) (5 − 1)·2 − 2 + 3 = 9,输出 9×9;普通卷积 ⌊(9 + 2 − 3)/2⌋ + 1 = 5,回到 5×5。(b) 补零后 6×6 中间是 [[2, 1], [0, 3]],核逐格相乘相加。例:第 2 行第 2 列窗口里 2 对核中心(权重 2)、1 对中心右侧(权重 1)、3 对右下角(权重 0),得 5。输出 [[0, 2, 1, 0], [2, 5, 7, 1], [0, 5, 7, 3], [0, 0, 3, 0]]。自查:输出和 36 = 输入和 6 × 核和 6。
English:(a) (5 − 1)·2 − 2·1 + 3 = 9, so 5×5 becomes 9×9; the ordinary convolution gives ⌊(9 + 2 − 3)/2⌋ + 1 = 5. (b) Padding to 6×6 and sliding K gives [[0, 2, 1, 0], [2, 5, 7, 1], [0, 5, 7, 3], [0, 0, 3, 0]], whose entries sum to 36 = 6 × 6.
易错点:对转置卷积套普通卷积公式;公式里的 p 是从输出边上减;动画每边补 k − 1 = 2 圈 0。
回查:L09 p.12。L09b 卷积自编码器
E6 · Capsule: transform, sum and squash
(a) u_1 = (0.5, 1, 1, 0.5), W_1 = [[1, 0, 0, 1], [0, 1, 1, 0], [0, 0, 2, 0]], û_2 = (3, 0, 2), c_1j = c_2j = 0.5. Compute û_1, s_j, v_j = ||s_j||²/(1 + ||s_j||²) · s_j/||s_j|| and its length. (b) A layer has 5 input capsules of dimension 8 and 2 output capsules of dimension 4. Count the transformation parameters without sharing (one W per pair) and with one W_i per input.
E6 · 查看答案与解析
答案:(a) û_1 = (0.5 + 0.5, 1 + 1, 2·1) = (1, 2, 2)。s_j = 0.5·(1, 2, 2) + 0.5·(3, 0, 2) = (2, 1, 2),||s_j|| = 3。长度系数 9/10 = 0.9,v_j = 0.9·(2, 1, 2)/3 = (0.6, 0.3, 0.6),长度 0.9,方向不变。(b) 一个 W 是 4×8 = 32;不共享 5·2·32 = 320,共享 5·32 = 160。
English:û_1 = (1, 2, 2), s_j = (2, 1, 2), ||s_j|| = 3, so v_j = 9/10 · (2, 1, 2)/3 = (0.6, 0.3, 0.6), of length 0.9. Each W is 4 × 8 = 32 numbers: 320 parameters without sharing, 160 with one W_i per input.
易错点:漏除 ||s_j||,长度变成 2.7;系数写成 ||s||/(1 + ||s||) = 0.75;W 形状写成 8×4。
回查:L09 p.17、p.18。L09c 胶囊层
E7 · Dynamic routing, two iterations
One output capsule j receives û_1 = (1, 1), û_2 = (2, 2), û_3 = (−3, 0). Use the lecture’s routing: b starts at 0, c = softmax(b) over the inputs i, s_j = Σ c_ij û_i, v_j = squash(s_j), b_ij ← b_ij + û_i · v_j. (a) Run iteration 1. (b) Give c, s_j and v_j in iteration 2. Which input gains weight?
E7 · 查看答案与解析
答案:(a) c = (1/3, 1/3, 1/3),s = (0, 1),||s|| = 1,系数 0.5,v = (0, 0.5)。内积 0.5、1、0,b = (0.5, 1, 0)。(b) e^0.5 ≈ 1.6487,e^1 ≈ 2.7183,e^0 = 1,和 ≈ 5.3670,c ≈ (0.3072, 0.5065, 0.1863)。s ≈ (0.7612, 1.3202),||s|| ≈ 1.5239,系数 ≈ 0.6990,v ≈ (0.3492, 0.6055)。û_2 与上一轮 v 的内积最大(1),权重升到 0.5065;û_3 与上一轮的 v 垂直,权重降到 0.1863。
English:Iteration 1: c = (1/3, 1/3, 1/3), s_j = (0, 1), v_j = (0, 0.5); the agreements 0.5, 1, 0 give b = (0.5, 1, 0). Iteration 2: c ≈ (0.307, 0.507, 0.186), s_j ≈ (0.761, 1.320), v_j ≈ (0.349, 0.606). û_2, which agrees most with v_j, gains weight; û_3 loses weight.
易错点:按原论文在输出 j 之间做 softmax(只有一个输出时 c 恒为 1);每轮把 b 清零;用 s 代替 v 算内积。
回查:L09 p.18–20。L09d 动态路由与 CapsNet
E8 · VAE: KL and reparameterization
A VAE has J = 2. The encoder outputs μ = (0.5, −1) and σ² = (1, 0.25). (a) Compute KL = Σ_j ½[−log σ_j² + μ_j² + σ_j² − 1]. (b) With ε = (−0.4, 1.2), compute z = μ + σ ⊙ ε, ∂z₂/∂μ₂ and ∂z₂/∂σ₂.
E8 · 查看答案与解析
答案:(a) 第 1 维 ½(0 + 0.25 + 1 − 1) = 0.125;第 2 维 ½(1.3863 + 1 + 0.25 − 1) ≈ 0.8181;合计 ≈ 0.9431。(b) σ = (1, 0.5),z = (0.5 − 0.4, −1 + 0.6) = (0.1, −0.4)。∂z₂/∂μ₂ = 1,∂z₂/∂σ₂ = ε₂ = 1.2,梯度能穿过采样传回编码器。
English:KL = ½(0 + 0.25 + 1 − 1) + ½(1.386 + 1 + 0.25 − 1) = 0.125 + 0.818 ≈ 0.943. With σ = (1, 0.5), z = (0.1, −0.4); ∂z₂/∂μ₂ = 1 and ∂z₂/∂σ₂ = ε₂ = 1.2, so gradients reach μ and σ.
易错点:KL 里代 σ 而不是 σ²;重参数化里用了 σ²;漏对 J 维求和。
回查:L10 p.13、p.14。L10a 概率编码与 VAE
E9 · GAN losses before and after an update
D(x) = 0.8 and D(x̂) = 0.3. Use ℓ_D = −log D(x) − log(1 − D(x̂)) and ℓ_G = log(1 − D(x̂)). (a) Compute both losses. (b) After a generator update D(x̂) = 0.6 and D(x) = 0.8. Compute both again; which player improved?
E9 · 查看答案与解析
答案:(a) ℓ_D = −ln 0.8 − ln 0.7 ≈ 0.2231 + 0.3567 ≈ 0.580;ℓ_G = ln 0.7 ≈ −0.357。(b) ℓ_G = ln 0.4 ≈ −0.916,下降,生成器变好;ℓ_D ≈ 0.2231 + 0.9163 ≈ 1.139,上升,判别器变差。
English:ℓ_D = −ln 0.8 − ln 0.7 ≈ 0.580 and ℓ_G = ln 0.7 ≈ −0.357. After the update ℓ_G = ln 0.4 ≈ −0.916 (the generator improved) and ℓ_D ≈ 1.139 (the discriminator got worse).
易错点:ℓ_G 多加负号;假图项写成 −log D(x̂);以为生成器要把 D(x̂) 压向 0。
回查:L10 p.21、p.22。L10b GAN
E10 · Diffusion forward: one step and closed form
x0 = 1.5, toy schedule β_1 = 0.19, β_2 = 0.36 (real schedules keep β_t < 0.02). (a) Mean and variance of q(x_1 | x0). (b) ᾱ_2 and q(x_2 | x0). (c) Check (b) by applying the second step to x_1. (d) Compute x_2 = √ᾱ_2 x0 + √(1 − ᾱ_2) ε for ε = 0.5.
E10 · 查看答案与解析
答案:(a) 均值 √0.81·1.5 = 1.35,方差 0.19。(b) ᾱ_2 = 0.81·0.64 = 0.5184,√ᾱ_2 = 0.72,均值 1.08,方差 1 − 0.5184 = 0.4816。(c) 均值 0.8·1.35 = 1.08;方差 0.64·0.19 + 0.36 = 0.1216 + 0.36 = 0.4816,与 (b) 一致。(d) √0.4816 ≈ 0.6940,x_2 ≈ 1.08 + 0.3470 ≈ 1.427。
English:q(x_1 | x0) = N(1.35, 0.19). ᾱ_2 = 0.81 × 0.64 = 0.5184, so q(x_2 | x0) = N(0.72 · 1.5, 0.4816) = N(1.08, 0.4816); two single steps give mean 0.8 · 1.35 = 1.08 and variance 0.64 · 0.19 + 0.36 = 0.4816. With ε = 0.5, x_2 ≈ 1.08 + 0.694 · 0.5 ≈ 1.427.
易错点:均值系数用 ᾱ_t 而不是 √ᾱ_t;方差写成 √β_t;逐步合成时忘了把上一步方差乘 (1 − β_t)。
回查:L10 p.33、p.34、p.38。L10c 扩散模型前向
个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。