TF Keras: ValueError: 层的输入0与层不兼容:期望的维度为3,发现的维度为2

我有一些由2D向量组成的序列,这些序列形成了模式。我想预测序列如何继续。我有一个start_xy数组,其中包含顺序、start_x和start_y,例如 [1, 2.4, 3.8],end_xy也是同样的结构。

我想训练一个序列预测模型:

import numpy as np
import pickle
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.callbacks import ModelCheckpoint
import training_data_generator
tdg = training_data_generator.training_data_generator(500)
trainingdata = tdg.produceTrainingSequences()
print("Printing DATA!:")
start_xy =[]
end_xy =[]
for batch in trainingdata:
    for pattern in batch:
        order = 1
        for sequence in pattern:
            start = [order,sequence[0],sequence[1]] 
            start_xy.append(start)
            end = [order,sequence[2],sequence[3]]
            end_xy.append(end)
            order = order +1
model = Sequential()
model.add(LSTM(64, return_sequences=False, input_shape=(2,len(start_xy))))
model.add(Dense(2, activation='relu'))
model.compile(loss='mse', optimizer='adam')
model.fit(start_xy,end_xy,batch_size=len(start_xy), epochs=5000,  verbose=2)

但我得到了以下错误信息:

 ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [320, 3]

我怀疑我需要以某种方式重塑我的输入,但我还不明白怎么做。我该怎么让这个工作?我这样做对吗?


回答:

你主要需要将你的数据转换为numpy数组,并对这些数据进行一些重塑,使模型能够接受它。

首先将start_xy转换为numpy数组,并将其重塑为3个维度:

start_xy = np.array(start_xy)
start_xy = start_xy.reshape(*start_xy.shape, 1)

接下来修正LSTM层的输入形状为[3, 1]:

model.add(LSTM(64, return_sequences=False, input_shape=start_xy.shape[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中创建了一个多类分类项目。该项目可以对…

发表回复

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