Transformer.py 中的 forword 方法调用了 SublayerConnection 类。该类实现了残差连接和标准化。

作者: dshwei创建于 2020年9月23日更新于 2025年6月22日

sublayerout = layerNorm(x + sublayer(x)) ** First is the residual connection, then is the layer normalization. In your code, in sublayer.py, it should be: def forward(self, x, sublayer): "Apply residual connection to any sublayer with the same size." # return x + self.dropout(sublayer(self.norm(x))) return self.norm(x + self.dropout(sublayer(x))) In transformer.py: def forward(self, x, mask): x = self.input_sublayer(x, lambda _x: self.attention.forward(_x, _x, _x, mask=mask)) x = self.output_sublayer(x, **lambda _x: self.feed_forward.forward(_x)) return self.dropout(x) Here I have immediately made a difference from the paper, and there are errors. Please give me some advice.

内容来源: codertimo/BERT-pytorch