Exemplo n.º 1
0
    def forward(self, inputs, mask, hidden, lengths):
        sorted_lengths, sort, unsort = sort_by_lengths(lengths)

        embedded, mask = self.embeddings(inputs)
        output_encoder, hidden = self.encoder(sort(embedded), hidden=None, mask=sort(mask), lengths=sorted_lengths)
        representations, attentions = self.attention(output_encoder, mask=sort(mask), lengths=sorted_lengths)

        output = self.hidden2out(unsort(representations))

        return output, hidden
Exemplo n.º 2
0
    def forward(self, inputs, mask, hidden, lengths):
        sorted_lengths, sort, unsort = sort_by_lengths(lengths)

        embedded, mask = self.embeddings(inputs)

        output, hidden = self.encoder(sort(embedded),
                                      hidden,
                                      sort(mask),
                                      lengths=sorted_lengths)

        output = self.dropout(unsort(output))

        decoded = self.decoder(
            output.view(output.size(0) * output.size(1), output.size(2)))

        return decoded.view(output.size(0), output.size(1),
                            decoded.size(1)), hidden
    def forward(self, inputs, mask, hidden, lengths):
        sorted_lengths, sort, unsort = sort_by_lengths(lengths)
        batch_size = len(inputs)

        embedded, mask = self.embeddings(inputs)
        output_encoder, (ht, ct) = self.encoder(sort(embedded),
                                                hidden=None,
                                                mask=sort(mask),
                                                lengths=sorted_lengths)

        if self.attention:
            representations, attentions = self.attention(
                output_encoder, mask=sort(mask), lengths=sorted_lengths)
        else:
            representations = ht[-2:].transpose(0, 1).contiguous().view(
                batch_size, -1)

        output = self.hidden2out(unsort(representations))

        return output, hidden