[英]Sequence classification with LSTM, Error when checking input
I am building my first neural network with LSTM and I have an error in the input size. 我正在用LSTM构建我的第一个神经网络,但是输入大小有错误。
I guess the error is in the input parameters, in the size, the dimension but I can not understand the error. 我猜错误是在输入参数,大小,尺寸上,但我无法理解该错误。
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)
shape: 形状:
(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
_________________________________________________________________
The error output tells me the following: 错误输出告诉我以下内容:
---> 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)
LSTM requires input of shape (batch_size, timestep, feature_size)
. LSTM需要输入形状
(batch_size, timestep, feature_size)
。 You are passing only two dimension features. 您仅传递二维特征。 Since
timesteps=13
you need to add one more dimension to your input. 由于
timesteps=13
您需要在输入中再增加一个维度。
If data is a numpy array, then: data = data[..., np.newaxis]
should do it. 如果data是一个numpy数组,则应使用:
data = data[..., np.newaxis]
。
Shape of data now will be (batch_size, timesteps, feature)
viz. 数据的形状现在将是
(batch_size, timesteps, feature)
。 (226, 13, 1)
. (226, 13, 1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.