线性层把输入特征做多组可学习的加权求和,再为每个输出加入偏置。单个列向量样本常写成 y = Wx + b;当 batch 中每个样本按行存放时,同一计算会写成 Y = XWᵀ + b。两种写法的计算含义相同,只是数据排列约定不同。

嘿嘿,久等各位,今天就正式进入大模型的知识领域了。对我来说这也是陌生的、等待探索的世界。欢迎大家随时提出质疑和发起讨论。现在就让我们继续开始漫游吧。

神经网络里最常见的层,可能也是最容易因公式而感到恐惧的一层:Linear、Dense 或全连接层。

其实它的核心并不复杂。每一个输出,都是所有输入特征的加权和,再加一个偏置;想得到多个输出,就准备多组权重和多个偏置。矩阵 W 只是把这些权重整齐地组织起来。

真正需要仔细理解的是 shape:为什么 W 的行数等于输出特征数,列数等于输入特征数?为什么单样本公式是 Wx + b,代码处理 batch 时却经常写成 XWᵀ + b?偏置又怎样自动加到每一个样本、每一个 Token 上?

这篇文章会从一个输出开始,逐步走到 batch、序列轴、参数量和反向传播,把线性层的前向与反向完整连起来。

An image to describe post

一、一个输出就是一次加权求和

An image to describe post

Day 21–23 已经学习过:

  • 向量可以用来组织一个对象的多个特征;
  • 矩阵乘法可以对多个特征进行加权组合;
  • XW 可以同时处理一批样本。

Day 33–36 又学习了:

  • loss 对每个参数的偏导数组成参数梯度;
  • 链式法则将局部导数沿计算图向后传递;
  • 自动微分可以对局部运算连续执行 VJP。

今天将这两组知识连起来:

线性层就是将输入特征做多组可学习的加权求和,再为每个输出加上一个可学习的偏置。

先从三个输入特征开始

假设一个样本拥有三个输入特征:

An image to describe post

我们希望将它们组合成一个输出 y:

An image to describe post

其中:

  • x₁, x₂, x₃ 是输入数值;
  • w₁, w₂, w₃ 是每个输入对当前输出的权重;
  • b 是偏置;
  • y 是当前线性层的输出。

权重决定每个输入对结果的影响方向和强度:

  • w_j > 0 时,x_j 变大会推动 y 变大;
  • w_j < 0 时,x_j 变大会推动 y 变小;
  • |w_j| 越大,当前输入对 y 的影响越强;
  • w_j = 0 时,当前输入不会直接影响 y。

偏置 b 则决定即使输入全部为 0,输出的基准值是什么:

An image to describe post

将加权求和写成向量点积

将三个输入写成列向量:

An image to describe post

将三个权重写成行向量:

An image to describe post

那么:

An image to describe post

所以单个输出本质上就是:

An image to describe post

二、多个输出意味着多组权重

An image to describe post

神经网络中的一层通常不会只生成一个数,而是生成一组新特征。

例如,三个输入特征要变成两个输出特征:

An image to describe post

每个输出都有自己的一组权重和一个偏置。

将两组权重按行排列:

An image to describe post

偏置写成:

An image to describe post

输出写成:

An image to describe post

于是两个公式可以一次写成:

An image to describe post

三、单个样本时的 shape 怎样对应

An image to describe post

定义:

  • d_in 表示输入特征数;
  • d_out 表示输出特征数。

对一个使用列向量表示的样本:

对象 shape 含义
x d_in × 1 一个样本的输入特征
W d_out × d_in 每一行负责生成一个输出特征
b d_out × 1 每个输出特征的偏置
y d_out × 1 一个样本的输出特征

矩阵乘法的 shape 为:

An image to describe post

中间的 d_in 相同,因此可以相乘;结果保留外侧的 d_out × 1。

这给出了一个非常重要的 shape 规则:

An image to describe post

线性层将最后一个输入特征维度从 d_in 变换为 d_out。

四、用完整数值例子计算一次线性层

An image to describe post

令:

An image to describe post

当前层有:

An image to describe post

先计算第一个输出:

An image to describe post

再计算第二个输出:

An image to describe post

所以:

An image to describe post

矩阵写法没有改变计算内容,只是将两组加权求和合并成一次矩阵乘法。

W 的每一行代表什么

对:

An image to describe post

第 i 行是:

An image to describe post

它包含生成第 i 个输出所需的所有权重:

An image to describe post

因此:

