fix: Remove redundant transpose around PositionalEncoding in Encoder/…#1
Open
fix: Remove redundant transpose around PositionalEncoding in Encoder/…#1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
非常感谢项目所提供的代码,对我理解 Transformer 编码器结构有极大帮助。此次修改是在学习过程中发现的一个小问题,也可能是我对模型结构理解不够深入,如果此处的双重转置有其特定意图,欢迎指正!
问题
在原始实现中,Encoder 中对
PositionalEncoding的调用如下所示:该写法默认
PositionalEncoding接收的是[seq_len, batch_size, d_model]格式的数据,因此进行了两次 transpose。但实际代码中实现的PositionalEncoding是面向[batch_size, seq_len, d_model](内部使用了[seq_len, d_model]的位置表通过广播机制进行加权。因此,这两次 transpose 似乎是多余的。
解决方案
将上述代码简化为:
移除不必要的转置操作,保持数据结构统一。