欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyTorch搭建LSTM實(shí)現(xiàn)多變量多步長(zhǎng)時(shí)序負(fù)荷預(yù)測(cè)

 更新時(shí)間:2022年05月11日 09:55:06   作者:Cyril_KI  
這篇文章主要為大家介紹了PyTorch搭建LSTM實(shí)現(xiàn)多變量多步長(zhǎng)時(shí)序負(fù)荷預(yù)測(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

I. 前言

在前面的兩篇文章PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列預(yù)測(cè)(負(fù)荷預(yù)測(cè))和PyTorch搭建LSTM實(shí)現(xiàn)多變量時(shí)間序列預(yù)測(cè)(負(fù)荷預(yù)測(cè))中,我們利用LSTM分別實(shí)現(xiàn)了單變量單步長(zhǎng)時(shí)間序列預(yù)測(cè)和多變量單步長(zhǎng)時(shí)間序列預(yù)測(cè)。

本篇文章主要考慮用PyTorch搭建LSTM實(shí)現(xiàn)多變量多步長(zhǎng)時(shí)間序列預(yù)測(cè)。

系列文章:

PyTorch搭建雙向LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè)

PyTorch搭建LSTM實(shí)現(xiàn)多變量時(shí)序負(fù)荷預(yù)測(cè)

PyTorch深度學(xué)習(xí)LSTM從input輸入到Linear輸出

PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè)

II. 數(shù)據(jù)處理

數(shù)據(jù)集為某個(gè)地區(qū)某段時(shí)間內(nèi)的電力負(fù)荷數(shù)據(jù),除了負(fù)荷以外,還包括溫度、濕度等信息。

本文中,我們根據(jù)前24個(gè)時(shí)刻的負(fù)荷以及該時(shí)刻的環(huán)境變量來預(yù)測(cè)接下來4個(gè)時(shí)刻的負(fù)荷(步長(zhǎng)可調(diào))。

def load_data(file_name):
    global MAX, MIN
    df = pd.read_csv(os.path.dirname(os.getcwd()) + '/data/new_data/' + file_name, encoding='gbk')
    columns = df.columns
    df.fillna(df.mean(), inplace=True)
    MAX = np.max(df[columns[1]])
    MIN = np.min(df[columns[1]])
    df[columns[1]] = (df[columns[1]] - MIN) / (MAX - MIN)
    return df
class MyDataset(Dataset):
    def __init__(self, data):
        self.data = data
    def __getitem__(self, item):
        return self.data[item]
    def __len__(self):
        return len(self.data)
def nn_seq(file_name, B, num):
    print('data processing...')
    data = load_data(file_name)
    load = data[data.columns[1]]
    load = load.tolist()
    data = data.values.tolist()
    seq = []
    for i in range(0, len(data) - 24 - num, num):
        train_seq = []
        train_label = []
        for j in range(i, i + 24):
            x = [load[j]]
            for c in range(2, 8):
                x.append(data[j][c])
            train_seq.append(x)
        for j in range(i + 24, i + 24 + num):
            train_label.append(load[j])
        train_seq = torch.FloatTensor(train_seq)
        train_label = torch.FloatTensor(train_label).view(-1)
        seq.append((train_seq, train_label))
    # print(seq[-1])
    Dtr = seq[0:int(len(seq) * 0.7)]
    Dte = seq[int(len(seq) * 0.7):len(seq)]
    train_len = int(len(Dtr) / B) * B
    test_len = int(len(Dte) / B) * B
    Dtr, Dte = Dtr[:train_len], Dte[:test_len]
    train = MyDataset(Dtr)
    test = MyDataset(Dte)
    Dtr = DataLoader(dataset=train, batch_size=B, shuffle=False, num_workers=0)
    Dte = DataLoader(dataset=test, batch_size=B, shuffle=False, num_workers=0)
    return Dtr, Dte

其中num表示需要預(yù)測(cè)的步長(zhǎng),如num=4表示預(yù)測(cè)接下來4個(gè)時(shí)刻的負(fù)荷。

