RoseTTAFold PositionalEncoding类解读
RoseTTAFold
的 PositionalEncoding
类实现了位置编码,用于将序列信息与神经网络模型的输入特征融合。该位置编码与 Transformer 的正弦-余弦编码类似,但在实现上包含了一些定制化的修改,尤其是在处理不同序列长度方面。
源代码:
class PositionalEncoding(nn.Module):
def __init__(self, d_model, p_drop=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.drop = nn.Dropout(p_drop,inplace=True)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) *
-(math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe) # (1, max_len, d_model)
def forward(self, x, idx_s):
pe = list()
for idx in idx_s:
pe.append(s