LSTM Gate Computation
Compute the LSTM gates for one timestep.
Given previous hidden state , previous cell state , and input , compute all four gates by concatenating into a combined vector, then:
- Forget gate:
- Input gate:
- Candidate:
- Cell state:
- Output gate:
- Hidden state:
Return (h_t, c_t) both as lists rounded to 4 decimal places.
Example
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]
([0.1743], [0.2876])
- Inputs: hidden size 1, input size 1. The memory starts empty (, ) and . Each weight matrix is : one row per hidden unit, one column per entry of . All biases are 0.
- Gates: every gate gets the same pre-activation, . So , and the candidate is .
- Cell state: . The old cell was empty, so all of the memory comes from the candidate.
- Hidden state: , so .
- Output:
([0.1743], [0.2876]), two lists of length hidden_size, first. Since , , with equality only when . Round only at the end: multiplying the rounded 0.6225 and 0.4621 gives 0.2877.
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