MLP 与 CNNMLPs & CNNs
16.3 节教会了你给任何计算图求梯度,这一节回答下一个问题:该搭一张什么样的图?多层感知机(MLP)把线性层和弯折的激活函数一层层叠起来,原则上能逼近任何连续函数,适合关节角、力、速度这类“向量”输入;卷积网络(CNN)在图像上共享同一组小权重,天生“平移等变”,用少得多的参数从相机图像里认出东西。你会亲手训练一个构型空间里的碰撞分类器,看深度为什么比宽度划算,摆弄卷积核、步长、填充与感受野,并看到 55 个参数的 CNN 在没见过的位置上打败 2371 个参数的 MLP。 Lesson 16.3 taught you to differentiate any computational graph; this lesson asks the next question: which graph should you build? A multilayer perceptron (MLP) stacks linear layers and bending activation functions and can in principle approximate any continuous function; it is the network for "vector" inputs such as joint angles, forces and velocities. A convolutional network (CNN) reuses one small set of weights all over an image, is translation-equivariant by construction, and recognises things in camera images with far fewer parameters. You will train a collision classifier in configuration space, see why depth is cheaper than width, play with kernels, stride, padding and receptive fields, and watch a 55-parameter CNN beat a 2371-parameter MLP at positions it has never seen.
1为什么机器人需要它Why robots need this
机器人每时每刻都要把一串读数变成一个判断或一个动作:七个关节角 → “这个姿势会不会撞到架子?”;关节的位置误差和速度历史 → “电机实际会出多大力矩?”;一张 640×480 的相机图像 → “杯子在哪里,夹爪该往哪边走?”。16.1 节的线性模型和逻辑回归只能画直线(或者在你手工设计好的特征上画直线);而上面这些映射弯弯曲曲,人也写不出合适的特征。神经网络的承诺是:特征也一起学。
16.3 节已经解决了“怎么学”:只要计算图由可微的小块组成,反向传播就能以大约三次前向计算的代价(一次前向加约两倍的反向)算出损失对所有参数的梯度,再交给 6.3 节的 Adam。剩下的问题是“学什么样的函数族”,也就是网络架构。架构决定了网络在见到数据之前就“相信”什么,这叫归纳偏置(inductive bias)。本节讲两种最基本、在机器人里也最常见的架构:
- 多层感知机(MLP):对输入向量的各个分量一视同仁,什么也不假设。它是本体感觉(关节角、速度、力)上的策略、动力学模型、价值函数的标准选择,也是后面 Transformer、扩散模型里反复出现的积木。
- 卷积网络(CNN):假设输入是一张图,同一个图案出现在哪里都是同一个图案。这个假设让它参数少、数据效率高,十多年来一直是机器人视觉编码器的主力(ResNet-18 至今仍常见于模仿学习策略里)。
At every instant a robot turns a list of readings into a judgement or an action: seven joint angles → "will this pose hit the shelf?"; a joint's position error and velocity history → "what torque will the motor really produce?"; one 640×480 camera image → "where is the cup, and which way should the gripper go?". The linear and logistic models of Lesson 16.1 can only draw straight lines (or straight lines on features you designed by hand); the maps above are curved, and nobody can write down the right features for them. The promise of neural networks is: learn the features too.
Lesson 16.3 already solved "how to learn": as long as the computational graph is made of differentiable pieces, backpropagation gives the gradient of the loss with respect to every parameter for about the cost of three forward passes (one forward pass plus a backward pass about twice as expensive), and the Adam of Lesson 6.3 takes it from there. What remains is "which family of functions to learn", that is, the architecture. The architecture decides what the network "believes" before it has seen any data, its inductive bias. This lesson covers the two most basic architectures, which are also the most common in robotics:
- Multilayer perceptrons (MLPs) treat all components of the input vector alike and assume nothing. They are the standard choice for policies, dynamics models and value functions on proprioception (joint angles, velocities, forces), and the building block that keeps reappearing inside transformers and diffusion models later on.
- Convolutional networks (CNNs) assume the input is an image and that a pattern is the same pattern wherever it appears. That assumption makes them small and data-efficient, and for over a decade they have been the workhorse visual encoders of robotics (a ResNet-18 is still a common sight inside imitation-learning policies).
2直觉:一个神经元是一个“折”Intuition: a neuron is a hinge
一个神经元做两件事:先算输入的加权和 \(z=\mathbf w^\top\mathbf x+b\)(和 16.1 节的线性模型一模一样),再把 \(z\) 送进一个非线性的激活函数。今天最常用的是 ReLU:\(\operatorname{ReLU}(z)=\max(0,z)\),负的砍成 0,正的原样通过。一维输入时,\(\operatorname{ReLU}(x-k)\) 的图像就是一根在 \(x=k\) 处“折了一下”的铰链:左边平躺,右边斜上。
一层有 \(N\) 个这样的神经元,输出层再把它们按权重加起来,结果是一条在 \(N\) 个地方折弯的折线。折点越多,折线就能越贴近任何一条光滑或不光滑的曲线。下面的目标都来自机器人:单个连杆的重力矩 \(\cos q\)、带硬限位的关节(限位前是软弹簧,撞上限位后突然变硬),以及库仑加黏性摩擦(在速度为零处跳变)。折点(下方蓝色圆圈)可以拖动;每次改动后,输出层的权重用最小二乘(1.13 节)直接解出,相当于把隐藏层固定住、只训练最后一层。
A neuron does two things: it forms a weighted sum of its inputs \(z=\mathbf w^\top\mathbf x+b\) (exactly the linear model of Lesson 16.1), then feeds \(z\) through a nonlinear activation function. The most common one today is the ReLU, \(\operatorname{ReLU}(z)=\max(0,z)\): negatives are cut to 0, positives pass unchanged. With one input, the graph of \(\operatorname{ReLU}(x-k)\) is a hinge that "bends" at \(x=k\): flat on the left, sloping up on the right.
A layer of \(N\) such neurons, summed with weights by the output layer, gives a polyline that bends in \(N\) places. The more bends, the more closely the polyline can follow any curve, smooth or not. The targets below all come from robots: the gravity torque \(\cos q\) of a single link, a joint with hard stops (a soft spring until it hits the limit, then suddenly stiff), and Coulomb-plus-viscous friction (which jumps at zero velocity). The kinks (blue circles at the bottom) can be dragged; after every change, the output-layer weights are solved directly by least squares (Lesson 1.13), which amounts to freezing the hidden layer and training only the last one.
- 选“重力矩 cos q”,增加 \(N\) 直到均方根误差低于 0.005。注意每把 \(N\) 翻一倍,误差大约变成四分之一(8 个单元约 0.016,16 个约 0.004)。Choose "gravity torque cos q" and raise \(N\) until the RMS error is below 0.005. Notice that each doubling of \(N\) cuts the error to about a quarter (about 0.016 with 8 units, 0.004 with 16).
- 选“硬限位关节”,把 \(N\) 设为 2,再把两个折点拖到目标真正折弯的地方(\(q=\pm1.2\) rad):均方根误差降到 0.02 以下。两个放对位置的单元,胜过 16 个均匀撒开的单元(均方根误差约 0.021)。Choose "joint with hard stops", set \(N\) to 2 and drag the two kinks to where the target really bends (\(q=\pm1.2\) rad): the RMS error drops below 0.02. Two units in the right places beat 16 spread evenly (RMS error about 0.021).
没有激活函数,多少层线性层叠起来还是一个线性函数(矩阵乘矩阵还是矩阵);有了“折”,网络才能表示弯曲的东西。一个隐藏层、足够多的单元就能逼近任意连续函数,这是万能逼近定理。但右图也说明了它没说的事:需要多少单元取决于函数有多“不光滑”,光滑目标的误差按 \(N^{-2}\) 下降,跳变的目标只按 \(N^{-1/2}\),最大误差始终卡在跳变高度的一半(约 0.65 到 0.8)附近。训练(梯度下降)真正做的,就是把这些折点挪到该折的地方。
Without an activation, any number of stacked linear layers is still one linear function (a matrix times a matrix is a matrix); the "bend" is what lets a network represent anything curved. One hidden layer with enough units can approximate any continuous function: the universal approximation theorem. But the right panel shows what the theorem does not say: how many units you need depends on how "unsmooth" the function is. The error on the smooth target falls like \(N^{-2}\), on the jump only like \(N^{-1/2}\), and the maximum error there stays stuck near half the jump (about 0.65 to 0.8). What training (gradient descent) really does is move the kinks to where the function bends.
3动手:训练一个构型空间碰撞分类器Play: train a collision classifier in configuration space
7.2 节用采样画出了 C-障碍物:对每个关节角组合 \(\mathbf q=(q_1,q_2)\) 问一次“两根连杆碰到障碍物了吗?”。一次几何检测不贵,但规划器(14.2 节的 RRT、14.3 节的轨迹优化)每秒要问成千上万次,轨迹优化还想要“离碰撞有多近”的梯度。一个常见的做法是用一个小网络学出碰撞概率 \(p(\mathbf q)\):查询只是一次前向计算,对 \(\mathbf q\) 的梯度由反向传播免费送上。
下图是一个真实在你浏览器里训练的 MLP。数据是 500 个随机关节角,标签由几何检测给出(臂长 1.0 m 和 0.8 m,障碍物是一个圆);损失是 16.1 节的二元交叉熵,优化器是 Adam(学习率 0.01,每批 32 个样本)。测试集是覆盖整个 \([-\pi,\pi]^2\) 的 36×36 网格。先用默认的 2 层×16 单元网络按“训练”,再试试别的架构。把 C-空间里的琥珀色手柄拖来拖去,左边的手臂会跟着动;把左边的红色障碍物拖到别处,标签会立刻重算,网络接着学。
Lesson 7.2 drew the C-obstacle by sampling: for each joint-angle pair \(\mathbf q=(q_1,q_2)\) it asked "do the two links touch the obstacle?". One geometric check is cheap, but a planner (RRT in Lesson 14.2, trajectory optimization in 14.3) asks thousands of times a second, and trajectory optimization would also like the gradient of "how close to collision". A common trick is to learn the collision probability \(p(\mathbf q)\) with a small network: a query is one forward pass, and the gradient with respect to \(\mathbf q\) comes free from backpropagation.
The figure below is an MLP really training in your browser. The data are 500 random joint-angle pairs labelled by the geometric check (links of 1.0 m and 0.8 m, a circular obstacle); the loss is the binary cross-entropy of Lesson 16.1 and the optimizer Adam (learning rate 0.01, batches of 32). The test set is a 36×36 grid covering all of \([-\pi,\pi]^2\). Press "Train" with the default 2 layers × 16 units first, then try other architectures. Drag the amber handle in C-space and the arm on the left follows; drag the red obstacle on the left somewhere else and the labels are recomputed at once while the network keeps learning.
- 架构选“线性(无隐藏层)”,训练 1000 步以上:测试准确率停在 81.8%,正好是“永远回答无碰撞”的水平:236 个碰撞格子一个也没认出来。一条直线围不住一块斑。Choose "linear (no hidden layer)" and train for 1000 steps or more: test accuracy stalls at 81.8%, exactly the score of "always answer free": not one of the 236 colliding cells is recognised. A straight line cannot enclose a blob.
- 用带隐藏层的网络训练到测试准确率 ≥ 97.5%,且漏报的碰撞格子不超过 15 个(默认 2×16 网络大约 500 步就能做到;1×4 网络停在 95% 左右)。With any network that has a hidden layer, train until test accuracy is ≥ 97.5% with at most 15 missed collision cells (the default 2×16 network gets there in about 500 steps; the 1×4 network stalls around 95%).
- 把障碍物拖到手臂身后(圆心 \(x<-0.6\),例如 \((-1.2,0.3)\) 附近;若接缝不一致一直很小,把它上下挪一点),用“原始角度”输入训练至少 300 步:C-障碍物跨过了 \(q_1=\pm\pi\) 这条接缝,读数里“接缝不一致”大于 0.2,而这两条边在物理上是同一个姿势。再把输入换成 \((\cos q,\sin q)\) 并训练到测试准确率 ≥ 97%:接缝不一致严格为 0。Drag the obstacle behind the arm (centre at \(x<-0.6\), e.g. near \((-1.2,0.3)\); if the seam mismatch stays small, move it up or down a little) and train at least 300 steps with "raw angles": the C-obstacle now straddles the seam \(q_1=\pm\pi\), and the "seam mismatch" readout exceeds 0.2 although the two edges are physically the same pose. Then switch the input to \((\cos q,\sin q)\) and train to a test accuracy ≥ 97%: the seam mismatch is exactly 0.
(1) 训练准确率和测试准确率很接近:500 个样本对一个二维问题来说足够多,过拟合(16.2 节)不是这里的瓶颈,容量才是。 (2) 打开“第一层直线”:每个第一层 ReLU 只能沿一条直线把平面切成两半,第二层把这些半平面组合成弯曲的区域,这就是“特征也一起学”的样子。 (3) 输入怎么编码和架构一样重要:角度在 \(\pm\pi\) 处绕回来,原始角度让网络把一块障碍物当成两块分别去学;\((\cos q,\sin q)\) 把这个拓扑直接告诉了网络。我们把障碍物放在 \((-1.2,0.3)\) 试过:2×16 网络训练 500 到 3000 步,原始角度漏报 36 到 62 个碰撞格子,\((\cos q,\sin q)\) 只漏报 12 到 19 个。这正是后面“傅里叶特征”的最简单形式(第 7 节)。 (4) 机器人更怕漏报(说没撞其实撞了)而不是误报,所以实际的碰撞网络通常会把阈值调低或在障碍物上加一层安全余量。
(1) Training and test accuracy stay close: 500 samples are plenty for a two-dimensional problem, so overfitting (Lesson 16.2) is not the bottleneck here; capacity is. (2) Turn on "first-layer lines": each first-layer ReLU can only cut the plane in two along a straight line, and the second layer combines these half-planes into curved regions; this is what "learning the features too" looks like. (3) Input encoding matters as much as architecture: angles wrap at \(\pm\pi\), so with raw angles the network must learn one obstacle as two separate pieces; \((\cos q,\sin q)\) hands it the topology directly. We tried it with the obstacle at \((-1.2,0.3)\): training the 2×16 network for 500 to 3000 steps, raw angles missed 36 to 62 colliding cells and \((\cos q,\sin q)\) only 12 to 19. This is the simplest form of the "Fourier features" of Section 7. (4) A robot fears misses (answering free when it is not) far more than false alarms, so real collision networks usually lower the threshold or add a safety margin around obstacles.
4为什么要“深”:每一层把空间折一次Why deep: every layer folds the space once
万能逼近定理只要一个隐藏层,那为什么大家都用很多层?因为同样数量的神经元,串起来比并排放能表示复杂得多的函数。一维里最干净的例子是“帐篷函数” \(g\):两个 ReLU 就能拼出来,它把 \([0,1]\) 先拉上去再折回来。把 \(g\) 自己和自己复合 \(L\) 次(一层的输出喂给下一层),每复合一次,每一段单调的折线都会被再对折一次,折线段数翻倍:\(L\) 层、每层 2 个单元,一共 \(2L\) 个单元,就有 \(2^L\) 段。而一个只有一层、\(n\) 个单元的 ReLU 网络,在一维上最多只有 \(n+1\) 段,要表示同一个函数至少需要 \(2^L-1\) 个单元(Telgarsky 2016)。
The universal approximation theorem needs only one hidden layer, so why does everyone use many? Because the same number of neurons chained in series can represent far more complex functions than placed side by side. The cleanest example in one dimension is the "tent map" \(g\): two ReLUs build it, and it stretches \([0,1]\) up and folds it back down. Compose \(g\) with itself \(L\) times (each layer's output feeds the next) and every composition folds each monotone piece once more, doubling the number of linear pieces: \(L\) layers of 2 units, \(2L\) units in total, give \(2^L\) pieces. A ReLU network with a single layer of \(n\) units has at most \(n+1\) pieces in one dimension, so it needs at least \(2^L-1\) units to represent the same function (Telgarsky 2016).
- 把层数拉到 5 或更多,并叠加“同样 2L 个单元”的浅网络:深网络用 10 个单元画出 32 段锯齿,同样 10 个单元的浅网络几乎只能输出一条 0.5 附近的平线(均方根误差约 0.29,正是锯齿相对其均值的 \(1/\sqrt{12}\))。Raise the depth to 5 or more and overlay the shallow net with "same 2L units": the deep net draws 32 teeth with 10 units, while a shallow net with the same 10 units can do little better than a flat line near 0.5 (RMS error about 0.29, which is the \(1/\sqrt{12}\) of the sawtooth around its mean).
折纸:每一层把输入空间沿几条折痕对折,后面的层在折好的纸上画的一笔,展开后会在很多地方重复出现。 组合:真实世界的函数常常是“部件的部件”:边组成角,角组成把手,把手组成杯子;深网络正好按这种层级去分解。 代价:深度不是免费的,层数多了梯度会消失或爆炸,这正是 16.3 节讲的初始化、归一化和残差连接要解决的问题(残差连接在第 6 节再看一次)。上面的例子也是“最好情况”:随机初始化的深网络实际用到的折段数远少于 \(2^L\),表达能力还得靠训练去兑现。
Origami: each layer folds the input space along a few creases, and one stroke drawn by a later layer on the folded paper reappears in many places once it is unfolded. Composition: real-world functions are often "parts of parts": edges make corners, corners make handles, handles make cups; a deep network decomposes along exactly such a hierarchy. Cost: depth is not free; with many layers gradients vanish or explode, which is what the initialization, normalization and residual connections of Lesson 16.3 are for (residual connections return in Section 6). The example above is also the best case: a randomly initialized deep network uses far fewer than \(2^L\) pieces, and training has to cash in the expressive power.
5从 MLP 到 CNN:卷积 = 共享权重From MLP to CNN: convolution = shared weights
把一张 224×224 的彩色图像拉成向量喂给 MLP,输入有 150,528 维,第一层哪怕只有 1000 个单元也要 1.5 亿个权重。更糟的是,MLP 不知道相邻像素是邻居,也不知道左上角的杯子和右下角的杯子是同一种东西,它得在每个位置分别学一遍。卷积层用两个假设修好了这两点:
- 局部性:一个输出只看输入里一小块 \(k\times k\) 的窗口(比如 3×3)。
- 权重共享:所有位置用同一个窗口权重(卷积核)。核在图像上滑动,每到一处算一次加权和,得到一张“特征图”。
于是参数个数和图像大小无关:一个 3×3 核只有 9 个权重加 1 个偏置。权重共享带来一个漂亮的性质,平移等变:输入平移,输出跟着平移同样的量,“先平移再卷积”等于“先卷积再平移”。下图的输入是一张 16×16 的“相机裁剪”:桌面上一个箱子。试几个经典的核:Sobel-x 对竖直的边(左右亮度变化)有反应,Sobel-y 对水平的边(比如桌沿)有反应。
Flatten a 224×224 colour image into a vector for an MLP and the input has 150,528 dimensions; even a first layer of only 1000 units needs 150 million weights. Worse, the MLP does not know that neighbouring pixels are neighbours, nor that a cup in the top-left corner and a cup in the bottom-right are the same thing: it must learn it separately at every position. A convolutional layer fixes both with two assumptions:
- Locality: an output looks only at a small \(k\times k\) window of the input (say 3×3).
- Weight sharing: every position uses the same window weights (the kernel). The kernel slides over the image, computes a weighted sum at each place, and produces a "feature map".
The parameter count therefore does not depend on the image size: a 3×3 kernel has 9 weights plus 1 bias. Weight sharing brings a beautiful property, translation equivariance: shift the input and the output shifts by the same amount; "shift then convolve" equals "convolve then shift". The input below is a 16×16 "camera crop" of a box on a table. Try the classic kernels: Sobel-x reacts to vertical edges (brightness changing left to right), Sobel-y to horizontal ones (such as the table edge).
- 找一组设置,让输出恰好是 7×7(提示:用第 7 节的公式 \(\lfloor(n+2p-k)/s\rfloor+1\);有不止一种做法)。Find settings that make the output exactly 7×7 (hint: use the formula \(\lfloor(n+2p-k)/s\rfloor+1\) from Section 7; there is more than one way).
- 步长 1、不池化时把箱子平移任意非零像素数:等变误差为 0。然后只打开步长 2 或 2×2 池化中的一个,再平移奇数个像素:输出不再是原输出的平移(误差不为 0)。两个都打开时,总降采样是 4 倍,最大池化对块内小位移的不敏感有时会让误差又变回 0。With stride 1 and no pooling, shift the box by any nonzero number of pixels: the equivariance error is 0. Then switch on either stride 2 or 2×2 pooling (one of them) and shift by an odd number of pixels: the output is no longer a shifted copy (nonzero error). With both on, the total downsampling is 4× and max pooling's insensitivity to small shifts inside a block can bring the error back to 0 for some shifts.
叠起来:感受野Stacking: the receptive field
一层 3×3 卷积只看得见 3×3 的邻域,但卷积层可以叠:第二层的一个输出看的是第一层的 3×3 个输出,而它们每个又看 3×3 个像素,于是第二层“看见”5×5 的输入。一个输出单元最终依赖的那块输入区域叫它的感受野。只叠步长 1 的卷积,感受野每层只长 \(k-1\) 个像素,要看清整只杯子得叠很多层;所以 CNN 会用步长 2 的卷积或池化来降采样:每降一次,后面每层的“一步”在输入上就跨两倍远,感受野按指数增长。另一种办法是空洞卷积(dilated convolution):核的元素之间隔开 \(d\) 个像素,分辨率不降,感受野同样指数增长(WaveNet 和时间卷积网络这样做;17.2 节则用循环网络和状态空间模型处理很长的序列)。下图是一维的示意,从顶层选一个单元,看它的感受野。
One 3×3 convolution sees only a 3×3 neighbourhood, but convolutions stack: an output of the second layer looks at 3×3 outputs of the first, each of which looks at 3×3 pixels, so the second layer "sees" 5×5 of the input. The input region an output unit ultimately depends on is its receptive field. Stacking only stride-1 convolutions grows the receptive field by just \(k-1\) pixels per layer, and seeing a whole cup would take many layers; so CNNs downsample with stride-2 convolutions or pooling: after each downsampling, one "step" of every later layer spans twice as far in the input, and the receptive field grows exponentially. Another way is the dilated convolution: the kernel taps are spread \(d\) pixels apart, the resolution stays, and the receptive field still grows exponentially (WaveNet and temporal convolutional networks do this; Lesson 17.2 handles long sequences with recurrent and state-space models instead). The figure is a one-dimensional sketch: pick a unit in the top layer and see its receptive field.
- 用不超过 4 层,让感受野达到 30 个输入像素以上。普通卷积做不到(5×5 核叠 6 层也只有 25);步长 2 和空洞卷积用 3×3 核 4 层都能到 31,区别在于顶层还剩多少个单元。With at most 4 layers, get a receptive field of 30 input pixels or more. Plain convolutions cannot (even 6 layers of size-5 kernels reach only 25); stride 2 and dilation both reach 31 with 4 layers of size-3 kernels, and the difference is how many units are left in the top layer.
6归纳偏置的威力:CNN 对 MLPInductive bias at work: CNN versus MLP
一台机器人从相机里截出料盘的 12×12 小图,要判断里面的销钉是立着(竖直)、躺着(水平)还是歪着(对角)。图里有像素噪声,销钉亮度也略有变化。为了把归纳偏置的作用放大,我们故意让训练集里的销钉只出现在左半边,测试集全在右半边。
两个网络看同一批数据、用同一个 Adam 训练:
- CNN:4 个 3×3 卷积核 + ReLU → 每个特征图取全局最大值(全局最大池化,回答“这个图案在图里任何地方出现过吗”)→ 一个 4→3 的线性层。共 55 个参数。
- MLP:144 个像素 → 16 个 ReLU → 3 类。共 2371 个参数。
CNN 的卷积在左边学到的“竖条检测器”在右边一样有效(等变),最大池化再把位置扔掉(不变);MLP 的每个权重都绑定在某个像素上,右半边的像素在训练时从来没亮过,它对它们一无所知。本课程共用的网络工具库里没有卷积,本页把它内联实现:最大池化的梯度只流回取到最大值的那个位置,卷积核的梯度是该位置的输入窗口乘上传回来的梯度(第 7 节)。
A robot crops a 12×12 patch of a parts tray from its camera and must decide whether the peg in it is standing (vertical), lying (horizontal) or tilted (diagonal). There is pixel noise, and the peg's brightness varies a little. To magnify the effect of inductive bias we deliberately put the training pegs only in the left half of the patch; the test pegs are all in the right half.
Two networks see the same mini-batches and are trained by the same Adam:
- CNN: 4 kernels of 3×3 + ReLU → the global maximum of each feature map (global max pooling, answering "did this pattern appear anywhere?") → one linear layer 4→3. 55 parameters in all.
- MLP: 144 pixels → 16 ReLUs → 3 classes. 2371 parameters in all.
The "vertical-bar detector" the CNN learns on the left works just as well on the right (equivariance), and the max pooling then discards the position (invariance); every weight of the MLP is tied to one pixel, the right-half pixels never lit up during training, and it knows nothing about them. There is no convolution in the shared network library, so this page implements it inline: the max-pool gradient flows back only to the position that attained the maximum, and the kernel gradient is that position's input window times the gradient sent back (Section 7).
- 用默认设置(训练销钉只在左半边,90 张)训练至少 300 步:CNN 在右半边测试集上 ≥ 95%(默认种子下是 100%),MLP 训练集 100% 但测试集 ≤ 50%(约 34%,和瞎猜差不多)。With the defaults (training pegs only in the left half, 90 images) train at least 300 steps: the CNN scores ≥ 95% on the right-half test set (100% with the default seed), while the MLP reaches 100% on its training set but ≤ 50% on the test set (about 34%, close to guessing).
- 把“训练位置”换成“任意位置”、训练集换成 600 张,再训练至少 1000 步:MLP 的测试准确率升过 50%,但仍比 CNN 低 20 个百分点以上。数据可以弥补一部分缺失的归纳偏置,但要多得多的数据。Switch "training positions" to "anywhere" and the training set to 600 images, then train at least 1000 steps: the MLP's test accuracy rises above 50% but stays more than 20 points below the CNN. Data can make up for part of a missing inductive bias, but it takes far more data.
从 LeNet 到 ResNet:残差连接From LeNet to ResNet: residual connections
FIG 6 的 CNN 只有一层。真实的 CNN 是“卷积 → 非线性 → 降采样”重复很多次:早期的层学到边和角,后面的层把它们组合成部件和物体,最后的特征图分辨率很低、通道很多,每个位置的感受野覆盖大半张图。LeNet-5(LeCun 等 1998)用这个配方认手写数字;AlexNet(Krizhevsky 等 2012)把它放到 GPU 和 ImageNet 上,开启了深度学习的这一轮浪潮;VGG(Simonyan 与 Zisserman 2015)说明堆叠 3×3 小核就够了。
再往深走就撞墙了:He 等人(2016)观察到,在 CIFAR-10 上 56 层的普通 CNN 连训练误差都比 20 层的高,问题不在过拟合,而在于优化。他们的修法极其简单:让每个块学习对输入的修正量,而不是整个映射,\(\mathbf h_{l+1}=\mathbf h_l+F(\mathbf h_l)\)。如果某个块什么也不该做,只需让 \(F\to0\),恒等映射成了“默认值”;反向传播时梯度沿着这条“捷径”直接流回去,不会被几十个雅可比连乘消磨掉(16.3 节)。这就是 ResNet,它让 152 层的网络可以训练,并赢得了 ILSVRC 2015。残差连接后来成了几乎所有深网络的标配,17.3 节的 Transformer 块里你会再次见到它。
The CNN of FIG 6 has a single layer. Real CNNs repeat "convolve → nonlinearity → downsample" many times: early layers learn edges and corners, later layers combine them into parts and objects, and the final feature maps have low resolution, many channels, and a receptive field covering most of the image at every position. LeNet-5 (LeCun et al. 1998) used this recipe to read handwritten digits; AlexNet (Krizhevsky et al. 2012) moved it onto GPUs and ImageNet and started the current wave of deep learning; VGG (Simonyan & Zisserman 2015) showed that stacks of small 3×3 kernels are enough.
Going deeper hit a wall: He et al. (2016) observed that on CIFAR-10 a plain 56-layer CNN had a higher training error than a 20-layer one, so the problem was not overfitting but optimization. Their fix is strikingly simple: let each block learn a correction to its input instead of the whole map, \(\mathbf h_{l+1}=\mathbf h_l+F(\mathbf h_l)\). If a block should do nothing it only needs \(F\to0\): the identity becomes the default. In backpropagation the gradient flows straight back along this "shortcut" instead of being worn down by a product of dozens of Jacobians (Lesson 16.3). This is the ResNet; it made 152-layer networks trainable and won ILSVRC 2015. Residual connections went on to become standard in nearly every deep network; you will meet them again inside the transformer block of Lesson 17.3.
7再看数学The math
提示:把鼠标悬停在公式中带底色的符号上,图中对应的元素会被高亮:FIG 1 的铰链、FIG 2 的第一层直线、FIG 3 的帐篷函数、FIG 4 的卷积窗口、FIG 5 的感受野括号、FIG 6 的最大池化位置、FIG 7 的 softmax 权重。Tip: hover the highlighted symbols in the formulas and the matching elements light up in the figures: the hinges of FIG 1, the first-layer lines of FIG 2, the tent map of FIG 3, the convolution window of FIG 4, the receptive-field bracket of FIG 5, the max-pool positions of FIG 6 and the softmax weights of FIG 7.
多层感知机The multilayer perceptron
一个 \(L\) 层的 MLP 是交替的仿射映射和逐元素激活 \(\sigma\):
An \(L\)-layer MLP alternates affine maps and an elementwise activation \(\sigma\):
参数 \(\theta=\{W^{(l)},\mathbf b^{(l)}\}\),\(W^{(l)}\in\R^{n_l\times n_{l-1}}\),总数 \(\sum_l n_l(n_{l-1}+1)\):FIG 2 默认网络 \([2,16,16,1]\) 有 \(16\cdot3+16\cdot17+1\cdot17=337\) 个。第一层的每一行 \(\mathbf w_i^\top\mathbf x+b_i=0\) 是输入空间里的一个超平面(FIG 2 的青色直线),ReLU 在它一侧为 0、另一侧线性增长;所以一个 ReLU 网络是分片线性函数,每一片是这些超平面切出来的一个区域。最后一层不加激活(回归)或接 sigmoid / softmax(分类,16.1 节)。
为什么必须有非线性:若 \(\sigma\) 是恒等,\(W^{(2)}(W^{(1)}\mathbf x+\mathbf b^{(1)})+\mathbf b^{(2)}=(W^{(2)}W^{(1)})\mathbf x+(W^{(2)}\mathbf b^{(1)}+\mathbf b^{(2)})\),再多层也只是一个仿射映射。常用激活:ReLU \(\max(0,z)\)(导数是 0 或 1,便宜,但单元可能“死掉”:如果它对所有数据都输出 0,梯度也恒为 0);tanh 和 sigmoid(光滑,但两端饱和、梯度变小);GELU、SiLU 这类光滑版 ReLU 常见于 Transformer。FIG 2 里 tanh 和 ReLU 都能学会,只是速度不同。
The parameters are \(\theta=\{W^{(l)},\mathbf b^{(l)}\}\) with \(W^{(l)}\in\R^{n_l\times n_{l-1}}\), \(\sum_l n_l(n_{l-1}+1)\) in total: the default \([2,16,16,1]\) network of FIG 2 has \(16\cdot3+16\cdot17+1\cdot17=337\). Each row of the first layer, \(\mathbf w_i^\top\mathbf x+b_i=0\), is a hyperplane in input space (the cyan lines of FIG 2); the ReLU is 0 on one side and grows linearly on the other, so a ReLU network is a piecewise-linear function, each piece being one region cut out by these hyperplanes. The last layer has no activation (regression) or a sigmoid / softmax (classification, Lesson 16.1).
Why the nonlinearity is essential: if \(\sigma\) were the identity, \(W^{(2)}(W^{(1)}\mathbf x+\mathbf b^{(1)})+\mathbf b^{(2)}=(W^{(2)}W^{(1)})\mathbf x+(W^{(2)}\mathbf b^{(1)}+\mathbf b^{(2)})\), and any number of layers would still be one affine map. Common activations: ReLU \(\max(0,z)\) (derivative 0 or 1, cheap, but a unit can "die": if it outputs 0 on all data its gradient is 0 for good); tanh and sigmoid (smooth, but saturating at both ends, which shrinks gradients); smooth ReLU variants such as GELU and SiLU are common in transformers. In FIG 2 both tanh and ReLU learn the task, at different speeds.
万能逼近,以及它没说的事Universal approximation, and what it does not say
定理(Cybenko 1989 对 sigmoid;Hornik 等 1989;Leshno 等 1993 推广到任何非多项式的激活):对紧集 \(K\subset\R^d\) 上的任意连续函数 \(f\) 和任意 \(\varepsilon>0\),存在一个单隐藏层网络 \(\hat f\) 使 \(\max_{\mathbf x\in K}|f(\mathbf x)-\hat f(\mathbf x)|<\varepsilon\)。一维的 ReLU 情形可以直接构造,FIG 1 就是这个构造:
Theorem (Cybenko 1989 for sigmoids; Hornik et al. 1989; extended by Leshno et al. 1993 to any non-polynomial activation): for any continuous \(f\) on a compact set \(K\subset\R^d\) and any \(\varepsilon>0\) there is a one-hidden-layer network \(\hat f\) with \(\max_{\mathbf x\in K}|f(\mathbf x)-\hat f(\mathbf x)|<\varepsilon\). In one dimension with ReLUs it can be built by hand, and FIG 1 is that construction:
第一行:分段线性插值的每个折点都是一个 ReLU,系数 \(c_i\) 是那里斜率的变化量(\(s_i\) 是第 \(i\) 个折点右侧那一段的斜率)。第二行是插值误差的经典上界(\(\tilde f\) 是在 \(N\) 个等距折点上的分段线性插值,\(f\) 二阶可导),间距 \(h\) 减半误差变成四分之一,这就是 FIG 1 光滑目标的 \(N^{-2}\);最小二乘拟合的均方根误差只会更小。若 \(f\) 有跳变,任何连续的 \(\hat f\) 在跳变附近都要差出跳变高度的一半,最大误差不会趋于 0,均方根误差只按 \(N^{-1/2}\) 下降。
定理没说的有三件事。(1) 多少单元:在 \(d\) 维里,同样的构造需要的单元数随 \(d\) 指数增长(维数灾难),除非函数有可利用的结构(光滑、组合式、低维流形),深度正是利用组合结构的方式。(2) 能不能学到:存在一组好权重,不代表梯度下降能找到它。(3) 能不能泛化:FIG 6 的 MLP 完美拟合了训练集,却在新位置上失败,见 16.2 节。
First line: every kink of the piecewise-linear interpolant is one ReLU, and the coefficient \(c_i\) is the change of slope there (\(s_i\) is the slope of the piece just right of kink \(i\)). The second line is the classic bound on the interpolation error (\(\tilde f\) is the piecewise-linear interpolant on \(N\) evenly spaced kinks, \(f\) twice differentiable): halve the spacing \(h\) and the error drops to a quarter, which is the \(N^{-2}\) of the smooth target in FIG 1; the least-squares fit can only have a smaller RMS error. If \(f\) jumps, every continuous \(\hat f\) is off by half the jump somewhere near it, so the maximum error does not tend to 0 and the RMS error falls only like \(N^{-1/2}\).
Three things the theorem does not say. (1) How many units: in \(d\) dimensions the same construction needs a number of units growing exponentially with \(d\) (the curse of dimensionality), unless the function has structure to exploit (smoothness, compositionality, a low-dimensional manifold); depth is how compositional structure is exploited. (2) Whether it can be learned: that good weights exist does not mean gradient descent finds them. (3) Whether it generalizes: the MLP of FIG 6 fits its training set perfectly and still fails at new positions; see Lesson 16.2.
深度:分段数的指数增长Depth: exponentially many pieces
\(g\) 把 \([0,a]\) 线性拉到 \([0,1]\),再把 \([a,1]\) 折回 \([1,0]\)。如果 \(g^{\circ(L-1)}\) 有 \(m\) 段、每段都单调地扫过整个 \([0,1]\),那么再复合一次,每段都恰好在取值 \(a\) 处多出一个折点,段数变成 \(2m\):归纳得 \(g^{\circ L}\) 有 \(2^L\) 段。另一方面,\(n\) 个单元的单隐藏层 ReLU 网络是 \(n\) 个铰链之和,一维上至多 \(n\) 个折点、\(n+1\) 段,所以表示 \(g^{\circ L}\) 至少需要 \(2^L-1\) 个单元。更一般地,宽 \(w\)、深 \(L\) 的 ReLU 网络的线性区域数可以随 \(L\) 指数增长,而浅网络只随宽度多项式增长。
\(g\) stretches \([0,a]\) linearly onto \([0,1]\) and folds \([a,1]\) back onto \([1,0]\). If \(g^{\circ(L-1)}\) has \(m\) pieces, each sweeping monotonically across all of \([0,1]\), then one more composition adds exactly one kink inside each piece (where its value equals \(a\)), giving \(2m\) pieces; by induction \(g^{\circ L}\) has \(2^L\). On the other hand, a one-hidden-layer ReLU network with \(n\) units is a sum of \(n\) hinges: at most \(n\) kinks and \(n+1\) pieces in one dimension, so it needs at least \(2^L-1\) units to represent \(g^{\circ L}\). More generally, the number of linear regions of a ReLU network of width \(w\) and depth \(L\) can grow exponentially in \(L\), whereas a shallow network's grows only polynomially with width.
卷积层The convolutional layer
输入有 \(C_{\rm i}\) 个通道(RGB 是 3),输出有 \(C_{\rm o}\) 个通道,核大小 \(k\),步长 \(s\),零填充 \(p\):
With \(C_{\rm i}\) input channels (3 for RGB), \(C_{\rm o}\) output channels, kernel size \(k\), stride \(s\) and zero padding \(p\):
注意三点。(1) 深度学习里的“卷积”其实是互相关:核没有翻转(4.7 节贝叶斯滤波里的卷积、信号处理里的卷积都会翻转)。核是学出来的,翻不翻都一样,所以没人在意。(2) 参数个数与图像尺寸 \(n\) 无关:64 进 64 出的 3×3 卷积层有 \(64(64\cdot9+1)=36{,}928\) 个参数,无论图是 64×64 还是 640×480;把同样大小的 64×64×64 特征图用全连接层映射到同样大小,需要 \((64^3)^2\approx6.9\times10^{10}\) 个权重。(3) 把卷积写成矩阵乘法,它是一个巨大但稀疏、并且沿对角线重复的矩阵(Toeplitz 结构):卷积层就是一个“被约束得很厉害”的全连接层,约束本身就是先验。
平移等变:记 \(T_{\mathbf d}\) 为把图像平移 \(\mathbf d\) 个像素,则步长 1 时(忽略边界)\(K\star(T_{\mathbf d}X)=T_{\mathbf d}(K\star X)\);把上式里的 \(j\) 换成 \(j-d\) 立即得到。步长 \(s\) 时,只有 \(\mathbf d\) 是 \(s\) 的整数倍才有 \(K\star_s(T_{\mathbf d}X)=T_{\mathbf d/s}(K\star_s X)\),其他平移在输出上没有对应的整数平移,FIG 4 的误差就来自这里(这种“混叠”效应可参见 Zhang 2019)。边界也会破坏等变:零填充的那一圈对靠边的物体和对中间的物体不一样,FIG 6 因此让销钉离边至少 1 个像素。
池化:\(2\times2\) 最大池化取每个 2×2 块的最大值,相当于步长 2 的降采样,对块内的小位移不敏感。全局池化对整张特征图取最大(或平均),\(z_c=\max_{i,j}\hl{pool}{Y[c,i,j]}\),得到对平移不变的特征:等变的卷积 + 不变的池化 = 不变的分类器,FIG 6 的 CNN 正是这样。它的梯度:\(\partial z_c/\partial Y[c,i,j]\) 只在取到最大值的 \((i^*,j^*)\) 为 1,所以 \(\partial L/\partial K[c,u,v]=\frac{\partial L}{\partial z_c}\,\mathbb 1[Y>0]\,X[i^*+u,j^*+v]\)。
Three remarks. (1) The "convolution" of deep learning is really a cross-correlation: the kernel is not flipped (the convolution of the Bayes filter in Lesson 4.7, and of signal processing, does flip). Since the kernel is learned, flipped or not makes no difference and nobody minds. (2) The parameter count does not depend on the image size \(n\): a 3×3 layer with 64 channels in and out has \(64(64\cdot9+1)=36{,}928\) parameters whether the image is 64×64 or 640×480; mapping a 64×64×64 feature map to one of the same size with a fully connected layer would take \((64^3)^2\approx6.9\times10^{10}\) weights. (3) Written as a matrix product, a convolution is a huge but sparse matrix that repeats along its diagonals (Toeplitz structure): a convolutional layer is a heavily constrained fully connected layer, and the constraint is the prior.
Translation equivariance: let \(T_{\mathbf d}\) shift an image by \(\mathbf d\) pixels; with stride 1 (ignoring borders) \(K\star(T_{\mathbf d}X)=T_{\mathbf d}(K\star X)\), which follows at once by substituting \(j-d\) for \(j\) above. With stride \(s\), only shifts \(\mathbf d\) that are multiples of \(s\) give \(K\star_s(T_{\mathbf d}X)=T_{\mathbf d/s}(K\star_s X)\); other shifts have no integer counterpart in the output, and that is the error in FIG 4 (on this aliasing see Zhang 2019). Borders break equivariance too: the ring of zero padding treats an object at the edge differently from one in the middle, which is why FIG 6 keeps its pegs at least 1 pixel from the border.
Pooling: \(2\times2\) max pooling keeps the maximum of each 2×2 block, a stride-2 downsampling that ignores small shifts inside the block. Global pooling takes the maximum (or mean) over the whole feature map, \(z_c=\max_{i,j}\hl{pool}{Y[c,i,j]}\), giving a feature that is invariant to translation: equivariant convolution + invariant pooling = an invariant classifier, which is exactly the CNN of FIG 6. Its gradient: \(\partial z_c/\partial Y[c,i,j]\) is 1 only at the maximizing \((i^*,j^*)\), so \(\partial L/\partial K[c,u,v]=\frac{\partial L}{\partial z_c}\,\mathbb 1[Y>0]\,X[i^*+u,j^*+v]\).
感受野The receptive field
\(j_l\) 是第 \(l\) 层相邻两个单元在输入上相隔的像素数(“跳距”),\(d_l\) 是空洞率。步长 1、\(k=3\):\(r_L=1+2L\),线性增长;每层步长 2:\(r_L=1+2(2^L-1)\),4 层是 31;空洞率 \(1,2,4,8\):同样是 31,但分辨率不降。这也解释了 VGG 的选择:三层 3×3 的感受野是 7×7,和一层 7×7 一样,却只有 \(27C^2\) 而不是 \(49C^2\) 个权重,中间还多了两次非线性。需要注意,这是理论感受野,有效感受野(真正对输出有明显影响的区域)通常小得多,中心像素的影响最大(Luo 等 2016)。
\(j_l\) is the distance in input pixels between neighbouring units of layer \(l\) (the "jump"), and \(d_l\) is the dilation. Stride 1 with \(k=3\): \(r_L=1+2L\), linear growth; stride 2 at every layer: \(r_L=1+2(2^L-1)\), 31 after 4 layers; dilations \(1,2,4,8\): also 31, without losing resolution. This also explains the choice in VGG: three 3×3 layers have a 7×7 receptive field like one 7×7 layer, but \(27C^2\) instead of \(49C^2\) weights, plus two extra nonlinearities in between. Note that this is the theoretical receptive field; the effective receptive field (the region that noticeably affects the output) is usually much smaller and dominated by the centre (Luo et al. 2016).
残差块与归一化Residual blocks and normalization
普通网络的雅可比是 \(\prod_m \partial F/\partial\mathbf h_m\),几十个小于 1 的因子相乘就消失了;残差网络展开后含有恒等项 \(I\),总有一条梯度通路不经过任何权重。ResNet 的块 \(F\) 通常是“卷积–批归一化–ReLU–卷积–批归一化”,更深的版本用 1×1–3×3–1×1 的“瓶颈”块减少计算。批归一化(BatchNorm)用一个小批量的均值和方差归一化每个通道,推理时改用训练时的滑动平均,这在机器人上是个常见坑:批量很小或训练与部署的数据分布不同时,统计量不准,所以不少机器人策略改用不依赖批量的 GroupNorm 或 LayerNorm(归一化层见 16.3 节)。
A plain network's Jacobian is \(\prod_m \partial F/\partial\mathbf h_m\), and dozens of factors below 1 make it vanish; the residual product, expanded, contains the identity term \(I\), so there is always a gradient path through no weights at all. A ResNet block \(F\) is usually "conv–batch norm–ReLU–conv–batch norm", and deeper versions use 1×1–3×3–1×1 "bottleneck" blocks to save compute. Batch normalization normalizes each channel with the mean and variance of a mini-batch and switches to running averages from training at inference; this is a common trap in robotics: with tiny batches, or when deployment data differ from training data, the statistics are off, so many robot policies use batch-independent GroupNorm or LayerNorm instead (normalization layers: Lesson 16.3).
坐标 MLP 与傅里叶特征Coordinate MLPs and Fourier features
当 MLP 的输入是低维坐标(关节角、时间、空间位置)时,输入怎么编码会决定它能学多细的东西。FIG 2 的 \((\cos q,\sin q)\) 解决了角度的周期性;更一般的做法是把坐标送进一组不同频率的正弦:
When the input of an MLP is a low-dimensional coordinate (joint angle, time, spatial position), how it is encoded decides how fine a detail the network can learn. The \((\cos q,\sin q)\) of FIG 2 took care of the periodicity of angles; the general recipe feeds the coordinate through sines of several frequencies:
\(B\) 的元素可以随机取自 \(\mathcal N(0,\sigma^2)\),也可以沿每个坐标轴取 \(2^k\)。直接吃坐标的 MLP 有“谱偏置”:先学低频,高频学得极慢。Tancik 等(2020)证明随机傅里叶特征能缓解它,\(\sigma\) 控制网络能表达的最高频率;NeRF 用的是按 \(2^k\) 递增的频率。12.4 节的 FIG 3 已经现场演示了这个效果,本节不再重复;17.3 节 Transformer 的正弦位置编码也是同一个思想。
The entries of \(B\) can be drawn from \(\mathcal N(0,\sigma^2)\) or set to \(2^k\) along each axis. An MLP fed raw coordinates has a "spectral bias": it learns low frequencies first and high frequencies extremely slowly. Tancik et al. (2020) showed that random Fourier features fix this, with \(\sigma\) controlling the highest frequency the network can express; NeRF uses frequencies growing as \(2^k\). FIG 3 of Lesson 12.4 already demonstrated the effect live, so it is not repeated here; the sinusoidal positional encoding of the transformer in Lesson 17.3 is the same idea.
MLP 的先验是“没有先验”,所以它通用但吃数据;CNN 的先验是“局部 + 平移对称”,所以它在图像上又小又准;选架构就是选先验,而先验对了,数据和参数都能省下一个数量级。
The prior of an MLP is "no prior", so it is general but data-hungry; the prior of a CNN is "local + translation-symmetric", so on images it is small and accurate; choosing an architecture is choosing a prior, and the right prior saves an order of magnitude in data and parameters.
8机器人中的它:从特征图到坐标In robotics: from feature maps to coordinates
CNN 输出的是特征图,控制器要的却是数字:物体在图像的哪个位置、夹爪该往哪边动。最常见的接口有两种:全局平均池化(得到一个向量,丢掉了位置)和空间 softmax(spatial soft-argmax,Levine 等 2016):对每个通道的特征图做 softmax,得到一个“注意力”分布 \(p_{ij}\),再求期望坐标 \(\hat{\mathbf u}=\sum p_{ij}(j,i)\)。每个通道于是变成一个可微的二维“关键点”,正好适合喂给控制策略。温度 \(T\) 决定分布有多尖:太高,所有像素差不多一样重,关键点被拉向图像中心;太低,几乎就是 argmax,只能落在整数像素上,梯度也几乎没有用处了。
A CNN outputs feature maps, but a controller wants numbers: where the object is in the image, which way the gripper should move. Two interfaces are most common: global average pooling (a vector, with the position thrown away) and the spatial softmax (spatial soft-argmax, Levine et al. 2016): take a softmax over each channel's feature map to get an "attention" distribution \(p_{ij}\), then the expected coordinate \(\hat{\mathbf u}=\sum p_{ij}(j,i)\). Each channel becomes a differentiable 2-D "keypoint", ideal input for a control policy. The temperature \(T\) sets how peaked the distribution is: too high and all pixels weigh about the same, pulling the keypoint towards the image centre; too low and it is almost an argmax, stuck on whole pixels and no longer usefully differentiable.
- 不开干扰物,让物体离图像中心 (7.5, 7.5) 至少 4 个像素(默认位置 (11.3, 4.6) 就满足),调温度使关键点误差小于 0.45 像素。\(T=1\) 时误差约 4.6 像素(被拉向中心),\(T=0.1\) 时约 0.3 像素,\(T\le0.02\) 时又回到约 0.5 像素(贴到最近的整数像素上)。With the distractor off and the object at least 4 px from the image centre (7.5, 7.5) (the default position (11.3, 4.6) qualifies), tune the temperature until the keypoint error is below 0.45 px. At \(T=1\) the error is about 4.6 px (pulled towards the centre), at \(T=0.1\) about 0.3 px, and at \(T\le0.02\) back to about 0.5 px (stuck to the nearest whole pixel).
再打开干扰物:温度高时关键点落在两个物体中间,那里什么也没有(\(T=0.3\) 时约在 (7.3, 7.9));温度低时它跳到稍高的那个峰上,这里恰好是干扰物。这和 22.1 节将要讲的“均方误差把多峰的示范平均掉”是同一类问题:对一个多峰分布取期望,结果可能落在任何一个峰之外。
Now switch the distractor on: at high temperature the keypoint lands between the two objects, where there is nothing (about (7.3, 7.9) at \(T=0.3\)); at low temperature it jumps to whichever bump is a hair higher, here the distractor. This is the same kind of problem as the "MSE averages multimodal demonstrations" of Lesson 22.1: the expectation of a multimodal distribution can land outside every mode.
MLP 无处不在:在仿真里用强化学习训练的腿足运动策略,通常是在本体感觉(关节角、角速度、上一步动作、机身姿态)上的几层 MLP;Hwangbo 等(2019,Science Robotics)为 ANYmal 训练策略时,还用一个 MLP“执行器网络”根据位置误差和速度的历史预测串联弹性驱动器的实际力矩,补上仿真里最难建模的一环。价值函数、Q 函数和学习到的动力学模型(20 章、24.1 节)也大多是 MLP。 CNN 做视觉编码器:Levine 等(2016,JMLR)端到端训练了从相机像素到 PR2 关节力矩的视觉运动策略,卷积层后面接的正是 FIG 7 的空间 softmax;Dex-Net 2.0(Mahler 等,RSS 2017)用一个 CNN(GQ-CNN)在深度图上给候选抓取打分;QT-Opt(Kalashnikov 等,CoRL 2018)用 CNN 表示以相机图像为输入的 Q 函数,在真实机械臂上学会抓取。 ResNet 作为骨干:ACT(Zhao 等 2023)用 ResNet-18 编码多路相机图像;Diffusion Policy(Chi 等 2023)也用 ResNet-18,并把全局平均池化换成空间 softmax、把 BatchNorm 换成 GroupNorm;R3M(Nair 等 2022)在人类第一视角视频上预训练 ResNet 编码器给机器人用(18.3 节)。ResNet-18 约有 1170 万个参数,224×224 的输入最后变成 7×7×512 的特征图,再池化成 512 维向量。 CNN 之后:越来越多的机器人基础模型改用 Vision Transformer 作为视觉编码器(17.3 节),但卷积的思想(局部性、权重共享、多尺度)仍然以各种形式留在里面。
MLPs are everywhere: legged-locomotion policies trained by RL in simulation are typically a few MLP layers on proprioception (joint angles, joint velocities, the previous action, body orientation); when Hwangbo et al. (2019, Science Robotics) trained ANYmal's policy, they also used an MLP "actuator network" that predicts the real torque of its series-elastic actuators from histories of position error and velocity, filling in the part of the simulation hardest to model. Value functions, Q-functions and learned dynamics models (Chapter 20, Lesson 24.1) are mostly MLPs as well. CNNs as visual encoders: Levine et al. (2016, JMLR) trained visuomotor policies end to end from camera pixels to PR2 joint torques, with exactly the spatial softmax of FIG 7 after the convolutional layers; Dex-Net 2.0 (Mahler et al., RSS 2017) scores candidate grasps on depth images with a CNN (the GQ-CNN); QT-Opt (Kalashnikov et al., CoRL 2018) represents a Q-function on camera images with a CNN and learns grasping on real arms. ResNets as backbones: ACT (Zhao et al. 2023) encodes its several camera images with ResNet-18; Diffusion Policy (Chi et al. 2023) also uses ResNet-18, replacing global average pooling with a spatial softmax and BatchNorm with GroupNorm; R3M (Nair et al. 2022) pre-trains ResNet encoders on egocentric human video for robots to use (Lesson 18.3). A ResNet-18 has about 11.7 million parameters and turns a 224×224 input into a 7×7×512 feature map, pooled into a 512-dimensional vector. After CNNs: more and more robot foundation models use a Vision Transformer as visual encoder (Lesson 17.3), but the ideas of convolution (locality, weight sharing, multiple scales) live on inside them in various forms.
9常见误区Common pitfalls
FIG 2 的线性模型训练多久都停在 81.8%:没有激活函数,叠多少层都一样。反过来,容量够了以后再加宽加深,收益很快递减(FIG 2 里 3×32 的 2241 个参数并不明显好过 2×16 的 337 个),却让每次查询更慢;在控制回路里跑的网络常常有毫秒级的时间预算,先从小网络开始。
The linear model of FIG 2 stalls at 81.8% however long it trains: without an activation, any number of layers is the same. Conversely, once capacity suffices, wider and deeper gives quickly diminishing returns (in FIG 2 the 2241 parameters of 3×32 are not clearly better than the 337 of 2×16) while making every query slower; a network inside a control loop often has a budget of milliseconds, so start small.
关节角以 rad 计、力以 N 计、像素是 0 到 255,直接拼在一起,数值大的分量会主导第一层,初始化(16.3 节)的假设也不再成立:先把每个分量标准化到零均值、单位方差(或像 FIG 2 那样除以 \(\pi\))。周期量要编码成 \((\cos,\sin)\)(FIG 2 的接缝),旋转不要直接喂欧拉角(5.2 节的奇异与跳变),用旋转矩阵的两列或四元数并注意 \(\pm\mathbf q\) 的二义性。
Joint angles in rad, forces in N and pixels from 0 to 255 concatenated as they are let the large components dominate the first layer and break the assumptions of the initialization (Lesson 16.3): standardize each component to zero mean and unit variance first (or divide by \(\pi\) as FIG 2 does). Periodic quantities should be encoded as \((\cos,\sin)\) (the seam of FIG 2), and rotations should not be fed as Euler angles (the singularities and jumps of Lesson 5.2): use two columns of the rotation matrix, or a quaternion with care for the \(\pm\mathbf q\) ambiguity.
定理只保证好的权重存在。它不保证单元数合理(维数灾难),不保证梯度下降找得到,更不保证在没见过的输入上正确:FIG 6 的 MLP 训练准确率 100%,测试只有三分之一。泛化靠的是数据覆盖和合适的归纳偏置,不是表达能力。
The theorem only promises that good weights exist. It does not promise a reasonable number of units (the curse of dimensionality), that gradient descent finds them, and least of all correctness on unseen inputs: the MLP of FIG 6 has 100% training accuracy and a third on the test set. Generalization comes from data coverage and the right inductive bias, not from expressive power.
严格的等变只对步长 1、远离边界的卷积成立。步长和池化引入混叠(FIG 4),边界的填充让靠边的物体与中间的不同;在机器人上,这意味着物体移动一个像素,抓取打分可能跳一下。另外,只有全局池化之后才谈得上“不变”,而很多机器人任务恰恰需要位置信息(夹爪该去哪里):这时应保留空间结构(空间 softmax、FIG 7),而不是池化掉。
Exact equivariance holds only for stride-1 convolutions away from the borders. Strides and pooling introduce aliasing (FIG 4), and padding makes an object at the edge differ from one in the middle; on a robot this means a one-pixel shift of the object can make a grasp score jump. Moreover, "invariance" only arrives after global pooling, and many robot tasks need the position (where should the gripper go?): then keep the spatial structure (spatial softmax, FIG 7) instead of pooling it away.
卷积只把平移对称写进了结构。同一个物体转 90° 或者离相机近一倍,在 CNN 看来是新图案,只能靠数据增强(16.2 节)让它见过,或者用群等变卷积(Cohen 与 Welling 2016)把旋转对称也写进结构;俯视抓取这类“平面旋转无关”的任务,后者能显著省数据。同理,FIG 6 的“歪着”类同时包含两条对角线,CNN 要用两个不同的卷积核分别去学。
Convolution builds only translation symmetry into the structure. The same object rotated by 90° or twice as close to the camera is a new pattern to a CNN; it can only be covered by data augmentation (Lesson 16.2) or by group-equivariant convolutions (Cohen & Welling 2016) that build rotation symmetry into the structure as well; for tasks that do not care about in-plane rotation, such as top-down grasping, the latter can save a lot of data. Likewise, the "tilted" class of FIG 6 contains both diagonals, and the CNN has to learn them with two different kernels.
10小练习Check yourself
11延伸阅读Further reading
- I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, MIT Press, 2016, Ch. 6 and 9 — 前馈网络与卷积网络的标准教材the standard textbook chapters on feedforward and convolutional networks
- G. Cybenko, Approximation by Superpositions of a Sigmoidal Function, Mathematics of Control, Signals and Systems, 1989; K. Hornik, M. Stinchcombe & H. White, Multilayer Feedforward Networks are Universal Approximators, Neural Networks, 1989 — 万能逼近定理the universal approximation theorem
- M. Telgarsky, Benefits of Depth in Neural Networks, COLT 2016 — 复合帐篷函数与深度的指数优势composed tent maps and the exponential advantage of depth
- Y. LeCun, L. Bottou, Y. Bengio & P. Haffner, Gradient-Based Learning Applied to Document Recognition, Proceedings of the IEEE, 1998 — LeNet-5:卷积、池化与权重共享LeNet-5: convolution, pooling and weight sharing
- A. Krizhevsky, I. Sutskever & G. Hinton, ImageNet Classification with Deep Convolutional Neural Networks, NeurIPS 2012 — AlexNetAlexNet
- K. He, X. Zhang, S. Ren & J. Sun, Deep Residual Learning for Image Recognition, CVPR 2016 — ResNet 与残差连接ResNet and residual connections
- V. Dumoulin & F. Visin, A Guide to Convolution Arithmetic for Deep Learning, arXiv 2016 — 步长、填充、空洞与转置卷积的尺寸算术size arithmetic for strides, padding, dilation and transposed convolutions
- R. Zhang, Making Convolutional Networks Shift-Invariant Again, ICML 2019 — 降采样的混叠与抗混叠aliasing in downsampling and how to fix it
- M. Tancik et al., Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains, NeurIPS 2020 — 坐标 MLP 的傅里叶特征Fourier features for coordinate MLPs
- S. Levine, C. Finn, T. Darrell & P. Abbeel, End-to-End Training of Deep Visuomotor Policies, JMLR 2016 — 从像素到力矩的 CNN 策略与空间 softmaxCNN policies from pixels to torques, and the spatial softmax