예제 #1
0
    def forward(self, inputs):
        input_len = inputs.shape
        embed = self._embedding(inputs.view([-1, input_len[2]]))
        embed = self._dropout(embed)

        hidden = init_const(2,
                            input_len[0] * input_len[1],
                            self._hidden_size,
                            use_cuda=self._use_cuda)
        word_rnn_output, _ = self._word_gru(embed, hidden)
        word_attn_output = self._word_attn(word_rnn_output)

        sen_input = word_attn_output.view(
            [-1, input_len[1], self._hidden_size * 2])
        sen_input = self._dropout(sen_input)

        hidden = init_const(2,
                            input_len[0],
                            self._hidden_size,
                            use_cuda=self._use_cuda)
        sen_rnn_output, _ = self._sen_gru(sen_input, hidden)
        sen_attn_output = self._sen_attn(sen_rnn_output)

        logits = self._fc(sen_attn_output)
        return logits
예제 #2
0
    def forward(self, encoder_hidden, encoder_output, targets=None):
        batch_size = encoder_output.shape[1]
        max_len = self.max_output_len
        if targets is not None:
            max_len = targets.shape[1]

        decoder_input = init_const(batch_size,
                                   value=TOKEN.START,
                                   dtype=torch.long,
                                   use_cuda=self.use_cuda)
        hidden = encoder_hidden[1].unsqueeze(0)
        decoder_outputs = []

        use_teacher_forcing = False if (targets is None
                                        or random.random() < 0.5) else True

        for i in range(max_len):
            decoder_output, hidden = self.forward_step(decoder_input, hidden,
                                                       encoder_output)
            decoder_outputs.append(decoder_output)
            if use_teacher_forcing:
                decoder_input = targets[:, i]
            else:
                _, top_index = decoder_output.topk(1)
                decoder_input = top_index
        return decoder_outputs
예제 #3
0
 def forward(self, inputs):
     batch_size = inputs.shape[0]
     embedded = self.embedding(inputs).transpose(0, 1)
     embedded = self.dropout(embedded)
     hidden = init_const(2,
                         batch_size,
                         self.hidden_size,
                         use_cuda=self.use_cuda)
     output, hidden = self.gru(embedded, hidden)
     return output, hidden
예제 #4
0
    def forward(self, inputs):
        input_len = inputs.shape
        embed = self._embedding(inputs)
        embed = self._dropout(embed)

        hidden = init_const(2,
                            input_len[0],
                            self._hidden_size,
                            use_cuda=self._use_cuda)
        rnn_output, _ = self._gru(embed, hidden)
        rnn_output = rnn_output.mean(dim=1)

        logits = self._fc(rnn_output)
        return logits
예제 #5
0
    def forward(self, inputs):
        input_len = inputs.shape
        embed = self._embedding(inputs)
        embed = self._dropout(embed)

        hidden = init_const(2,
                            input_len[0],
                            self._hidden_size,
                            use_cuda=self._use_cuda)
        rnn_output, _ = self._gru(embed, hidden)
        rnn_output = rnn_output.transpose(1, 2)
        conv_output = self._conv1(rnn_output).view([input_len[0], -1])

        logits = self._fc(conv_output)
        return logits