![](/img/trans.png)
[英]How is the output h_n of an RNN (nn.LSTM, nn.GRU, etc.) in PyTorch structured?
[英]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)
此外,我必須在每次調用模型之前(在測試期間)設置相同的種子。 在你的情況下:
torch.manual_seed(42)
out, _ = bilstm(x) # (seq_len, batch, n_direction * hidden_size)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.