W 的行数决定输出特征数,列数必须等于输入特征数。

每个输出特征也常被称为一个输出单元或神经元。在线性层中,一个输出单元就是一组加权求和与一个偏置。

五、从全连接结构走到 Batch 写法

An image to describe post

线性层也常被称为:

  • 全连接层(fully connected layer);
  • 稠密层(dense layer)。

原因是对每个输出 y_i,公式都允许它使用所有输入 x_j:

An image to describe post

“全连接”表示结构上允许每个输出使用每个输入。训练后某些权重可以很小或等于 0,但连接位置本身是允许存在的。

为什么 Batch 代码常写成 XWᵀ + b

前面对单个样本使用列向量:

An image to describe post

所以写成:

An image to describe post

但在数据表和深度学习代码中,一批样本常按行排列。

假设 batch size 为 B,则:

An image to describe post

每一行是一个样本:

An image to describe post

为了让每一行都与 W 的一行做点积,批量写法是:

An image to describe post

shape 为:

An image to describe post

所以:

对象 批量 shape
X B × d_in
W d_out × d_in
Wᵀ d_in × d_out
b d_out
Y B × d_out

这与单样本的 y = Wx + b 并不矛盾。差别只是:

  • 单样本推导将样本写成列向量;
  • 批量代码将每个样本写成矩阵的一行。

两种写法对每个样本执行的加权求和完全相同。

六、Batch、序列轴与偏置广播

An image to describe post

批量乘法得到:

An image to describe post

而偏置只有:

An image to describe post

代码使用 broadcasting,将同一个 b 加到每个样本的输出上:

An image to describe post

其中:

  • r 表示第 r 个样本;
  • i 表示第 i 个输出特征。

如果 B = 3、d_out = 2,那么:

An image to describe post

在加法中等效于:

An image to describe post

它不是为每个样本学一个新偏置,而是所有样本共享同一组 d_out 个偏置。

输入不只有 batch 轴时会怎样

在 Transformer 等模型中,输入可能是:

An image to describe post

其中:

  • B 是 batch size;
  • T 是序列长度;
  • d_in 是每个位置的输入特征数。

线性层通常只变换最后一个特征轴:

An image to describe post

B 和 T 保持不变,因为同一个线性变换被分别应用到每个样本的每个序列位置。

更一般地,如果输入 shape 是:

An image to describe post

那么线性层输出 shape 是:

An image to describe post

前面的轴作为样本或位置索引保留,最后的特征维度被 W 变换。

七、参数量与仿射变换

An image to describe post

权重矩阵包含:

An image to describe post

个参数。

偏置向量包含:

An image to describe post

个参数。

因此总参数量是:

An image to describe post

例如 d_in = 3、d_out = 2:

An image to describe post

其中有 6 个权重和 2 个偏置。

严格来说,Wx + b 是线性变换吗

在严格线性代数中,线性函数 f 需要满足:

An image to describe post

对:

An image to describe post

有:

An image to describe post

当 b ≠ 0 时,它不是严格线性变换,而是仿射变换(affine transformation)。

但深度学习框架和课程习惯上仍然将 Wx + b 称为线性层。因此需要区分:

  • 严格数学中,Wx 是线性变换;
  • 严格数学中,Wx + b 是仿射变换;
  • 深度学习语境中,Linear 层通常包含偏置。

八、为什么不能只叠加线性层

An image to describe post

考虑两层:

An image to describe post

An image to describe post

将第一层代入第二层:

An image to describe post

定义:

An image to describe post

就得到:

An image to describe post

所以:

多个线性层之间如果不加其他非线性运算,无论叠多少层,整体仍然可以合并成一个 W′x + b′。

这就引出后续的问题:

为什么神经网络需要在多个线性层之间加入 ReLU、Sigmoid、Tanh 或 GELU 等非线性激活函数?

九、单样本线性层怎样反向传播

An image to describe post

假设单个样本的前向计算是:

An image to describe post

并且反向传播已经从后面的计算得到:

An image to describe post

那么线性层需要计算三类梯度:

An image to describe post

An image to describe post

An image to describe post

shape 分别是:

An image to describe post

An image to describe post

以及:

An image to describe post

这与 Day 35 学习的 VJP 一致:线性层的 Jacobian 对输入而言就是 W,因此输入梯度是 W^T g_y。

权重梯度和偏置梯度的逐元素推导见【附录:线性层的单样本梯度】。

十、Batch 中的梯度怎样累加

An image to describe post

批量前向计算是:

