赞
踩
LSTM 模型如 图1 所示。横向穿过 cell 上部的线分别称作 c \mathbf{c} c 总线,下部的线称为 h \mathbf{h} h 总线,这意味着 c t − 1 \mathbf{c}_{t - 1} ct−1 与 h t − 1 \mathbf{h}_{t - 1} ht−1 会对 t t t 时刻的计算产生影响 。其中:
图1. LSTM 模型
LSTM 从名称来看,是用于处理长短时序。
程序代码见: https://github.com/garstka/char-rnn-java
为了学习它, 我又来逐个方法来分析.
// 前向传播核心代码 // acts 根据字符串存取实型二维数组 public void active(int t, Map<String, DoubleMatrix> acts) { // 获取 t 时刻输入 DoubleMatrix x = acts.get("x" + t); // 上一时刻的 h 和 c DoubleMatrix preH = null, preC = null; if (t == 0) { preH = new DoubleMatrix(1, getOutSize()); preC = preH.dup(); } else { preH = acts.get("h" + (t - 1)); preC = acts.get("c" + (t - 1)); } DoubleMatrix i = Activer.logistic(x.mmul(Wxi).add(preH.mmul(Whi)).add(preC.mmul(Wci)).add(bi)); DoubleMatrix f = Activer.logistic(x.mmul(Wxf).add(preH.mmul(Whf)).add(preC.mmul(Wcf)).add(bf)); DoubleMatrix gc = Activer.tanh(x.mmul(Wxc).add(preH.mmul(Whc)).add(bc)); DoubleMatrix c = f.mul(preC).add(i.mul(gc)); DoubleMatrix o = Activer.logistic(x.mmul(Wxo).add(preH.mmul(Who)).add(c.mmul(Wco)).add(bo)); DoubleMatrix gh = Activer.tanh(c); DoubleMatrix h = o.mul(gh); // 存储各个二维矩阵 acts.put("i" + t, i); acts.put("f" + t, f); acts.put("gc" + t, gc); acts.put("c" + t, c); acts.put("o" + t, o); acts.put("gh" + t, gh); acts.put("h" + t, h); }
在我运行的程序中, x t x_t xt 为 one-hot 编码的 1 × 62 1 \times 62 1×62 向量, i t i_t it 至 h t h_t ht 均为 1 × 100 1 \times 100 1×100 向量.
代码所表示的信息比 图1 更丰富。矩阵变量之间要运算,很多时候要乘以权重矩阵。为使得结构更清晰,图1 牺牲了表达的准确性。以下将向量的计算翻译成数学表达式,这些向量都会被存储在模型中。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。