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). it=σ(Wi[ht−1,xt]+bi). Ct=tanh(Wc[ht−1,xt]+bc). Ct=ft⊙Ct−1+it⊙Ct. ot=σ(Wo[ht−1,xt]+bo). ht=ot⊙tanh(Ct).H is the hidden width and D is the input width. Return a dictionary containing hidden_state and cell_state as float64 NumPy arrays.
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.
All steps in LSTM
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
LSTM Cell
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). it=σ(Wi[ht−1,xt]+bi). Ct=tanh(Wc[ht−1,xt]+bc). Ct=ft⊙Ct−1+it⊙Ct. ot=σ(Wo[ht−1,xt]+bo). ht=ot⊙tanh(Ct).H is the hidden width and D is the input width. Return a dictionary containing hidden_state and cell_state as float64 NumPy arrays.
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.
All steps in LSTM
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array