简体   繁体   English

使用LSTM进行序列分类,检查输入时出错

[英]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.

相关问题 Python Keras LSTM“检查输入时出错” - Python Keras LSTM “ Error when checking input” 检查输入时出错:预期 lstm_1_input 有 3 个维度 - Error when checking input: expected lstm_1_input to have 3 dimensions 序列分类 Tensorflow - LSTM - Sequence classification Tensorflow - LSTM Keras LSTM:检查模型输入尺寸时出错 - Keras LSTM: Error when checking model input dimension Tensorflow 检查输入时抛出值错误(lstm_1) - Tensorflow throw value error when checking input (lstm_1) 如何将图像序列输入LSTM网络以进行视频分类 - How to input the sequence of the images in to LSTM network for video classification 文本分类RNN-LSTM-错误检查目标 - Text-Classification RNN - LSTM - Error checking target ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(393613,50) - ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50) !! ValueError:检查输入时出错:预期lstm_2_input具有3个维,但数组的形状为(4982,12) - !! ValueError: Error when checking input: expected lstm_2_input to have 3 dimensions, but got array with shape (4982, 12) ValueError:检查输入时出错:预期 lstm_11_input 具有形状 (1, 1) 但得到形状为 (1, 3841) 的数组 - ValueError: Error when checking input: expected lstm_11_input to have shape (1, 1) but got array with shape (1, 3841)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM