[英]Regression with LSTM - python and Keras
I am trying to use a LSTM network in Keras to make predictions of timeseries data one step into the future.我正在尝试使用 Keras 中的 LSTM 网络来预测未来的时间序列数据。 The data I have is of 5 dimensions, and I am trying to use the previous 3 periods of readings to predict the a future value in the next period.我拥有的数据有 5 个维度,我试图使用前 3 个时期的读数来预测下一个时期的未来值。 I have normalised the data and removed all NaN etc, and this is the code I am trying to use to train the network:我已经对数据进行了标准化并删除了所有 NaN 等,这是我试图用来训练网络的代码:
def Network_ii(IN, OUT, TIME_PERIOD, EPOCHS, BATCH_SIZE, LTSM_SHAPE):
length = len(OUT)
train_x = IN[:int(0.9 * length)]
validation_x = IN[int(0.9 * length):]
train_y = OUT[:int(0.9 * length)]
validation_y = OUT[int(0.9 * length):]
# Define Network & callback:
train_x = train_x.reshape(train_x.shape[0],3, 5)
validation_x = validation_x.reshape(validation_x.shape[0],3, 5)
model = Sequential()
model.add(LSTM(units=128, return_sequences= True, input_shape=(train_x.shape[1],3)))
model.add(LSTM(units=128))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')
train_y = np.asarray(train_y)
validation_y = np.asarray(validation_y)
history = model.fit(train_x, train_y, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=(validation_x, validation_y))
# Score model
score = model.evaluate(validation_x, validation_y, verbose=0)
print('Test loss:', score)
# Save model
model.save(f"models/new_model")
I am attempting to roughly follow the steps outlined here- https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/我试图粗略地遵循这里概述的步骤 - https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/
However, no matter what adjustments I have made in terms of changing the number of dimensions used to train the network or the length of the time period I cannot get the output of the model to give predictions that are not either 1 or 0. This is even though the target data, in the array 'OUT' is made up of data continuous on [0,1].然而,无论我在改变用于训练网络的维数或时间段长度方面做出什么调整,我都无法得到模型的输出来给出不是 1 或 0 的预测。这是即使目标数据,在数组 'OUT' 中由 [0,1] 上连续的数据组成。
I think there may be something wrong with how I am setting up the .Sequential() function, but I cannot see what to adjust.我认为我设置 .Sequential() 函数的方式可能有问题,但我看不出要调整什么。 I am relatively new to this so any help would be greatly appreciated.我对此比较陌生,因此将不胜感激任何帮助。
You are probably using a prediction function that is not the standard.您可能使用的预测函数不是标准的。 Maybe you are using predict_classes
?也许您正在使用predict_classes
?
The one that is well documented and the standard is model.predict
.有据可查的标准是model.predict
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.