|  | 
| 4 | 4 | 
 | 
| 5 | 5 | 
 | 
| 6 | 6 | class LSTMDecoder(nn.Module): | 
| 7 |  | -    def __init__(self, hidden_size, output_size, device, attention=False, pointer_network=False): | 
|  | 7 | +    """ | 
|  | 8 | +    Sequence decoder which makes use of a single-layer LSTM. | 
|  | 9 | +    """ | 
|  | 10 | +    def __init__(self, hidden_size, output_size, device, attention=False): | 
| 8 | 11 |         super(LSTMDecoder, self).__init__() | 
| 9 | 12 |         self.hidden_size = hidden_size | 
| 10 | 13 |         self.output_size = output_size | 
|  | 14 | +        self.attention = attention | 
|  | 15 | +        self.device = device | 
|  | 16 | + | 
| 11 | 17 |         self.embedding = nn.Embedding(output_size, hidden_size).to(device) | 
| 12 | 18 |         self.lstm = nn.LSTM(hidden_size, hidden_size).to(device) | 
| 13 | 19 |         self.out = nn.Linear(hidden_size, output_size).to(device) | 
| 14 | 20 |         self.softmax = nn.LogSoftmax(dim=1) | 
| 15 |  | -        self.attention = attention | 
| 16 |  | -        self.pointer_network = pointer_network | 
| 17 | 21 |         self.attention_layer = nn.Linear(hidden_size * 2, 1).to(device) | 
| 18 | 22 |         self.attention_combine = nn.Linear(hidden_size * 2, hidden_size).to(device) | 
| 19 |  | -        self.device = device | 
| 20 | 23 | 
 | 
| 21 | 24 |     def forward(self, input, hidden, encoder_hiddens, input_seq=None): | 
| 22 | 25 |         # encoder_hiddens has shape [batch_size, seq_len, hidden_dim] | 
| 23 | 26 |         output = self.embedding(input).view(1, 1, -1) | 
| 24 | 27 | 
 | 
|  | 28 | +        # Compute attention | 
| 25 | 29 |         if self.attention: | 
| 26 | 30 |             # Create a matrix of shape [batch_size, seq_len, 2 * hidden_dim] where the last | 
| 27 | 31 |             # dimension is a concatenation of the ith encoder hidden state and the current decoder | 
|  | 
0 commit comments