0
定義我的神經網絡和訓練我的模型後:TFLearn時間序列預測預測
net = tflearn.input_data(shape=[None, 1, 1])
tnorm = tflearn.initializations.uniform(minval=-1.0, maxval=1.0)
net = tflearn.lstm(net, timesteps, dropout=0.8)
net = tflearn.fully_connected(net, 1, activation='linear', weights_init=tnorm)
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
loss='mean_square', metric='R2')
# Define model
model = tflearn.DNN(net, clip_gradients=0.)
model.fit(X, y, n_epoch=nb_epoch, batch_size=batch_size, shuffle=False, show_metric=True)
score = model.evaluate(X, y, batch_size=128)
model.save('ModeSpot.tflearn')
現在我有一個問題,大部分的教程,我發現這樣做的時間序列預測使用的測試設置預測(他們將測試集設置爲.predict())。問題是,實際上我們不知道這一點,因爲這是我們想要預測的。
現在我使用:
def forecast_lstm(model, X):
X = X.reshape(len(X), 1, 1)
yhat = model.predict(X)
return yhat[0, 0]
# split data into train and test-sets
train, test = supervised_values[0:-10000], supervised_values[-10000:]
# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)
# Build neural network
net = tflearn.input_data(shape=[None, 1, 1])
tnorm = tflearn.initializations.uniform(minval=-1.0, maxval=1.0)
net = tflearn.lstm(net, 1, dropout=0.3)
net = tflearn.fully_connected(net, 1, activation='linear', weights_init=tnorm)
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
loss='mean_square', metric='R2')
lstm_model = tflearn.DNN(net, clip_gradients=0.)
lstm_model.load('ModeSpot.tflearn')
# forecast the entire training dataset to build up state for forecasting
train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
lstm_model.predict(train_reshaped)
# walk-forward validation on the test data
predictions = list()
error_scores = list()
for i in range(len(test_scaled)):
# make one-step forecast
X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
yhat = forecast_lstm(lstm_model, X)
# invert scaling
yhat2 = invert_scale(scaler, X, yhat)
# # invert differencing
yhat3 = inverse_difference(raw_values, yhat2, len(test_scaled) + 1 - i)
# store forecast
predictions.append(yhat3)
但只爲我的測試集工作。我該如何預測下一個x值? 我想我已經看到某處預測T值的地方,我將不得不使用T-1的值作爲預測值(然後T對於T + 1,並且像這樣,直到達到我想要的預測數)。這是一個好方法嗎?
我試着這樣做:
def forecast_lstm2(model, X):
X = X.reshape(-1, 1, 1)
yhat = model.predict(X)
return yhat[0, 0]
test = list()
X, y = train_scaled[0, 0:-1], train_scaled[0, -1]
test.append(X)
for i in range(len(test_scaled)):
# make one-step forecast
yhat = forecast_lstm2(lstm_model, test[i])
test.append(yhat)
# invert scaling
yhat2 = invert_scale(scaler, test[i+1], yhat)
# # invert differencing
yhat3 = inverse_difference(raw_values, yhat2, len(test) + 1 - i)
# store forecast
predictions.append(yhat3)
但它並沒有收到預期的效果(一些預測後,總是給出相同的結果)。
感謝您的關注和時間。