我有一些由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:]))
如果错误仍然存在或者出现了新的错误,请告诉我!