我有一个数据集,我想预测每t + 1次的未来价值。我用LSTM预测了未来的价值。但是我不知道为什么我的代码会出现错误,并且也没有很好的预测。 这是我的代码。
model = Sequential()
model.add(LSTM(4, return_sequences=True,input_shape= (x_train_n.shape[2])))
# returns a sequence of vectors of dimension 32
model.add(LSTM(8, return_sequences=True)) # returns a sequence of vectors of dimension 32
model.add(LSTM(8)) # return a single vector of dimension 32
model.add(Dense(1))
batchsize = 1
model.compile(loss="mean_squared_error",optimizer="adam")
#model.compile(loss='mean_squared_error', optimizer='adadelta',metrics= ['accuracy'])
history = model.fit(x_train_n,y_train_n, batch_size = batchsize,
nb_epoch=30,validation_data=(x_test_n, y_test_n),shuffle =True)\
model.reset_states()
pred1=model.predict(x_test_n)
pred2= model.predict(x_train_n)
#end of the sequences
model.reset_states()
pred1 = scaler_y.inverse_transform(np.array(pred1).reshape ((len(pred1), 1)))
real_test = scaler_y.inverse_transform(np.array(y_test_n).reshape ((len(y_test_n), 1))).astype(int)
real = scaler_y.inverse_transform(np.array(y_train_n).reshape ((len(y_train_n), 1))).astype(int)
pred1 = pred1[:,0]
real_test = real_test[:,0]
sequence_timestep = 1
last_sequence_train = x_train_n[-1]
pred1 = []
def sequence_constructor():
if len(pred1) >= sequence_timestep:
new_sequence = pred1[-x_train:]
else:
splitter = sequence_timestep - len(pred1)
part_1 = last_sequence_train[-splitter:]
new_sequence = np.append(part_1,pred1) #Concatenate 2 list
new_sequence = np.array(new_sequence)
return new_sequence
在我的csv文件开始时间t中,下一个预测值将为t + 1