Skip to content
New issue

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

class PositionalEncoder代码是否存在问题? #34

Open
imgits opened this issue Nov 6, 2023 · 1 comment
Open

class PositionalEncoder代码是否存在问题? #34

imgits opened this issue Nov 6, 2023 · 1 comment

Comments

@imgits
Copy link

imgits commented Nov 6, 2023

章节“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)

@qzhangFDU
Copy link
Contributor

收到,已修改

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

No branches or pull requests

2 participants