在获得太多帮助后,我最后一次来这里是我找不到解决方案的最后一个问题。

在我之前的question之后,一个用户指出了一个事实,即我对时间序列预测的不良结果可能是由于我的体系结构未收敛。

在查看并尝试了一些我在其他问题(设置权重,较低的学习率,更改优化器/激活)上找到的解决方法之后,我似乎无法获得更好的结果,始终将精度设为0(或0.0003),还不够好)。

我的代码:

import numpy
import numpy as np
import tflearn
from pandas import DataFrame
from pandas import Series
from pandas import concat
from pandas import read_csv
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from math import sqrt

import datetime

# Preprocessing function
from tflearn import Accuracy, Momentum


def preprocess(data):
    return np.array(data, dtype=np.int32)

def parser(x):
    return datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')


# frame a sequence as a supervised learning problem
def timeseries_to_supervised(data, lag=1):
    df = DataFrame(data)
    columns = [df.shift(i) for i in range(1, lag + 1)]
    columns.append(df)
    df = concat(columns, axis=1)
    df.fillna(0, inplace=True)
    return df


def difference(dataset, interval=1):
    diff = list()
    for i in range(interval, len(dataset)):
        value = dataset[i] - dataset[i - interval]
        diff.append(value)
    return Series(diff)


# invert differenced value
def inverse_difference(history, yhat, interval=1):
    return yhat + history[-interval]


# scale train and test data to [-1, 1]
def scale(train, test):
    # fit scaler
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler = scaler.fit(train)
    # transform train
    train = train.reshape(train.shape[0], train.shape[1])
    train_scaled = scaler.transform(train)
    # transform test
    test = test.reshape(test.shape[0], test.shape[1])
    test_scaled = scaler.transform(test)
    return scaler, train_scaled, test_scaled


# inverse scaling for a forecasted value
def invert_scale(scaler, X, value):
    new_row = [x for x in X] + [value]
    array = numpy.array(new_row)
    array = array.reshape(1, len(array))
    inverted = scaler.inverse_transform(array)
    return inverted[0, -1]


def fit_lstm(train, batch_size, nb_epoch, neurons):
    X, y = train[0:-1], train[:, -1]
    X = X[:, 0].reshape(len(X), 1, 1)
    y = y.reshape(len(y), 1)
    print (X.shape)
    print (y.shape)
    # Build neural network
    net = tflearn.input_data(shape=[None, 1, 1])
    tnorm = tflearn.initializations.uniform(minval=-1.0, maxval=1.0)
    net = tflearn.dropout(net, 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')
    # Define model
    model = tflearn.DNN(net, tensorboard_verbose=3, best_val_accuracy=0.6)
    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)
    print (score)
    return model


# make a one-step forecast
def forecast_lstm(model, X):
    X = X.reshape(len(X), 1, 1)
    yhat = model.predict(X)
    return yhat[0, 0]

# Load CSV file, indicate that the first column represents labels
data = read_csv('nowcastScaled.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)


# transform data to be stationary
raw_values = data.values
diff_values = difference(raw_values, 1)

# transform data to be supervised learning
supervised = timeseries_to_supervised(diff_values, 1)
supervised_values = supervised.values

# split data into train and test-sets
train, test = supervised_values[0:10000], supervised_values[10000:10100]

# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)
repeats = 1
for r in range(repeats):
    # fit the model
    lstm_model = fit_lstm(train_scaled, 128, 6, 1)

    # forecast the entire training dataset to build up state for forecasting
    train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
    print (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
        yhat = invert_scale(scaler, X, yhat)
        # # invert differencing
        yhat = inverse_difference(raw_values, yhat, len(test_scaled) + 1 - i)
        # store forecast
        predictions.append(yhat)
    rmse = sqrt(mean_squared_error(raw_values[10000:10100], predictions))
    print('%d) Test RMSE: %.3f' % (1, rmse))
    error_scores.append(rmse)
    print predictions
    print raw_values[10000:10100]


这是我从运行它得到的结果(提高纪元似乎并没有使它变得更好):

Training Step: 472  | total loss: 0.00486 | time: 0.421s
| Adam | epoch: 006 | loss: 0.00486 - binary_acc: 0.0000 -- iter: 9856/9999
Training Step: 473  | total loss: 0.00453 | time: 0.427s
| Adam | epoch: 006 | loss: 0.00453 - binary_acc: 0.0000 -- iter: 9984/9999
Training Step: 474  | total loss: 0.00423 | time: 0.430s
| Adam | epoch: 006 | loss: 0.00423 - binary_acc: 0.0000 -- iter: 9999/9999


我试图降低/提高大多数设置,但是什么也没有。

这是data I'm using(单变量时间序列)的摘录,在训练中使用或多或少的数据也无济于事。

(注:我的代码主要来自this tutorial,由于我想尝试使用Tflearn,因此不得不对其进行一些更改)

最佳答案

您不能为回归问题定义accuracy。您只需跟踪预测的MSE和实际的MSE。您的训练损失似乎很低,因此,如果预测值不接近,则您的换算反比不正确或过度拟合。

08-19 21:19