簡體   English   中英

PyTorch:nn.LSTM 對同一批中的相同輸入輸出不同的結果

[英]PyTorch: nn.LSTM output different results for the same inputs in the same batch

我嘗試使用torch.nn.LSTM實現兩層雙向 LSTM。

我做了一個玩具示例:一批 3 個張量,它們完全相同(見下面我的代碼)。 我希望 BiLSTM 的輸出在批次維度上是相同的,即out[:,0,:] == out[:,1,:] == out[:, 2, :]

但情況似乎並非如此。 根據我的實驗,20%~40% 的時間,輸出是不一樣的。 所以我想知道我哪里錯了。

# Python 3.6.6, Pytorch 0.4.1
import torch

def test(hidden_size, in_size):
    seq_len, batch = 4, 3
    bilstm = torch.nn.LSTM(input_size=in_size, hidden_size=hidden_size, 
                            num_layers=2, bidirectional=True)

    # create a batch with 3 exactly the same tensors
    a = torch.rand(seq_len, 1, in_size)  # (seq_len, 1, in_size)
    x = torch.cat((a, a, a), dim=1)

    out, _ = bilstm(x)  # (seq_len, batch, n_direction * hidden_size)

    # expect the output should be the same along the batch dimension
    assert torch.equal(out[:, 0, :], out[:, 1, :])  
    assert torch.equal(out[:, 1, :], out[:, 2, :])

if __name__ == '__main__':
    count, total = 0, 0
    for h_size in range(1, 51):
        for in_size in range(1, 51):
            total += 1
            try:
                test(h_size, in_size)
            except AssertionError:
                count += 1
    print('percentage of assertion error:', count / total)

使您困惑的是浮點精度。 浮點運算有些不准確,並且可能相差很小。請改用以下方法:

torch.set_default_dtype(torch.float64) 

然后,您將看到它們在批處理暗處應該是相同的。

感謝您糾正一些英語語法錯誤。

我對GRU有同樣的問題,以下為我解決了這個問題。
在測試之前設置手動種子並將模型設置為評估模式:

torch.manual_seed(42)
bilstm.eval()  # or: bilstm.train(false)

來源: LSTMcell 和 LSTM 返回不同的輸出

此外,我必須在每次調用模型之前(在測試期間)設置相同的種子。 在你的情況下:

torch.manual_seed(42)
out, _ = bilstm(x)  # (seq_len, batch, n_direction * hidden_size)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM