PIXELBANKv9.1.0
Menu

Compute the LSTM gates for one timestep.

Given previous hidden state ht−1h_{t-1}, previous cell state ct−1c_{t-1}, and input xtx_t, compute all four gates by concatenating [ht−1,xt][h_{t-1}, x_t] into a combined vector, then:

  • Forget gate: f=σ(Wf⋅[h,x]+bf)f = \sigma(W_f \cdot [h, x] + b_f)
  • Input gate: i=σ(Wi⋅[h,x]+bi)i = \sigma(W_i \cdot [h, x] + b_i)
  • Candidate: c~=tanh⁡(Wc⋅[h,x]+bc)\tilde{c} = \tanh(W_c \cdot [h, x] + b_c)
  • Cell state: ct=f⊙ct−1+i⊙c~c_t = f \odot c_{t-1} + i \odot \tilde{c}
  • Output gate: o=σ(Wo⋅[h,x]+bo)o = \sigma(W_o \cdot [h, x] + b_o)
  • Hidden state: ht=o⊙tanh⁡(ct)h_t = o \odot \tanh(c_t)

Return (h_t, c_t) both as lists rounded to 4 decimal places.

Example:

Input:
h_prev = [0]
c_prev = [0]
x = [1]
W_f = [[0.5, 0.5]]
b_f = [0]
W_i = [[0.5, 0.5]]
b_i = [0]
W_c = [[0.5, 0.5]]
b_c = [0]
W_o = [[0.5, 0.5]]
b_o = [0]
Output:
([0.1743], [0.2876])
Reasoning:
  • First, we concatenate ht−1h_{t-1} and xtx_t into a combined vector [h,x]=[0,1][h, x] = [0, 1].
  • Then, we compute the forget gate: f=σ(Wf⋅[h,x]+bf)=σ([0.5,0.5]⋅[0,1]+0)=σ(0.5)f = \sigma(W_f \cdot [h, x] + b_f) = \sigma([0.5, 0.5] \cdot [0, 1] + 0) = \sigma(0.5), the input gate: i=σ(Wi⋅[h,x]+bi)=σ(0.5)i = \sigma(W_i \cdot [h, x] + b_i) = \sigma(0.5), and the candidate: c~=tanh⁡(Wc⋅[h,x]+bc)=tanh⁡(0.5)\tilde{c} = \tanh(W_c \cdot [h, x] + b_c) = \tanh(0.5).
  • Next, we calculate the cell state: ct=f⊙ct−1+i⊙c~c_t = f \odot c_{t-1} + i \odot \tilde{c}, and the output gate: o=σ(Wo⋅[h,x]+bo)=σ(0.5)o = \sigma(W_o \cdot [h, x] + b_o) = \sigma(0.5).
  • The final output is calculated as ht=o⊙tanh⁡(ct)h_t = o \odot \tanh(c_t), resulting in ht=[0.1743]h_t = [0.1743] and ct=[0.2876]c_t = [0.2876].

Constraints:

  • h_prev, c_prev: 1D lists (hidden_size)
  • x: 1D list (input_size)
  • W_f, W_i, W_c, W_o: 2D lists (hidden_size x (hidden_size + input_size))
  • b_f, b_i, b_c, b_o: 1D lists (hidden_size)
  • Return tuple (h_t, c_t) rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
LSTM Gate Computation - Hard | PixelBank