如何计算LSTM网络的参数数量?

时间:2016-06-28 15:13:05

标签: machine-learning neural-network deep-learning keras lstm

有没有办法计算LSTM网络中的参数总数。

我找到了一个例子,但我不确定this是多么正确或者我是否理解正确。

例如,考虑以下示例: -

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.layers import LSTM
model = Sequential()
model.add(LSTM(256, input_dim=4096, input_length=16))
model.summary()

输出

____________________________________________________________________________________________________
Layer (type)                       Output Shape        Param #     Connected to                     
====================================================================================================
lstm_1 (LSTM)                      (None, 256)         4457472     lstm_input_1[0][0]               
====================================================================================================
Total params: 4457472
____________________________________________________________________________________________________

根据我的理解n是输入向量长度。 而m是时间步数。在这个例子中,他们认为隐藏层的数量为1。

因此,根据我的示例中的the post. 4(nm+n^2)中的公式m=16; n=4096; num_of_units=256

4*((4096*16)+(4096*4096))*256 = 17246978048

为什么会有这样的差异? 我误解了这个例子,或者公式错了吗?

5 个答案:

答案 0 :(得分:22)

否 - Keras中LSTM层的参数数量等于:

params = 4 * ((size_of_input + 1) * size_of_output + size_of_output^2)

其他1来自偏见条款。因此n是输入的大小(由偏差项增加),m是LSTM图层的输出大小。

最后:

4 * (4097 * 256 + 256^2) = 4457472

答案 1 :(得分:8)

enter image description here image via this post

num_params = [(num_units + input_dim + 1) * num_units] * 4

num_units + input_dim :concat [h(t-1),x(t)]

+ 1:偏向

* 4::共有4个神经网络层(黄色框){W_forget,W_input,W_output,W_cell}

model.add(LSTM(units=256, input_dim=4096, input_length=16))

[(256 + 4096 + 1)* 256] * 4 = 4457472

PS:num_units = num_hidden_​​units = output_dims

答案 2 :(得分:1)

LSTM方程(通过deeplearning.ai Coursera)

enter image description here

  • 从等式可以看出,所有6个等式的最终尺寸都相同,并且最终尺寸必须等于a(t)的尺寸

  • 在这6个方程中,只有4个方程对参数数量有所贡献,并且通过看这些方程可以推断出这4个方程都是对称的。因此,如果我们找到了一个方程的参数数量,则可以将其乘以4并得出参数总数。

  • 重要的一点是要注意,参数的总数不取决于时间步长(或input_length),因为在整个时间步长中共享相同的“ W”和“ b”。

  • 假设,LSTM单元的内部成员只有一层用于门(如Keras中的那样)。

  • 取等式1并使之相关。让该层中的神经元数量为 n ,并且x维度的数量为 m (不包括示例数和时间步长)。因此,忘记门的尺寸也将为 n 。现在,与ANN中的一样,``Wf''的尺寸将为n *(n + m),而``bf''的尺寸将为n。因此,一个方程式的参数总数将为 [{n *(n + m)} + n] 。因此,参数总数将为4 * [{{n *(n + m)} + n]。让我们打开括号,我们将得到-> 4 *(nm + n 2 + n)

  • 因此,根据您的价值观。将其输入公式可得出:->(n = 256,m = 4096),参数总数为4 *((256 * 256)+(256 * 4096)+(256))= 4 *(1114368)= 4457472.

  • 答案 3 :(得分:0)

    我认为,如果我们从简单的RNN开始,会更容易理解。

    让我们假设我们有4个单位(请忽略网络中的...,而只专注于可见单位),并且输入大小(维数)为3:

    enter image description here

    循环连接的权数为28 = 16(num_units * num_units)+输入为12(input_dim * num_units)。偏差的数量仅为num_units

    递归意味着每个神经元输出被反馈到整个网络,因此,如果我们按时间顺序展开,它看起来像两个密集层:

    enter image description here

    这很清楚为什么我们对循环部分使用num_units * num_units权重。

    此简单RNN的参数数量为32 = 4 * 4 + 3 * 4 + 4,可以表示为num_units * num_units + input_dim * num_units + num_unitsnum_units * (num_units + input_dim + 1)

    现在,对于LSTM,我们必须将这些参数的数量乘以4,因为这是每个单元内的子参数的数量,@ FelixHo的答案很好地说明了这一点

    答案 4 :(得分:0)

    其他人几乎回答了它。但是只是为了进一步澄清,有关创建LSTM层。参数数目如下:

    参数数量= 4 *((使用的功能数+1)*数量单位+ num_units ^ 2)

    +1是因为我们还有其他偏见。

    其中num_features是LSTM输入形状中的num_features: Input_shape =(window_size,num_features)