有没有办法计算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
为什么会有这样的差异? 我误解了这个例子,或者公式错了吗?
答案 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)
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)
答案 3 :(得分:0)
我认为,如果我们从简单的RNN开始,会更容易理解。
让我们假设我们有4个单位(请忽略网络中的...,而只专注于可见单位),并且输入大小(维数)为3:
循环连接的权数为28 = 16(num_units * num_units
)+输入为12(input_dim * num_units
)。偏差的数量仅为num_units
。
递归意味着每个神经元输出被反馈到整个网络,因此,如果我们按时间顺序展开,它看起来像两个密集层:
这很清楚为什么我们对循环部分使用num_units * num_units
权重。
此简单RNN的参数数量为32 = 4 * 4 + 3 * 4 + 4,可以表示为num_units * num_units + input_dim * num_units + num_units
或num_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)