#393RNN parameters — weight sharing across timeEasy

RNN parameters — weight sharing across time

Background

An RNN reuses the same weights at every timestep, so its parameter count depends on the sizes, not the sequence length. For input size DD and hidden size HH:

HDWxh+HHWhh+Hbh=H(D+H+1)\underbrace{H \cdot D}_{W_{xh}} + \underbrace{H \cdot H}_{W_{hh}} + \underbrace{H}_{b_h} = H\,(D + H + 1)

Problem statement

Implement rnn_param_count(input_size, hidden_size) returning the number of parameters in a vanilla RNN cell.

Input

  • input_sizeint, DD.
  • hidden_sizeint, HH.

Output

Returns an int: H(D+H+1)H(D + H + 1).

Examples

Example 1

Input:  input_size = 3, hidden_size = 4
Output: 32

Explanation: 4×3+4×4+4=12+16+4=324\times3 + 4\times4 + 4 = 12 + 16 + 4 = 32.

Example 2

Input:  input_size = 10, hidden_size = 20
Output: 620

Constraints

  • Count W_xh (H×DH\times D), W_hh (H×HH\times H), and bias (HH).
  • The answer must not depend on the sequence length.
  • Return a plain int.

Notes

  • This length-independence is exactly weight sharing: a 5-token and a 500-token sequence use the identical parameter budget, which is how RNNs handle variable-length inputs.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example: 3 in, 4 hidden
  • Reference: 10 in, 20 hidden
  • Sample: single in and hidden