如是我闻:

语言模型的目标是估计:
p(wt∣w1,w2,…,wt−1) p(w_t \mid w_1, w_2, \dots, w_{t-1}) p(wtw1,w2,,wt1)
RNN 是一种可以逐步读取输入词序列并更新“记忆”的神经网络模型。今天我们就手动算一个最小化 RNN 的例子,从输入词向量 → 计算隐藏状态 → softmax 预测。


✅ 设定:极简词表

我们用一个小词表:

单词 编号
“I” 0
“like” 1
“cats” 2

✅ 模型参数设计

📦 词嵌入矩阵 C∈R2×3C \in \mathbb{R}^{2 \times 3}CR2×3

每个词是一个 2 维向量,矩阵为:
C=[101011] C = \begin{bmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix} C=[100111]

表示:

  • “I” → [1,0]T[1, 0]^T[1,0]T
  • “like” → [0,1]T[0, 1]^T[0,1]T
  • “cats” → [1,1]T[1, 1]^T[1,1]T

🧠 RNN 权重参数:

  • W=[1101]W = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}W=[1011](词向量部分)
  • U=[1011]U = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}U=[1101](隐藏状态部分)
  • b=[00]b = \begin{bmatrix} 0 \\ 0 \end{bmatrix}b=[00](偏置项)
  • 初始状态:h0=[00]h_0 = \begin{bmatrix} 0 \\ 0 \end{bmatrix}h0=[00]
  • 激活函数:简化为 tanh⁡(x)≈x\tanh(x) \approx xtanh(x)x

🔚 输出层参数:

  • V=[100122]∈R3×2V = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 2 & 2 \end{bmatrix} \in \mathbb{R}^{3 \times 2}V= 102012 R3×2

输出层目标是将隐藏状态 ( h ) 投影为 logits,表示下一个词的得分。


🧮 输入句子:“I like”

我们计算这个序列经过 RNN 的处理,最终预测第三个词的概率分布。


⏳ Step 1: 输入 “I”

查词向量:
x1=C(w1)=C(0)=[10] x_1 = C(w_1) = C(0) = \begin{bmatrix} 1 \\ 0 \end{bmatrix} x1=C(w1)=C(0)=[10]

隐藏状态计算:
h1=tanh⁡(b+Uh0+Wx1)≈b+Uh0+Wx1=0+0+[10]=[10] h_1 = \tanh(b + U h_0 + W x_1) \approx b + U h_0 + W x_1 = 0 + 0 + \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 1 \\ 0 \end{bmatrix} h1=tanh(b+Uh0+Wx1)b+Uh0+Wx1=0+0+[10]=[10]


⏳ Step 2: 输入 “like”

查词向量:
x2=C(1)=[01] x_2 = C(1) = \begin{bmatrix} 0 \\ 1 \end{bmatrix} x2=C(1)=[01]

隐藏状态计算:
Uh1=[1011]⋅[10]=[11] U h_1 = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} Uh1=[1101][10]=[11]

Wx2=[1101]⋅[01]=[11] W x_2 = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} Wx2=[1011][01]=[11]

h2=tanh⁡(b+Uh1+Wx2)≈[22] h_2 = \tanh(b + U h_1 + W x_2) \approx \begin{bmatrix} 2 \\ 2 \end{bmatrix} h2=tanh(b+Uh1+Wx2)[22]


🎯 输出预测:

V=[100122],h2=[22] V = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 2 & 2 \end{bmatrix}, \quad h_2 = \begin{bmatrix} 2 \\ 2 \end{bmatrix} V= 102012 ,h2=[22]

Vh2=[1⋅2+0⋅20⋅2+1⋅22⋅2+2⋅2] V h_2 = \begin{bmatrix} 1 \cdot 2 + 0 \cdot 2 \\ 0 \cdot 2 + 1 \cdot 2 \\ 2 \cdot 2 + 2 \cdot 2 \end{bmatrix} Vh2= 12+0202+1222+22
Vh2=[228] V h_2 = \begin{bmatrix} 2 \\ 2 \\ 8 \end{bmatrix} Vh2= 228

softmax(Vh2)=softmax([2,2,8])=[e2e2+e2+e8,e2e2+e2+e8,e8e2+e2+e8]≈[0.0025, 0.0025, 0.995] \text{softmax}(V h_2) = \text{softmax}([2, 2, 8]) = \left[ \frac{e^2}{e^2 + e^2 + e^8}, \frac{e^2}{e^2 + e^2 + e^8}, \frac{e^8}{e^2 + e^2 + e^8} \right] \approx [0.0025,\ 0.0025,\ 0.995] softmax(Vh2)=softmax([2,2,8])=[e2+e2+e8e2,e2+e2+e8e2,e2+e2+e8e8][0.0025, 0.0025, 0.995]

softmax:
softmax([2,2,8])≈[e2Z,e2Z,e8Z] \text{softmax}([2, 2, 8]) \approx \left[ \frac{e^2}{Z}, \frac{e^2}{Z}, \frac{e^8}{Z} \right] softmax([2,2,8])[Ze2,Ze2,Ze8]

近似计算:

  • e2≈7.39e^2 \approx 7.39e27.39
  • e8≈2980e^8 \approx 2980e82980
  • Z≈7.39+7.39+2980=2994.78Z \approx 7.39 + 7.39 + 2980 = 2994.78Z7.39+7.39+2980=2994.78

所以概率分布为:
p≈[0.0025,0.0025,0.995] p \approx [0.0025, 0.0025, 0.995] p[0.0025,0.0025,0.995]


✅ 最终结果

模型预测下一个词是:“cats”,概率高达 99.5%。结果非常amazing啊

以上

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