05 Transformer 中的前饋神經網絡(FFN)的實現

2:20:理論鏈接

博客配套視頻鏈接: https://space.bilibili.com/383551518?spm_id_from=333.1007.0.0 b 站直接看

配套 github 鏈接:https://github.com/nickchen121/Pre-training-language-model

配套博客鏈接:https://www.cnblogs.com/nickchen121/p/15105048.html

img
class PositionWiseFeedForward(nn.Module):

    """
    w2(relu(w1(layer_norm(x))+b1))+b2
    """

    def __init__(self, d_model, d_ff, dropout=0.1):
        super(PositionWiseFeedForward, self).__init__()
        self.w_1 = nn.Linear(d_model, d_ff)
        self.w_2 = nn.Linear(d_ff, d_model)
        self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)
        self.dropout_1 = nn.Dropout(dropout)
        self.relu = nn.ReLU()
        self.dropout_2 = nn.Dropout(dropout)

    def forward(self, x):
        inter = self.dropout_1(self.relu(self.w_1(self.layer_norm(x))))
        output = self.dropout_2(self.w_2(inter))
        return output
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章