2

I have this model structure and want to know the formula for calculating the parameters count in the GRU layer. I did not find that in the docs.

model = Sequential()
model.add(layers.GRU(32, input_shape=(None, float_data_1.shape[-1]))) 
model.add(layers.Dense(1))
model.compile(optimzer=RMSprop(), loss='mae')
history = model.fit(train_gen_1,
                steps_per_epoch=per_epoch,
                epochs=20,
                validation_data=val_gen_1,
                validation_steps=val_steps)
# model summary 
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
gru (GRU)                    (None, 32)                6336      
_________________________________________________________________
dense (Dense)                (None, 1)                 33        
=================================================================
Total params: 6,369
Trainable params: 6,369
Non-trainable params: 0
_________________________________________________________________

I found the equation in this answer but is does not produce the right results. I would be interested also in the information source as I would need to cite it in an academic report.

the input data shape is (85880, 32)

Hesham
  • 21
  • 2

1 Answers1

1

This is calculated as = (32x32 + 32x32 + 32x2)x3 = 6336 Let's try with different numbers -

model = Sequential()
model.add(tf.keras.layers.GRU(5, input_shape=(None, 25))) 
model.add(tf.keras.layers.Dense(1))
model.summary()

Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
gru_7 (GRU)                  (None, 5)                 480       
_________________________________________________________________
dense_10 (Dense)             (None, 1)                 6         
=================================================================
Total params: 486
Trainable params: 486
Non-trainable params: 0

Number of parameters = (25x5 + 5x5 + 5x2)x3 = 480

If you add the parameter reset_after=False in GRU layer, bias will be counted once - tf.keras.layers.GRU(5, input_shape=(None, 25), reset_after=False)

user3476359
  • 111
  • 2