所以我有形成图案的2D矢量序列。我想预测序列如何继续。 我有一个由start_x和start_y顺序组成的数组组成的start_xy数组: 例如[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]
我怀疑我必须以某种方式重塑我的输入,但是我还不知道如何。 我该如何工作? 我什至以正确的方式这样做吗?
答案 0 :(得分:0)
您几乎只需要将数据转换为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:]))