任意輸出其中一條數(shù)據(jù):

(tensor([[0.5830, 1.0000, 0.9091, 0.6957, 0.8333, 0.4884, 0.5122],
        [0.6215, 1.0000, 0.9091, 0.7391, 0.8333, 0.4884, 0.5122],
        [0.5954, 1.0000, 0.9091, 0.7826, 0.8333, 0.4884, 0.5122],
        [0.5391, 1.0000, 0.9091, 0.8261, 0.8333, 0.4884, 0.5122],
        [0.5351, 1.0000, 0.9091, 0.8696, 0.8333, 0.4884, 0.5122],
        [0.5169, 1.0000, 0.9091, 0.9130, 0.8333, 0.4884, 0.5122],
        [0.4694, 1.0000, 0.9091, 0.9565, 0.8333, 0.4884, 0.5122],
        [0.4489, 1.0000, 0.9091, 1.0000, 0.8333, 0.4884, 0.5122],
        [0.4885, 1.0000, 0.9091, 0.0000, 1.0000, 0.3256, 0.3902],
        [0.4612, 1.0000, 0.9091, 0.0435, 1.0000, 0.3256, 0.3902],
        [0.4229, 1.0000, 0.9091, 0.0870, 1.0000, 0.3256, 0.3902],
        [0.4173, 1.0000, 0.9091, 0.1304, 1.0000, 0.3256, 0.3902],
        [0.4503, 1.0000, 0.9091, 0.1739, 1.0000, 0.3256, 0.3902],
        [0.4502, 1.0000, 0.9091, 0.2174, 1.0000, 0.3256, 0.3902],
        [0.5426, 1.0000, 0.9091, 0.2609, 1.0000, 0.3256, 0.3902],
        [0.5579, 1.0000, 0.9091, 0.3043, 1.0000, 0.3256, 0.3902],
        [0.6035, 1.0000, 0.9091, 0.3478, 1.0000, 0.3256, 0.3902],
        [0.6540, 1.0000, 0.9091, 0.3913, 1.0000, 0.3256, 0.3902],
        [0.6181, 1.0000, 0.9091, 0.4348, 1.0000, 0.3256, 0.3902],
        [0.6334, 1.0000, 0.9091, 0.4783, 1.0000, 0.3256, 0.3902],
        [0.6297, 1.0000, 0.9091, 0.5217, 1.0000, 0.3256, 0.3902],
        [0.5610, 1.0000, 0.9091, 0.5652, 1.0000, 0.3256, 0.3902],
        [0.5957, 1.0000, 0.9091, 0.6087, 1.0000, 0.3256, 0.3902],
        [0.6427, 1.0000, 0.9091, 0.6522, 1.0000, 0.3256, 0.3902]]), tensor([0.6360, 0.6996, 0.6889, 0.6434]))

數(shù)據(jù)格式為(X, Y)。其中X一共24行,表示前24個(gè)時(shí)刻的負(fù)荷值和該時(shí)刻的環(huán)境變量。Y一共四個(gè)值,表示需要預(yù)測(cè)的四個(gè)負(fù)荷值。需要注意的是,此時(shí)input_size=7,output_size=4。

III. LSTM模型

這里采用了深入理解PyTorch中LSTM的輸入和輸出(從input輸入到Linear輸出)中的模型:

class LSTM(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size):
        super().__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.output_size = output_size
        self.num_directions = 1
        self.batch_size = batch_size
        self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
        self.linear = nn.Linear(self.hidden_size, self.output_size)
    def forward(self, input_seq):
        h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
        c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
        # print(input_seq.size())
        seq_len = input_seq.shape[1]
        # input(batch_size, seq_len, input_size)
        input_seq = input_seq.view(self.batch_size, seq_len, self.input_size)
        # output(batch_size, seq_len, num_directions * hidden_size)
        output, _ = self.lstm(input_seq, (h_0, c_0))
        # print('output.size=', output.size())
        # print(self.batch_size * seq_len, self.hidden_size)
        output = output.contiguous().view(self.batch_size * seq_len, self.hidden_size)  # (5 * 30, 64)
        pred = self.linear(output)  # pred()
        # print('pred=', pred.shape)
        pred = pred.view(self.batch_size, seq_len, -1)
        pred = pred[:, -1, :]
        return pred

