LSTM多元时间序列的优化

时间:2019-07-30 00:26:51

标签: python optimization time-series lstm prediction

我正在研究用于预测多元时间序列的LSTM模型。我尝试使用几个参数(例如时期,批处理大小等)优化模型,但是我无法在实际数据和预测数据之间获得可接受的匹配。知道如何优化模型吗?

我的代码如下所示(不包括原始数据):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.model_selection import train_test_split

def create_timeblock(X, Y, look_back=5):
    dataX, dataY = [], []
    for i in range(len(X) - look_back):
        dataX.append(X[i:(i + look_back), :])
        dataY.append(Y[i + look_back, :])
    print('# of data: ', len(dataY))
    print(len(X))
    return np.array(dataX), np.array(dataY)


def plot_pred_press(t, p_actual, p_pred, test_fraction):
    """This function plots actual and predicted bottom hole pressure"""
    # Plotting pwf v time
    plt.figure()
    plt.plot(t, p_actual, 'k-', linewidth=3, label='Actual pressure')

    # Training
    train_end = int((1 - test_fraction) * p_pred.shape[0])
    plt.plot(t[0:train_end], p_pred[0:train_end],
                 'r-', markeredgecolor='red', label='Training Set', linewidth=3)

    # Test
    plt.plot(t[train_end:], p_pred[train_end:],
                 'g-', markeredgecolor='green', label='Test Set', linewidth=3)    

    plt.xlabel("Time (hours)", fontsize=14)
    plt.ylabel("Pressue (psi)", fontsize=14)
    plt.xlim(0, max(t))
    plt.ylim(0, max(max(p_actual), max(p_pred)) + 10)
    plt.grid(True)

def lstm_time_steps():

    data = pd.read_csv('twhophasebigtime.csv')

    np.random.seed(7)
    t = data.loc[:, ['Time']] # Time in simulation: DAY

    #data_set = data.values.astype('float32')
    X_raw = data.loc[:, ['Time', 'OilQ', 'WaterQ']]
    Y_raw = data.loc[:, ['Pressure']]

    # Split train-dev and test sets
    test_fraction = 0.15
    x_train_raw, x_test_raw, y_train_raw, y_test_raw = train_test_split(X_raw,
                                                  Y_raw,
                                                  test_size=test_fraction,
                                                  shuffle=False)

    scaler_x = MinMaxScaler(feature_range=(0, 1))
    x_train_norm = scaler_x.fit_transform(x_train_raw.as_matrix())
    x_test_norm = scaler_x.transform(x_test_raw.as_matrix())

    scaler_y = MinMaxScaler(feature_range=(0, 1))
    y_train_norm = scaler_y.fit_transform(y_train_raw.as_matrix())
    y_test_norm = scaler_y.transform(y_test_raw.as_matrix())

    # Create time blocks
    x_train, y_train = create_timeblock(x_train_norm, y_train_norm, look_back=5)
    x_test, y_test = create_timeblock(x_test_norm, y_test_norm, look_back=5)

    # create the LSTM model
    look_back = 5
    model = Sequential()
    model.add(LSTM(units=4,
                   activation='tanh',
                   recurrent_activation='hard_sigmoid',
                   use_bias=True,
                   kernel_initializer='ones',
                   recurrent_initializer='zeros',
                   bias_initializer='zeros',
                   unit_forget_bias=True,
                   dropout=0.0,
                   recurrent_dropout=0.0,
                   input_shape=(look_back, 3)))
    model.add(Dense(units=1,
                    activation='linear',
                    use_bias=True,
                    kernel_initializer='glorot_uniform',
                    bias_initializer='zeros'))

    model.summary()
    for layer in model.layers:
        g = layer.get_config()
        h = layer.get_weights()
        print(g)
        print(h)

    model.compile(loss='mean_squared_error', optimizer='Adam', metrics=['mae','accuracy'])
    hist = model.fit(x_train, y_train,
                     epochs=50,
                     batch_size=15,
                     validation_split=0.0,
                     verbose=2,
                     shuffle=False)

    # Training
    y_pred_train_pre = model.predict(x_train)
    y_pred_train = scaler_y.inverse_transform(y_pred_train_pre)
    y_train_act = scaler_y.inverse_transform(y_train)

    # Test
    y_pred_test_pre = model.predict(x_test)
    y_pred_test = scaler_y.inverse_transform(y_pred_test_pre)
    y_test_act = scaler_y.inverse_transform(y_test)

    # Train-Dev-Test
    y_act = np.concatenate([y_train_act, y_test_act])
    y_pred = np.concatenate([y_pred_train, y_pred_test])

    # root mean square error
    train_score = np.sqrt(mean_squared_error(y_train_act, y_pred_train))
    test_score = np.sqrt(mean_squared_error(y_test_act, y_pred_test))
    print(np.shape(y_pred_train))
    print(np.shape(y_pred_test))
    print("Training set RMSE: %.2f" % train_score)
    print("Testing set RMSE: %.2f" % test_score)

    plot_pred_press(t[-len(y_act):].as_matrix(), y_act, y_pred, test_fraction=test_fraction)

    plt.show()

if __name__=="__main__":

    # plot_pressure_data()
    # lstm_regression()
    lstm_time_steps()

0 个答案:

没有答案