700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Encoder-Decoder LSTM模型对家庭用电进行多步时间序列预测(多变量输入)

Encoder-Decoder LSTM模型对家庭用电进行多步时间序列预测(多变量输入)

时间:2021-08-08 13:40:57

相关推荐

Encoder-Decoder LSTM模型对家庭用电进行多步时间序列预测(多变量输入)

在本节中,我们将更新上一节中开发的编码器-解码器LSTM,使用8个时间序列变量中的每一个来预测下一个标准周的每日总功耗。我们将通过将每个一维时间序列作为单独的输入序列提供给模型来实现这一点。LSTM将依次创建每个输入序列的内部表示,这些输入序列将由解码器一起解释。使用多元输入有助于解决这样的问题,即输出序列是来自多个不同特征的先前时间步长的某个函数,而不只是(或包括)预测的特征。目前还不清楚在电力消耗问题上是否存在这种情况,但我们可以探索它。首先,我们必须更新培训数据的准备工作,使其包含所有八个特性,而不仅仅是每天消耗的总能量。

它需要一行更改:

X.append(data[in_start:in_end, :])

完成此更改的to_supervised()函数如下所示。

# convert history into inputs and outputsdef to_supervised(train, n_input, n_out=7):# flatten datadata = train.reshape((train.shape[0]*train.shape[1], train.shape[2]))X, y = list(), list()in_start = 0# step over the entire history one time step at a timefor _ in range(len(data)):# define the end of the input sequencein_end = in_start + n_inputout_end = in_end + n_out# ensure we have enough data for this instanceif out_end < len(data):X.append(data[in_start:in_end, :])y.append(data[in_end:out_end, 0])# move along one time stepin_start += 1return array(X), array(y)

我们还必须更新使用fit模型进行预测的函数,以使用前面时间步骤中的所有8个特性。还有一个小变化:

# retrieve last observations for input datainput_x = data[-n_input:, :]# reshape into [1, n_input, n]input_x = input_x.reshape((1, input_x.shape[0], input_x.shape[1]))

修改后的complete forecast()函数如下:

# make a forecastdef forecast(model, history, n_input):# flatten datadata = array(history)data = data.reshape((data.shape[0]*data.shape[1], data.shape[2]))# retrieve last observations for input datainput_x = data[-n_input:, :]# reshape into [1, n_input, n]input_x = input_x.reshape((1, input_x.shape[0], input_x.shape[1]))# forecast the next weekyhat = model.predict(input_x, verbose=0)# we only want the vector forecastyhat = yhat[0]return yhat

直接使用相同的模型体系结构和配置,尽管我们将把训练周期的数量从20个增加到50个,因为输入数据量增加了8倍。

完整的示例如下所示。

# multivariate multi-step encoder-decoder lstmfrom math import sqrtfrom numpy import splitfrom numpy import arrayfrom pandas import read_csvfrom sklearn.metrics import mean_squared_errorfrom matplotlib import pyplotfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import Flattenfrom keras.layers import LSTMfrom keras.layers import RepeatVectorfrom keras.layers import TimeDistributed# split a univariate dataset into train/test setsdef split_dataset(data):# split into standard weekstrain, test = data[1:-328], data[-328:-6]# restructure into windows of weekly datatrain = array(split(train, len(train)/7))test = array(split(test, len(test)/7))return train, test# evaluate one or more weekly forecasts against expected valuesdef evaluate_forecasts(actual, predicted):scores = list()# calculate an RMSE score for each dayfor i in range(actual.shape[1]):# calculate msemse = mean_squared_error(actual[:, i], predicted[:, i])# calculate rmsermse = sqrt(mse)# storescores.append(rmse)# calculate overall RMSEs = 0for row in range(actual.shape[0]):for col in range(actual.shape[1]):s += (actual[row, col] - predicted[row, col])**2score = sqrt(s / (actual.shape[0] * actual.shape[1]))return score, scores# summarize scoresdef summarize_scores(name, score, scores):s_scores = ', '.join(['%.1f' % s for s in scores])print('%s: [%.3f] %s' % (name, score, s_scores))# convert history into inputs and outputsdef to_supervised(train, n_input, n_out=7):# flatten datadata = train.reshape((train.shape[0]*train.shape[1], train.shape[2]))X, y = list(), list()in_start = 0# step over the entire history one time step at a timefor _ in range(len(data)):# define the end of the input sequencein_end = in_start + n_inputout_end = in_end + n_out# ensure we have enough data for this instanceif out_end < len(data):X.append(data[in_start:in_end, :])y.append(data[in_end:out_end, 0])# move along one time stepin_start += 1return array(X), array(y)# train the modeldef build_model(train, n_input):# prepare datatrain_x, train_y = to_supervised(train, n_input)# define parametersverbose, epochs, batch_size = 0, 50, 16n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]# reshape output into [samples, timesteps, features]train_y = train_y.reshape((train_y.shape[0], train_y.shape[1], 1))# define modelmodel = Sequential()model.add(LSTM(200, activation='relu', input_shape=(n_timesteps, n_features)))model.add(RepeatVector(n_outputs))model.add(LSTM(200, activation='relu', return_sequences=True))model.add(TimeDistributed(Dense(100, activation='relu')))model.add(TimeDistributed(Dense(1)))pile(loss='mse', optimizer='adam')# fit networkmodel.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)return model# make a forecastdef forecast(model, history, n_input):# flatten datadata = array(history)data = data.reshape((data.shape[0]*data.shape[1], data.shape[2]))# retrieve last observations for input datainput_x = data[-n_input:, :]# reshape into [1, n_input, n]input_x = input_x.reshape((1, input_x.shape[0], input_x.shape[1]))# forecast the next weekyhat = model.predict(input_x, verbose=0)# we only want the vector forecastyhat = yhat[0]return yhat# evaluate a single modeldef evaluate_model(train, test, n_input):# fit modelmodel = build_model(train, n_input)# history is a list of weekly datahistory = [x for x in train]# walk-forward validation over each weekpredictions = list()for i in range(len(test)):# predict the weekyhat_sequence = forecast(model, history, n_input)# store the predictionspredictions.append(yhat_sequence)# get real observation and add to history for predicting the next weekhistory.append(test[i, :])# evaluate predictions days for each weekpredictions = array(predictions)score, scores = evaluate_forecasts(test[:, :, 0], predictions)return score, scores# load the new filedataset = read_csv('household_power_consumption_days.csv', header=0, infer_datetime_format=True, parse_dates=['datetime'], index_col=['datetime'])# split into train and testtrain, test = split_dataset(dataset.values)# evaluate model and get scoresn_input = 14score, scores = evaluate_model(train, test, n_input)# summarize scoressummarize_scores('lstm', score, scores)# plot scoresdays = ['sun', 'mon', 'tue', 'wed', 'thr', 'fri', 'sat']pyplot.plot(days, scores, marker='o', label='lstm')pyplot.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。