我正在用LSTM构建我的第一个神经网络,输入大小有误。
我猜错误在于输入参数,大小,尺寸,但我无法理解错误。
print df.shape
data_dim = 13
timesteps = 13
num_classes = 1
batch_size = 32
model = Sequential()
model.add(LSTM(32, return_sequences = True, stateful = True,
batch_input_shape = (batch_size, timesteps, data_dim)))
model.add(LSTM(32, return_sequences = True, stateful = True))
model.add(LSTM(32, stateful = True))
model.add(Dense(1, activation = 'relu'))
#Compile.
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.summary()
#Fit.
history = model.fit(data[train], label[train], epochs = iteraciones, verbose = 0)
#Eval.
scores = model.evaluate(data[test], label[test], verbose = 0)
#Save.
cvshistory.append(history)
cvscores.append(scores[1] * 100)
形状:
(303, 14)
summary:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_19 (LSTM) (32, 13, 32) 5888
_________________________________________________________________
lstm_20 (LSTM) (32, 13, 32) 8320
_________________________________________________________________
lstm_21 (LSTM) (32, 32) 8320
_________________________________________________________________
dense_171 (Dense) (32, 1) 33
=================================================================
Total params: 22,561
Trainable params: 22,561
Non-trainable params: 0
_________________________________________________________________
错误输出告诉我以下内容:
---> 45 history = model.fit(data[train], label[train], epochs = iteraciones, verbose = 0)
ValueError: Error when checking input: expected lstm_19_input to have 3 dimensions, but got array with shape (226, 13)
答案 0 :(得分:0)
LSTM需要输入形状(batch_size, timestep, feature_size)
。您仅传递二维特征。由于timesteps=13
,您需要在输入中再添加一个维度。
如果数据是一个numpy数组,则:
data = data[..., np.newaxis]
应该这样做。
现在数据的形状将为(batch_size, timesteps, feature)
。 (226, 13, 1)
。