graykode / graykode/nlp-tutorial

about seq2seq(attention)-Torch multiple sample training question

未关闭
#54 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Jupyter Notebook
星标
14.9k
派生
3.9k
PR 合并指标
30 天内没有已合并 PR

描述

hello, first thank your code, but i want to know if batch_size is more than 1, i should how to modify the code, thank you
```
def get_att_weight(self, output, enc_output): # get attention weight one 'output' with 'enc_output'
'''
output: [1, batch_size, num_directions(=1) * n_hidden]
enc_output: [n_step+1, batch_size, num_directions(=1) * n_hidden]
'''
length = len(enc_output)
attn_scores = torch.zeros(length) # attn_scores : [batch_size, n_step+1]
for i in range(length):
attn_scores[i] = self.get_att_score(output, enc_output[i])

# Normalize scores to weights in range 0 to 1
# return [batch_size, 1, n_step+1]
return F.softmax(attn_scores).view(batch_size, 1, -1)

def get_att_score(self, output, enc_output):
'''
output: [batch_size, num_directions(=1) * n_hidden]
enc_output: [batch_size, num_directions(=1) * n_hidden]
'''
score = self.attn(enc_output) # score : [1, n_hidden]
return torch.dot(output.view(-1), score.view(-1)) # inner product make scalar value, get a real number
```

贡献指南

打开贡献指南

调研方向

从提供的 get_att_weight 和 get_att_score 方法开始,检查周围的 seq2seq attention 实现。跟踪 batch_size 大于一时的张量形状,然后验证 attention 训练能够在多个样本上正常工作且不会出现 scalar-shape 错误。完成的标准是示例支持 batch_size > 1,并且 attention 权重的形状正确。

由索引模型根据 Issue 内容生成。

评估

技术栈
python, pytorch
领域
machine-learning
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。