We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
章节“2.1.1 嵌入表示层” PositionalEncoder类代码有误
1. class PositionalEncoder(nn.Module): 2. def __init__(self, d_model, max_seq_len = 80): 3. super().__init__() 4. self.d_model = d_model 5. 6. # 根据 pos 和 i 创建一个常量 PE 矩阵 7. pe = torch.zeros(max_seq_len, d_model) 8. for pos in range(max_seq_len): 9. for i in range(0, d_model, 2): 10. pe[pos, i] = math.sin(pos / (10000 ** ((2 * i)/d_model))) 11. pe[pos, i + 1] = math.cos(pos / (10000 ** ((2 * (i + 1))/d_model))) 12. pe = pe.unsqueeze(0) 13. self.register_buffer('pe', pe) 14. 15. def forward(self, x): 16. # 使得单词嵌入表示相对大一些 17. x = x * math.sqrt(self.d_model) 18. # 增加位置常量到单词嵌入表示中 19. seq_len = x.size(1) 20. x = x + Variable(self.pe[:,:seq_len], requires_grad=False).cuda() 21.
第10、11行代码:
pe[pos, i] = math.sin(pos / (10000 ** ((2 * i)/d_model))) pe[pos, i + 1] = math.cos(pos / (10000 ** ((2 * (i + 1))/d_model)))
应该是:
pe[pos, i] = math.sin(pos / (10000 ** ( i/d_model))) pe[pos, i + 1] = math.cos(pos / (10000 ** (i/d_model)))
第20行最好不要强行加上".cuda()", 建议更改为: x = x + self.pe[:, :x.size(1)].requires_grad(False)
x = x + self.pe[:, :x.size(1)].requires_grad(False)
The text was updated successfully, but these errors were encountered:
收到,已修改
Sorry, something went wrong.
你好,这里应该是.requires_grad_(False),如果使用requires_grad(False)的话会报TypeError: 'bool' object is not callable
No branches or pull requests
章节“2.1.1 嵌入表示层” PositionalEncoder类代码有误
第10、11行代码:
应该是:
第20行最好不要强行加上".cuda()", 建议更改为:
x = x + self.pe[:, :x.size(1)].requires_grad(False)
The text was updated successfully, but these errors were encountered: