小编XMa*_*t3R的帖子

如何让torch.nn.Transformer获得稳定的输出

看起来 pytorch 的 Transformer 层无法提供可重现的输出。cpu和gpu都会出现这种情况。我知道有时会因为 GPU 上的并行计算而发生这种情况。

emb = nn.Embedding(10, 12).to(device)
inp1 = torch.LongTensor([1, 2, 3, 4]).to(device)
inp1 = emb(inp1).reshape(inp1.shape[0], 1, 12) #S N E

encoder_layer = nn.TransformerEncoderLayer(d_model=12, nhead=4)
transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=4)

out1 = transformer_encoder(inp1)
out2 = transformer_encoder(inp1)
Run Code Online (Sandbox Code Playgroud)

out1 和 out2 不同。它可以在 cpu 上进行多处理,但结果看起来太不稳定。如何解决这个问题?

python transformer-model neural-network pytorch

0
推荐指数
1
解决办法
789
查看次数