Keras SimpleRNN参数数量疑问:为何SimpleRNN(10)的参数为120?
Hey there! Great question about RNN parameter counts—this trips up a lot of folks when they first start working with recurrent layers, so let’s break down exactly why your SimpleRNN(10, input_shape=(3, 1)) layer has 120 parameters.
First, let’s recap your model setup for context:
model.add(SimpleRNN(10, input_shape=(3, 1))) model.add(Dense(1, activation="linear"))
The key thing to remember is that RNN parameter counts depend only on the number of input features and the number of hidden units—the time step count (the 3 in your input_shape) doesn’t affect how many parameters the layer has. Here’s the breakdown for your SimpleRNN layer:
SimpleRNN layers have three types of parameters:
- Input-to-hidden weights: Connects each input feature to each hidden unit. For your case, input features = 1 (from
input_shape=(3,1)), hidden units = 10. So this is1 * 10 = 10parameters. - Hidden-to-hidden recurrent weights: Connects each hidden unit to every other hidden unit (including itself) for the looped recurrence. That’s
10 * 10 = 100parameters. - Hidden layer biases: A bias term for each hidden unit. That’s
10parameters.
Adding those up: 10 + 100 + 10 = 120—which matches exactly what your model summary shows!
You can also use this general formula to calculate SimpleRNN parameters anytime:Total Parameters = (input_dim + units) * units + units = (input_dim + units + 1) * units
Plugging in your values: (1 + 10 + 1)*10 = 12*10 = 120—perfect, same result.
As a quick sanity check: if you changed your input to have 2 features (e.g., input_shape=(3,2)), the total parameters would jump to (2+10+1)*10=130—you can test that yourself to confirm the math holds.
内容的提问来源于stack exchange,提问作者youngtackpark

