Task: Implement Long Short-Term Memory (LSTM) Network
Implement forward(self, x, initial_hidden_state, initial_cell_state) of the class LSTM: a single-layer LSTM run over a sequence.
Shapes (input size D, hidden size H, sequence length T):
x is (T,D); step t uses xt as a column vector (D,1). Initial states are (H,1).
- Weights
Wf, Wi, Wc, Wo are (H,D+H), biases bf, bi, bc, bo are (H,1). The tests overwrite these attributes after construction, so forward must read them from self.
- Gate input: the stacked column zt=[ht−1;xt] of shape (D+H,1), hidden state first, then the input.
Per step, with σ the logistic sigmoid and ⊙ element-wise product:
- ft=σ(Wfzt+bf), it=σ(Wizt+bi), c~t=tanh(Wczt+bc), ot=σ(Wozt+bo)
- ct=ft⊙ct−1+it⊙c~t, ht=ot⊙tanh(ct)
Return the tuple (outputs, h_T, c_T): outputs is a NumPy array of shape (T,H,1) holding every ht; h_T, c_T are the final hidden and cell states, shape (H,1), unrounded.