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

我在构建第一个使用LSTM的神经网络时,遇到了输入大小的错误。

我猜测错误出在输入参数的尺寸和维度上,但我无法理解这个错误。

print df.shapedata_dim = 13timesteps = 13num_classes = 1batch_size = 32model = 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,561Trainable params: 22,561Non-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)

回答:

LSTM需要形状为(batch_size, timestep, feature_size)的输入。你传递的只是二维特征。由于timesteps=13,你需要为你的输入增加一个维度。

如果数据是numpy数组,那么data = data[..., np.newaxis]应该可以解决这个问题。

现在数据的形状将变为(batch_size, timesteps, feature),即(226, 13, 1)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注