Problems
Loading...
1 / 1

RNN Step Forward (Tanh Cell)

Neural NetworksNLP
Easy

Implement a single RNN step for a basic tanh RNN cell. At each time step, the RNN takes the current input and previous hidden state to compute the new hidden state.

Mathematical Formula

RNN Hidden State Update:

ht=tanh(xtWx+hprevWh+b)h_t = \tanh(x_t W_x + h_{\text{prev}} W_h + b)

where xt tis the input at time t, hprev is the previous hidden state, and Wx,Wh,b are the learned parameters.

Function Arguments

  • x_t: shape (D,) - input at time t
  • h_prev: shape (H,) - previous hidden state
  • Wx: shape (D, H) - input-to-hidden weights
  • Wh: shape (H, H) - hidden-to-hidden weights
  • b: shape (H,) - bias vector
Loading visualization...

Examples

Input: xt=[1.0,0.0],hprev=[0.0,0.0]

Weights: Wx=I2×2,Wh=0,b=0

Output: ht=tanh⁡([1.0,0.0])≈[0.7616,0.0]

Input: xt=[0.0,0.0],hprev=[1.0,-1.0]

Weights: Wx=0,Wh=I2×2,b=0

Output: ht=tanh⁡([1.0,-1.0])≈[0.7616,-0.7616]

Hint 1

Use @ operator for matrix multiplication: x_t @ Wx and h_prev @ Wh.

Hint 2

Combine the three terms before applying tanh: np.tanh().

Requirements

  • Compute: pre_act = x_t @ Wx + h_prev @ Wh + b
  • Apply: h_t = np.tanh(pre_act)
  • Return h_t as 1D array of shape (H,)
  • Do not modify inputs in place
  • No batching: single time-step vectors only

Constraints

  • 1 ≤ D, H ≤ 256
  • Use NumPy only
  • Time limit: 200ms
Try Similar Problems