Problems
Loading...
Step 5 of 6
LSTM Cell
Medium

Implement one complete LSTM cell. Compute the forget gate, input gate, candidate state, updated cell state, output gate, and hidden state.

ft=σ(Wf[ht−1,xt]+bf).f_t=\sigma\left(W_f[h_{t-1},x_t]+b_f\right). it=σ(Wi[ht−1,xt]+bi).i_t=\sigma\left(W_i[h_{t-1},x_t]+b_i\right). C~t=tanh⁡(Wc[ht−1,xt]+bc).\widetilde{C}_t=\tanh\left(W_c[h_{t-1},x_t]+b_c\right). Ct=ft⊙Ct−1+it⊙C~t.C_t=f_t\odot C_{t-1}+i_t\odot\widetilde{C}_t. ot=σ(Wo[ht−1,xt]+bo).o_t=\sigma\left(W_o[h_{t-1},x_t]+b_o\right). ht=ot⊙tanh⁡(Ct).h_t=o_t\odot\tanh(C_t).

H is the hidden width and D is the input width. Return a dictionary containing hidden_state and cell_state as float64 NumPy arrays.

Loading visualization...

Examples

Input: x_t = [1,0.5], h_prev = [0,0], C_prev = [0,0], W_f = [[0.1,0.2,0.3,0.4],[0.5,0.1,0.2,0.3]], W_i = [[0.2,0.3,0.1,0.4],[0.4,0.2,0.3,0.1]], W_c = [[0.3,0.1,0.2,0.1],[0.1,0.4,0.1,0.3]], W_o = [[0.1,0.3,0.4,0.2],[0.3,0.1,0.2,0.4]], b_f = [0,0], b_i = [0,0], b_c = [0,0], b_o = [0,0]

Output: {"hidden_state":[0.087002,0.085429],"cell_state":[0.140692,0.143674]}

Explanation: The gates control retained memory, new memory, and the part of the updated cell exposed as hidden state.

Input: x_t = [0.5,1], h_prev = [0.3,-0.2], C_prev = [0.2,-0.4], W_f = [[0.1,0.2,0.3,0.4],[0.5,0.1,0.2,0.3]], W_i = [[0.2,0.3,0.1,0.4],[0.4,0.2,0.3,0.1]], W_c = [[0.3,0.1,0.2,0.1],[0.1,0.4,0.1,0.3]], W_o = [[0.1,0.3,0.4,0.2],[0.3,0.1,0.2,0.4]], b_f = [0.1,0], b_i = [0,-0.1], b_c = [0.1,0.1], b_o = [0,0.1]

Output: {"hidden_state":[0.197436,-0.026491],"cell_state":[0.347112,-0.040068]}

Input: x_t = [[1,0],[0,1]], h_prev = [[0,0],[0.5,-0.3]], C_prev = [[0,0],[0.2,-0.1]], W_f = [[0.1,0.2,0.3,0.4],[0.5,0.1,0.2,0.3]], W_i = [[0.2,0.3,0.1,0.4],[0.4,0.2,0.3,0.1]], W_c = [[0.3,0.1,0.2,0.1],[0.1,0.4,0.1,0.3]], W_o = [[0.1,0.3,0.4,0.2],[0.3,0.1,0.2,0.4]], b_f = [0,0], b_i = [0,0], b_c = [0,0], b_o = [0,0]

Output: {"hidden_state":[[0.061814,0.031446],[0.131932,0.039956]],"cell_state":[[0.103618,0.057254],[0.249403,0.063796]]}

Hint 1

Reuse joined = np.concatenate([h_prev, x_t], axis=-1) for all four branches.

Hint 2

Update cell_state before calculating hidden_state.

Hint 3

Return both arrays with their documented dictionary keys.

Requirements

  • Compute all four branches from the documented concatenation order.
  • Update the cell state before computing the exposed hidden state.
  • Return exactly the keys hidden_state and cell_state.

Constraints

  • x_t has shape (D,) or (N, D); h_prev and C_prev have shape (H,) or (N, H).
  • Every weight matrix has shape (H, H + D).
  • Every bias vector has shape (H,).
  • Both returned arrays use float64.