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

2024-04-26 18:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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)

Tags: addtrueinputverbosedatamodel错误batch
1条回答
网友
1楼 · 发布于 2024-04-26 18:33:13

LSTM需要输入shape(batch_size, timestep, feature_size)。您只传递二维特征。由于timesteps=13,您需要在输入中再添加一个维度。你知道吗

如果数据是numpy数组,则: data = data[..., np.newaxis]应该这样做。你知道吗

现在数据的形状将是(batch_size, timesteps, feature),即。(226, 13, 1)。你知道吗

相关问题 更多 >