| 742 | # Luong attention layer |
| 743 | class Attn(nn.Module): |
| 744 | def __init__(self, method, hidden_size): |
| 745 | super(Attn, self).__init__() |
| 746 | self.method = method |
| 747 | if self.method not in ['dot', 'general', 'concat']: |
| 748 | raise ValueError(self.method, "is not an appropriate attention method.") |
| 749 | self.hidden_size = hidden_size |
| 750 | if self.method == 'general': |
| 751 | self.attn = nn.Linear(self.hidden_size, hidden_size) |
| 752 | elif self.method == 'concat': |
| 753 | self.attn = nn.Linear(self.hidden_size * 2, hidden_size) |
| 754 | self.v = nn.Parameter(torch.FloatTensor(hidden_size)) |
| 755 | |
| 756 | def dot_score(self, hidden, encoder_output): |
| 757 | return torch.sum(hidden * encoder_output, dim=2) |