TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

RNN Step Forward (Tanh Cell)

Neural NetworksNLP
Easy

Implement one step of a tanh recurrent neural network:

at=xtWx+ht−1Wh+b\mathbf{a}_t=\mathbf{x}_tW_x+\mathbf{h}_{t-1}W_h+\mathbf{b}at​=xt​Wx​+ht−1​Wh​+b ht=tanh⁡(at)\mathbf{h}_t=\tanh(\mathbf{a}_t)ht​=tanh(at​)

Here, xt∈RD\mathbf{x}_t\in\mathbb{R}^{D}xt​∈RD is the current input, ht−1∈RH\mathbf{h}_{t-1}\in\mathbb{R}^{H}ht−1​∈RH is the previous hidden state, Wx∈RD×HW_x\in\mathbb{R}^{D\times H}Wx​∈RD×H maps inputs, Wh∈RH×HW_h\in\mathbb{R}^{H\times H}Wh​∈RH×H maps the previous state, and b∈RH\mathbf{b}\in\mathbb{R}^{H}b∈RH is the bias. Return ht\mathbf{h}_tht​ as a one-dimensional NumPy array.

Loading visualization...

Examples

Input: x_t = [1.0, 0.0], h_prev = [0.0, 0.0], Wx = [[1.0, 0.0], [0.0, 1.0]], Wh = [[0.0, 0.0], [0.0, 0.0]], b = [0.0, 0.0]

Output: [0.761594, 0.0]

Explanation: Only the first input coordinate reaches the pre-activation, so tanh is applied to [1, 0].

Input: x_t = [0.0, 0.0], h_prev = [1.0, -1.0], Wx = [[0.0, 0.0], [0.0, 0.0]], Wh = [[1.0, 0.0], [0.0, 1.0]], b = [0.0, 0.0]

Output: [0.761594, -0.761594]

Hint 1

Compute the pre-activation with x_t @ Wx + h_prev @ Wh + b.

Hint 2

Pass the complete pre-activation array to np.tanh.

Requirements

  • Compute both affine contributions and add the bias
  • Apply np.tanh elementwise
  • Do not modify any input array
  • Return a one-dimensional NumPy array of length H

Constraints

  • All arrays contain finite floating-point values
  • Matrix and vector shapes follow the stated dimensions
  • Use NumPy only
Try Similar Problems
Rnn Step BackwardGru Cell ForwardTanh ActivationLinear Layer ForwardSigmoid Numpy

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
PrevNext

RNN Step Forward (Tanh Cell)

Neural NetworksNLP
Easy

Implement one step of a tanh recurrent neural network:

at=xtWx+ht−1Wh+b\mathbf{a}_t=\mathbf{x}_tW_x+\mathbf{h}_{t-1}W_h+\mathbf{b}at​=xt​Wx​+ht−1​Wh​+b ht=tanh⁡(at)\mathbf{h}_t=\tanh(\mathbf{a}_t)ht​=tanh(at​)

Here, xt∈RD\mathbf{x}_t\in\mathbb{R}^{D}xt​∈RD is the current input, ht−1∈RH\mathbf{h}_{t-1}\in\mathbb{R}^{H}ht−1​∈RH is the previous hidden state, Wx∈RD×HW_x\in\mathbb{R}^{D\times H}Wx​∈RD×H maps inputs, Wh∈RH×HW_h\in\mathbb{R}^{H\times H}Wh​∈RH×H maps the previous state, and b∈RH\mathbf{b}\in\mathbb{R}^{H}b∈RH is the bias. Return ht\mathbf{h}_tht​ as a one-dimensional NumPy array.

Loading visualization...

Examples

Input: x_t = [1.0, 0.0], h_prev = [0.0, 0.0], Wx = [[1.0, 0.0], [0.0, 1.0]], Wh = [[0.0, 0.0], [0.0, 0.0]], b = [0.0, 0.0]

Output: [0.761594, 0.0]

Explanation: Only the first input coordinate reaches the pre-activation, so tanh is applied to [1, 0].

Input: x_t = [0.0, 0.0], h_prev = [1.0, -1.0], Wx = [[0.0, 0.0], [0.0, 0.0]], Wh = [[1.0, 0.0], [0.0, 1.0]], b = [0.0, 0.0]

Output: [0.761594, -0.761594]

Hint 1

Compute the pre-activation with x_t @ Wx + h_prev @ Wh + b.

Hint 2

Pass the complete pre-activation array to np.tanh.

Requirements

  • Compute both affine contributions and add the bias
  • Apply np.tanh elementwise
  • Do not modify any input array
  • Return a one-dimensional NumPy array of length H

Constraints

  • All arrays contain finite floating-point values
  • Matrix and vector shapes follow the stated dimensions
  • Use NumPy only
Try Similar Problems
Rnn Step BackwardGru Cell ForwardTanh ActivationLinear Layer ForwardSigmoid Numpy

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.