Skip to content

章节2.3.2:位置编码计算结果修正#175

Open
EF-Lin233 wants to merge 1 commit intodatawhalechina:mainfrom
EF-Lin233:main
Open

章节2.3.2:位置编码计算结果修正#175
EF-Lin233 wants to merge 1 commit intodatawhalechina:mainfrom
EF-Lin233:main

Conversation

@EF-Lin233
Copy link

2.3.2 位置编码 中的位置示例中的x =[[ 0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5], [0.3, 0.4, 0.5, 0.6], [0.4, 0.5, 0.6, 0.7]]
输出结果有误,XPE应该是
[[ 0.100, 1.200, 0.300, 1.400],
[ 1.042, 0.840, 0.410, 1.500],
[ 1.210, -0.016, 0.520, 1.600],
[ 0.541, -0.490, 0.630, 1.700]]] (全部保留三位小数)

测试

class PositionalEncoding(nn.Module):
    def __init__(self, d_model, dropout=0.0, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * -math.log(10000.0) / d_model)
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.pe = pe.unsqueeze(0)

    def forward(self, x):
        x = x + self.pe[:, : x.size(1)].requires_grad_(False)
        return self.dropout(x)

x = torch.tensor([[ 0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5], [0.3, 0.4, 0.5, 0.6], [0.4, 0.5, 0.6, 0.7]])
pe = PositionalEncoding(4)
x = pe(x)
print(x)

得到

tensor([[[ 0.1000,  1.2000,  0.3000,  1.4000],
         [ 1.0415,  0.8403,  0.4100,  1.4999],
         [ 1.2093, -0.0161,  0.5200,  1.5998],
         [ 0.5411, -0.4900,  0.6300,  1.6996]]])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant