Problems
Loading...
Step 2 of 6
Input Gate
Easy

Implement the two LSTM input branches. The input gate chooses how much new information to write, while the candidate branch proposes the new content.

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).

Return a dictionary containing input_gate and candidate_state as float64 NumPy arrays. Each array has shape (H,) for one sample or (N, H) for a batch.

Loading visualization...

Examples

Input: h_prev = [0,0], x_t = [1,0.5], W_i = [[0.2,0.3,0.1,0.4],[0.4,0.2,0.3,0.1]], b_i = [0,0], W_c = [[0.3,0.1,0.2,0.1],[0.1,0.4,0.1,0.3]], b_c = [0,0]

Output: {"input_gate":[0.574443,0.586618],"candidate_state":[0.244919,0.244919]}

Explanation: Both branches read the same joined features but use different weights and activations.

Input: h_prev = [0.3,-0.2], x_t = [0.5,1], W_i = [[0.2,0.3,0.1,0.4],[0.4,0.2,0.3,0.1]], b_i = [0.1,-0.1], W_c = [[0.3,0.1,0.2,0.1],[0.1,0.4,0.1,0.3]], b_c = [0.05,0.1]

Output: {"input_gate":[0.634136,0.557248],"candidate_state":[0.309507,0.379949]}

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

Output: {"input_gate":[[0.574443,0.586618],[0.610639,0.581759]],"candidate_state":[[0.244919,0.244919],[0.263625,0.291313]]}

Hint 1

Compute joined once and reuse it for both affine transforms.

Hint 2

Use sigmoid for input_gate and np.tanh for candidate_state.

Requirements

  • Use the same [h_prev, x_t] concatenation for both branches.
  • Apply sigmoid to the input-gate branch and tanh to the candidate branch.
  • Return exactly the keys input_gate and candidate_state.

Constraints

  • Unbatched h_prev and x_t have shapes (H,) and (D,); batched inputs have shapes (N, H) and (N, D).
  • W_i and W_c have shape (H, H + D).
  • b_i and b_c have shape (H,).
  • Both returned arrays use float64.