IV. 訓(xùn)練和預(yù)測(cè)

訓(xùn)練和預(yù)測(cè)代碼和前幾篇都差不多,只是需要注意input_size和output_size的大小。

訓(xùn)練了100輪,預(yù)測(cè)接下來四個(gè)時(shí)刻的負(fù)荷值,MAPE為7.53%:

V. 源碼及數(shù)據(jù)

源碼及數(shù)據(jù)我放在了GitHub上,LSTM-Load-Forecasting

以上就是PyTorch搭建LSTM實(shí)現(xiàn)多變量多步長(zhǎng)時(shí)序負(fù)荷預(yù)測(cè)的詳細(xì)內(nèi)容,更多關(guān)于LSTM多變量多步長(zhǎng)時(shí)序負(fù)荷預(yù)測(cè)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python數(shù)據(jù)結(jié)構(gòu)與算法中的隊(duì)列詳解(2)

    Python數(shù)據(jù)結(jié)構(gòu)與算法中的隊(duì)列詳解(2)

    這篇文章主要為大家詳細(xì)介紹了Python中的隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • python中readline判斷文件讀取結(jié)束的方法

    python中readline判斷文件讀取結(jié)束的方法

    這篇文章主要介紹了python中readline判斷文件讀取結(jié)束的方法,實(shí)例形式詳細(xì)分析了Python中readline的用法,需要的朋友可以參考下
    2014-11-11
  • Python利用Selenium實(shí)現(xiàn)彈出框的處理

    Python利用Selenium實(shí)現(xiàn)彈出框的處理

    經(jīng)常出現(xiàn)在網(wǎng)頁(yè)上的基于JavaScript實(shí)現(xiàn)的彈出框有三種,分別是?alert、confirm、prompt?。本文主要是學(xué)習(xí)如何利用selenium處理這三種彈出框,感興趣的可以了解一下
    2022-06-06
  • Python中Django的URL反向解析

    Python中Django的URL反向解析

    這篇文章主要介紹了Python中Django的URL反向解析,url反向解析是指在視圖或模板中,用path定義的名稱來動(dòng)態(tài)查找或計(jì)算出相應(yīng)的路由,本文提供了部分實(shí)現(xiàn)代碼與解決思路,需要的朋友可以參考下
    2023-09-09
  • Python對(duì)象循環(huán)引用垃圾回收算法詳情

    Python對(duì)象循環(huán)引用垃圾回收算法詳情

    這篇文章主要介紹了Python對(duì)象循環(huán)引用垃圾回收算法詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-09-09
  • 查看django版本的方法分享

    查看django版本的方法分享

    今天小編就為大家分享一篇查看django版本的方法分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python,pycharm的環(huán)境變量設(shè)置方式

    python,pycharm的環(huán)境變量設(shè)置方式

    這篇文章主要介紹了python,pycharm的環(huán)境變量設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python的等深分箱實(shí)例

    python的等深分箱實(shí)例

    今天小編就為大家分享一篇python的等深分箱實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 使用matplotlib繪制并排柱狀圖的實(shí)戰(zhàn)案例

    使用matplotlib繪制并排柱狀圖的實(shí)戰(zhàn)案例

    堆積柱狀圖有堆積柱狀圖的好處,比如說我們可以很方便地看到多分類總和的趨勢(shì),下面這篇文章主要給大家介紹了關(guān)于使用matplotlib繪制并排柱狀圖的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • 使用PyInstaller庫(kù)把Python程序打包成exe

    使用PyInstaller庫(kù)把Python程序打包成exe

    這篇文章介紹了使用PyInstaller庫(kù)把Python程序打包成exe的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論