了解可训练的LSTM参数的数量

了解可训练的LSTM参数的数量

本文介绍了Keras:了解可训练的LSTM参数的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行 Keras LSTM演示包含以下代码(在第166行之后):

I have run a Keras LSTM demo containing the following code (after line 166):

m = 1
model=Sequential()
dim_in = m
dim_out = m
nb_units = 10

model.add(LSTM(input_shape=(None, dim_in),
                    return_sequences=True,
                    units=nb_units))
model.add(TimeDistributed(Dense(activation='linear', units=dim_out)))
model.compile(loss = 'mse', optimizer = 'rmsprop')

当我预先呼叫model.summary()时,会看到以下输出:

When I prepend a call to model.summary(), I see the following output:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm_4 (LSTM)                (None, None, 10)          480
_________________________________________________________________
time_distributed_4 (TimeDist (None, None, 1)           11
=================================================================
Total params: 491
Trainable params: 491
Non-trainable params: 0

我了解到时间分布层的11个参数仅由nb_units权重和一个偏差值组成.

I understand that the 11 params of the time distributed layer simply consist of nb_units weights plus one bias value.

现在用于LSTM层:这些 答案说:

Now for the LSTM layer:These answers say:

params = 4 * ((input_size + 1) * output_size + output_size^2)

在我使用input_size = 1output_size = 1的情况下,这对于10个单位中的每一个仅产生12个参数,总计120个参数.与报告的480相比,误差减少了4倍.我的错误在哪里?

In my case with input_size = 1 and output_size = 1 this yields only 12 parameters for each of the 10 units, totaling to 120 parameters. Compared to the reported 480, this is off by a factor of 4. Where is my error?

推荐答案

params公式适用于整个图层,而不适用于每个Keras单位.

The params formula holds for the whole layer, not per Keras unit.

引用此答案:

Keras中的LSTM仅精确定义了一个LSTM块,其单元长度为单位长度.

LSTM in Keras only define exactly one LSTM block, whose cells is of unit-length.

直接设置output_size = 10(像这样评论)正确地产生了480个参数.

Directly setting output_size = 10 (like in this comment) correctly yields the 480 parameters.

这篇关于Keras:了解可训练的LSTM参数的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:28