RNN Step Forward (Tanh Cell)
RNN Step Forward (Tanh Cell)
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)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 th_prev: shape (H,)- previous hidden stateWx: shape (D, H)- input-to-hidden weightsWh: shape (H, H)- hidden-to-hidden weightsb: shape (H,)- bias vector
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_tas 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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
RNN Step Forward (Tanh Cell)
RNN Step Forward (Tanh Cell)
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)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 th_prev: shape (H,)- previous hidden stateWx: shape (D, H)- input-to-hidden weightsWh: shape (H, H)- hidden-to-hidden weightsb: shape (H,)- bias vector
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_tas 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
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array