An image to describe post

假设:

An image to describe post

那么:

An image to describe post

An image to describe post

An image to describe post

这只是单样本梯度公式在 batch 矩阵上的写法。GᵀX 的矩阵乘法已经包含对 batch 维度 B 的累加,不需要再额外执行一次求和。偏置没有与输入相乘,因此对 G 的 batch 维度直接求和。

十一、梯度怎样变成参数更新

An image to describe post

反向传播计算出:

An image to describe post

它们表示 loss 对参数的局部变化率,不是 W 和 b 已经发生的改变量。

最简单的梯度下降更新才是:

An image to describe post

An image to describe post

然后:

An image to describe post

An image to describe post

学习率 η 与优化器决定如何将梯度转换成实际参数更新。

常见误解

  • W 的 shape 应该是 d_in × d_out 吗? 取决于约定。本文单样本写成 y = Wx + b,所以 W 是 d_out × d_in。批量行向量写法使用 XWᵀ。
  • 转置只是为了让 shape 勉强对上吗? 不是。转置来自“单样本是列向量”与“batch 中每个样本是一行”这两种数据排列方式的差别。
  • Wx 是逐元素乘法吗? 不是。它是矩阵乘向量,每个输出是一行权重与输入的点积。
  • 每个样本都有一个自己的 b 吗? 不是。同一线性层的所有样本共享同一个 b,只是 broadcasting 将它加到每行。
  • 输出数量由 batch size 决定吗? 不是。batch size 决定同时处理多少个样本,d_out 才决定每个样本有多少个输出特征。
  • 偏置也要与所有输入相乘吗? 不是。每个输出只直接加上一个偏置 b_i。
  • 叠很多线性层就会自动得到复杂非线性函数吗? 不会。不加激活函数时,多层仍可合并为一层仿射变换。
  • 参数梯度就是参数的实际更新量吗? 不是。优化器还要根据学习率和更新规则将梯度转换为参数改变量。

结语:线性层是可学习的特征重组器

  1. 单样本列向量写法为:

An image to describe post

  1. shape 对应为:

An image to describe post

  1. batch 按行排列时:

An image to describe post

  1. 偏置 b 的长度是 d_out,并通过 broadcasting 加到每个样本。

  2. 线性层只改变最后的特征维度:

An image to describe post

  1. 参数总数是:

An image to describe post

  1. 严格数学上 Wx + b 是仿射变换,但深度学习中习惯称为线性层。

  2. 不加非线性激活函数时,多层线性层仍可以合并为一层。

  3. 单样本反向传播为:

An image to describe post

自检

  1. 对一个输入特征数为 4、输出特征数为 3 的线性层,W、b、x、y 的 shape 分别是什么?
  2. 为什么 W 的每一行对应一个输出特征?
  3. 为什么单样本可以写成 Wx + b,批量又常写成 XWᵀ + b?
  4. 对 X ∈ R^(32 × 128) 和 W ∈ R^(64 × 128),XWᵀ 的 shape 是什么?
  5. 为什么 b ∈ R^64 可以加到 shape 为 32 × 64 的矩阵上?
  6. 线性层从 128 个输入特征变成 256 个输出特征时,包含多少权重和偏置?
  7. 对输入 shape [B, T, 128],如果线性层将最后一维从 128 变换为 256,输出 shape 是什么?
  8. 为什么 Wx + b 在严格数学上不一定是线性变换?
  9. 为什么两个线性层之间不加激活函数时,可以合并成一层?
  10. 已知 g_y ∈ R^(d_out),为什么 W^T g_y 与输入 x 同 shape?
  11. 为什么单样本权重梯度是外积 g_y x^T?
  12. 已知 X ∈ R^(B × d_in)、G ∈ R^(B × d_out),GᵀX 和 GW 的 shape 分别是什么?

附录

线性层的单样本梯度为什么是这些形状

对第 i 个输出:

An image to describe post

记:

An image to describe post

输入梯度

输入 x_j 会影响所有输出 y_i,而:

An image to describe post

因此根据多路径梯度相加:

An image to describe post

将所有 j 组织成向量:

An image to describe post

权重梯度

W_ij 只直接影响 y_i,并且:

An image to describe post

因此:

An image to describe post

矩阵 g_y x^T 的第 (i, j) 个元素恰好是 g_i x_j,所以:

An image to describe post

偏置梯度

因为:

An image to describe post

所以:

An image to describe post

将所有分量组织起来:

An image to describe post