溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PyTorch中LSTM的輸入和輸出實例分析

發(fā)布時間:2022-07-27 09:31:54 來源:億速云 閱讀:326 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“PyTorch中LSTM的輸入和輸出實例分析”,在日常操作中,相信很多人在PyTorch中LSTM的輸入和輸出實例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PyTorch中LSTM的輸入和輸出實例分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

LSTM參數(shù)

官方文檔給出的解釋為:

PyTorch中LSTM的輸入和輸出實例分析

總共有七個參數(shù),其中只有前三個是必須的。由于大家普遍使用PyTorch的DataLoader來形成批量數(shù)據(jù),因此batch_first也比較重要。LSTM的兩個常見的應(yīng)用場景為文本處理和時序預(yù)測,因此下面對每個參數(shù)我都會從這兩個方面來進行具體解釋。

  • input_size:在文本處理中,由于一個單詞沒法參與運算,因此我們得通過Word2Vec來對單詞進行嵌入表示,將每一個單詞表示成一個向量,此時input_size=embedding_size。比如每個句子中有五個單詞,每個單詞用一個100維向量來表示,那么這里input_size=100;在時間序列預(yù)測中,比如需要預(yù)測負荷,每一個負荷都是一個單獨的值,都可以直接參與運算,因此并不需要將每一個負荷表示成一個向量,此時input_size=1。 但如果我們使用多變量進行預(yù)測,比如我們利用前24小時每一時刻的[負荷、風(fēng)速、溫度、壓強、濕度、天氣、節(jié)假日信息]來預(yù)測下一時刻的負荷,那么此時input_size=7。

  • hidden_size:隱藏層節(jié)點個數(shù)??梢噪S意設(shè)置。

  • num_layers:層數(shù)。nn.LSTMCell與nn.LSTM相比,num_layers默認為1。

  • batch_first:默認為False,意義見后文。

Inputs

關(guān)于LSTM的輸入,官方文檔給出的定義為:

PyTorch中LSTM的輸入和輸出實例分析

可以看到,輸入由兩部分組成:input、(初始的隱狀態(tài)h_0,初始的單元狀態(tài)c_0)

其中input:

input(seq_len, batch_size, input_size)
  • seq_len:在文本處理中,如果一個句子有7個單詞,則seq_len=7;在時間序列預(yù)測中,假設(shè)我們用前24個小時的負荷來預(yù)測下一時刻負荷,則seq_len=24。

  • batch_size:一次性輸入LSTM中的樣本個數(shù)。在文本處理中,可以一次性輸入很多個句子;在時間序列預(yù)測中,也可以一次性輸入很多條數(shù)據(jù)。

  • input_size

(h_0, c_0):

h_0(num_directions * num_layers, batch_size, hidden_size)
c_0(num_directions * num_layers, batch_size, hidden_size)

h_0和c_0的shape一致。

  • num_directions:如果是雙向LSTM,則num_directions=2;否則num_directions=1。num_layers:

  • batch_size:

  • hidden_size:

 Outputs

關(guān)于LSTM的輸出,官方文檔給出的定義為:

PyTorch中LSTM的輸入和輸出實例分析

可以看到,輸出也由兩部分組成:otput、(隱狀態(tài)h_n,單元狀態(tài)c_n)

其中output的shape為:

output(seq_len, batch_size, num_directions * hidden_size)

h_n和c_n的shape保持不變,參數(shù)解釋見前文。

batch_first

如果在初始化LSTM時令batch_first=True,那么input和output的shape將由:

input(seq_len, batch_size, input_size)
output(seq_len, batch_size, num_directions * hidden_size)

變?yōu)椋?/strong>

input(batch_size, seq_len, input_size)
output(batch_size, seq_len, num_directions * hidden_size)

即batch_size提前。

案例

簡單搭建一個LSTM如下所示:

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 # 單向LSTM
        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):
        batch_size, seq_len = input_seq[0], input_seq[1]
        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)
        # output(batch_size, seq_len, num_directions * hidden_size)
        output, _ = self.lstm(input_seq, (h_0, c_0)) # output(5, 30, 64)
        pred = self.linear(output)  # (5, 30, 1)
        pred = pred[:, -1, :]  # (5, 1)
        return pred

其中定義模型的代碼為:

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)

我們加上具體的數(shù)字:

self.lstm = nn.LSTM(self.input_size=1, self.hidden_size=64, self.num_layers=5, batch_first=True)
self.linear = nn.Linear(self.hidden_size=64, self.output_size=1)

再看前向傳播:

def forward(self, input_seq):
    batch_size, seq_len = input_seq[0], input_seq[1]
    h_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    c_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
    # input(batch_size, seq_len, input_size)
    # output(batch_size, seq_len, num_directions * hidden_size)
    output, _ = self.lstm(input_seq, (h_0, c_0))  # output(5, 30, 64)
    pred = self.linear(output) # (5, 30, 1)
    pred = pred[:, -1, :]  # (5, 1)
    return pred

假設(shè)用前30個預(yù)測下一個,則seq_len=30,batch_size=5,由于設(shè)置了batch_first=True,因此,輸入到LSTM中的input的shape應(yīng)該為:

input(batch_size, seq_len, input_size) = input(5, 30, 1)

經(jīng)過DataLoader處理后的input_seq為:

input_seq(batch_size, seq_len, input_size) = input_seq(5, 30, 1)

然后將input_seq送入LSTM:

output, _ = self.lstm(input_seq, (h_0, c_0))  # output(5, 30, 64)

根據(jù)前文,output的shape為:

output(batch_size, seq_len, num_directions * hidden_size) = output(5, 30, 64)

全連接層的定義為:

self.linear = nn.Linear(self.hidden_size=64, self.output_size=1)

然后將output送入全連接層:

pred = self.linear(output)  # pred(5, 30, 1)

得到的預(yù)測值shape為(5, 30, 1),由于輸出是輸入右移,我們只需要取pred第二維度(time)中的最后一個數(shù)據(jù):

pred = pred[:, -1, :]  # (5, 1)

這樣,我們就得到了預(yù)測值,然后與label求loss,然后再反向更新參數(shù)即可。

到此,關(guān)于“PyTorch中LSTM的輸入和輸出實例分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI