[英]Keras LSTM input dimension setting
I was trying to train a LSTM model using keras but I think I got something wrong here. 我试图用keras训练LSTM模型,但我觉得我在这里弄错了。
I got an error of 我收到了错误
ValueError: Error when checking input: expected lstm_17_input to have 3 dimensions, but got array with shape (10000, 0, 20) ValueError:检查输入时出错:预期lstm_17_input有3个维度,但得到的数组有形状(10000,0,20)
while my code looks like 而我的代码看起来像
model = Sequential()
model.add(LSTM(256, activation="relu", dropout=0.25, recurrent_dropout=0.25, input_shape=(None, 20, 64)))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=10)
where X_train
has a shape of (10000, 20)
and the first few data points are like 其中X_train
的形状为(10000, 20)
X_train
(10000, 20)
,前几个数据点类似
array([[ 0, 0, 0, ..., 40, 40, 9],
[ 0, 0, 0, ..., 33, 20, 51],
[ 0, 0, 0, ..., 54, 54, 50],
...
and y_train
has a shape of (10000, )
, which is a binary (0/1) label array. y_train
的形状为(10000, )
,它是二进制(0/1)标签数组。
Could someone point out where I was wrong here? 有人能指出我错在哪里吗?
For the sake of completeness, here's what's happened. 为了完整起见,这就是发生的事情。
First up, LSTM
, like all layers in Keras, accepts two arguments: input_shape
and batch_input_shape
. 首先, LSTM
与batch_input_shape
所有图层一样,接受两个参数: input_shape
和batch_input_shape
。 The difference is in convention that input_shape
does not contain the batch size , while batch_input_shape
is the full input shape including the batch size . 不同之处在于, input_shape
不包含批量大小 ,而batch_input_shape
是包含批量大小的完整输入形状 。
Hence, the specification input_shape=(None, 20, 64)
tells keras to expect a 4-dimensional input, which is not what you want. 因此,规范input_shape=(None, 20, 64)
告诉keras期望一个4维输入,这不是你想要的。 The correct would have been just (20,)
. 正确的只是(20,)
。
But that's not all. 但那还不是全部。 LSTM layer is a recurrent layer, hence it expects a 3-dimensional input (batch_size, timesteps, input_dim)
. LSTM层是一个循环层,因此它需要一个三维输入(batch_size, timesteps, input_dim)
。 That's why the correct specification is input_shape=(20, 1)
or batch_input_shape=(10000, 20, 1)
. 这就是为什么正确的规范是input_shape=(20, 1)
batch_input_shape=(10000, 20, 1)
input_shape=(20, 1)
或batch_input_shape=(10000, 20, 1)
。 Plus, your training array should also be reshaped to denote that it has 20
time steps and 1
input feature per each step. 此外,您的训练阵列也应该重新形成,以表示每步有20
时间步和1
输入功能。
Hence, the solution: 因此,解决方案:
X_train = np.expand_dims(X_train, 2) # makes it (10000,20,1)
...
model = Sequential()
model.add(LSTM(..., input_shape=(20, 1)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.