为什么 Keras 模型中的第一个 LSTM 比后续的有更多的参数?

Why does the first LSTM in a Keras model have more params than the subsequent one?

我只是从一个相当简单的顺序模型中查看 Keras 模型的细节,其中我有多个 LSTM 层,一个接一个。我很惊讶地发现第一层总是有更多的参数,尽管与后续的 LSTM 层具有相同的定义。

这里的模型定义说明的很清楚:

Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 400, 5)            380       
_________________________________________________________________
lstm_2 (LSTM)                (None, 400, 5)            220       
_________________________________________________________________
time_distributed_1 (TimeDist (None, 400, 650)          3900      
_________________________________________________________________
lstm_3 (LSTM)                (None, 400, 20)           53680     
_________________________________________________________________
lstm_4 (LSTM)                (None, 400, 20)           3280      
_________________________________________________________________

同样在时间分布的密集层之后,接下来的两个相同的LSTM也是如此。

我对 LSTM 的理解是否不正确,相同的定义不会导致生成与末尾标记的 'duplicate' 相同的层,或者参数计数中是否有其他我需要的东西明白吗?目前我觉得它很奇怪!

任何解释都将有助于我 (a) 更好地理解,以及 (b) 基于这些新知识构建性能更高的模型。

LSTM 的输出仅取决于其 units

我们看到您的第一层都有 5 个单位。
另外两个有20个单位。

但是可训练参数(对输入执行计算并带来预期输出的数字),这些需要考虑有多少输入特征,因为它们会必须在计算中考虑所有输入。

输入越大,需要的参数越多。我们可以看出输入中有超过 5 个特征。对于最后两层,第一层的输入是 650,而另一层是 20。


详细的参数数量。

在LSTM层中,如their code中所见,有3组权重:

  • 内核 - 形状为 (units, 4*inputs)
  • 循环内核 - 形状为 (units,4*units)
  • 偏差 - 形状为 (4*units,)

通过一些计算,我们可以推断出您的输入具有 (None, 400, 13)

的形状
Layer (type)         Output Shape        Param #   
========================================================================
input_6 (InputLayer) (None, 400, 13)     0         
________________________________________________________________________
lstm_1 (LSTM)        (None, 400, 5)      380   = 4*(13*5 + 5*5 + 5)   
________________________________________________________________________
lstm_2 (LSTM)        (None, 400, 5)      220   = 4*(5*5 + 5*5 + 5)     
________________________________________________________________________
time_distributed_1   (None, 400, 650)    3900  = ?  
________________________________________________________________________
lstm_3 (LSTM)        (None, 400, 20)     53680 = 4*(650*20 + 20*20 + 20)   
________________________________________________________________________
lstm_4 (LSTM)        (None, 400, 20)     3280  = 4*(20*20 + 20*20 + 20)    
________________________________________________________________________
  • LSTM 1 个参数 = 4*(13*5 + 5*5 + 5)
  • LSTM 2 个参数 = 4*(5*5 + 5*5 + 5)
  • 分配时间=??
  • LSTM 3 个参数 = 4*(650*20 + 20*20 + 20)
  • LSTM 4 个参数 = 4*(20*20 + 20*20 + 20)

其他层也有类似的行为

如果您使用密集层进行测试,您还会看到:

Layer (type)         Output Shape    Param #   
=========================================================
input_6 (InputLayer) (None, 13)      0      
_________________________________________________________
dense_1 (Dense)      (None, 5)       70    = 13*5 + 5      
_________________________________________________________
dense_2 (Dense)      (None, 5)       30    = 5*5 + 5   
_________________________________________________________
dense_3 (Dense)      (None, 650)     3900  = 5*650 + 650     
_________________________________________________________
dense_4 (Dense)      (None, 20)      13020 = 650*20 + 20   
_________________________________________________________
dense_5 (Dense)      (None, 20)      420   = 20*20 + 20    
=========================================================

区别在于密集层没有循环核,它们的核也没有乘以4。

  • 密集1个参数=13*5+5
  • 密集2个参数=5*5+5
  • 密集3个参数=5*650+650
  • 密集4个参数=650*20+20
  • 密集5个参数=20*20+20