#59 · Deep Learning · Medium
⊣ Solve on deep-ml.comImplement a Long Short-Term Memory (LSTM) network from scratch. Given input data, initial hidden and cell states, and weight/bias parameters for the forget, input, candidate, and output gates, compute the forward pass of a single LSTM cell for each time step and return the final hidden state.
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def lstm_forward(input_seq, initial_hidden, initial_cell,
Wf, Uf, bf,
Wi, Ui, bi,
Wc, Uc, bc,
Wo, Uo, bo):
h = initial_hidden
c = initial_cell
for x in input_seq:
f = sigmoid(np.dot(Wf, x) + np.dot(Uf, h) + bf)
i = sigmoid(np.dot(Wi, x) + np.dot(Ui, h) + bi)
c_hat = np.tanh(np.dot(Wc, x) + np.dot(Uc, h) + bc)
o = sigmoid(np.dot(Wo, x) + np.dot(Uo, h) + bo)
c = f * c + i * c_hat
h = o * np.tanh(c)
return hh and cell state c from provided initial values.f: decides what to discard from the cell state.i: decides which new values to store.c_hat: new candidate values via tanh.o: decides what part of the cell state to output.c = f * c + i * c_hat.h = o * tanh(c).