LSTM (Long Short-Term Memory) Animation

Click "Play" or "Step" to start.
seq_len = 4, input_dim = 3, hidden_dim = 4, concat_dim = hidden + input = 7
Parameters (Weight Matrices & Biases) ▼
W_f (hidden × concat)
b_f
W_i (hidden × concat)
b_i
W_c (hidden × concat)
b_c
W_o (hidden × concat)
b_o
Input x_t
Previous h_{t-1}
Previous c_{t-1}
LSTM Cell
Forget Gate (f_t)
σ(W_f·[h,x] + b_f)
Input Gate (i_t)
σ(W_i·[h,x] + b_i)
Cell Candidate (c̃_t)
tanh(W_c·[h,x] + b_c)
Output Gate (o_t)
σ(W_o·[h,x] + b_o)
Cell State (c_t)
f_t ⊙ c_{t-1} + i_t ⊙ c̃_t
Hidden State (h_t)
o_t ⊙ tanh(c_t)
Output h_t
LSTM processes sequences step by step, maintaining cell state and hidden state.

LSTM (Long Short-Term Memory) とは

LSTMは、長期的な依存関係を学習できるRNN(再帰型ニューラルネットワーク)の一種です。 通常のRNNが抱える勾配消失問題を、ゲート機構とセル状態によって解決しています。

4つのゲート

Forget Gate (忘却ゲート)

過去の情報をどれだけ「忘れる」かを決定。0〜1の値で、0なら完全に忘れ、1なら完全に保持。

Input Gate (入力ゲート)

新しい情報をどれだけ「取り込む」かを決定。セル候補と掛け合わせて使用。

Cell Candidate (セル候補)

セル状態に追加する可能性のある新しい情報。tanh で -1〜1 の範囲。

Output Gate (出力ゲート)

セル状態のどの部分を出力(隠れ状態)として使うかを決定。

数式

f_t = σ(W_f · [h_{t-1}, x_t] + b_f)   ← 忘却ゲート
i_t = σ(W_i · [h_{t-1}, x_t] + b_i)   ← 入力ゲート
c̃_t = tanh(W_c · [h_{t-1}, x_t] + b_c)   ← セル候補
c_t = f_t ⊙ c_{t-1} + i_t ⊙ c̃_t   ← セル状態の更新
o_t = σ(W_o · [h_{t-1}, x_t] + b_o)   ← 出力ゲート
h_t = o_t ⊙ tanh(c_t)   ← 隠れ状態

なぜLSTMが有効か