Exemplo n.º 1
0
def evaluate(model, input, output):
    model.eval()
    with torch.no_grad():
        correct = 0
        result = model(input)
        for idx, gt_class in enumerate(output):
            pred_class = argmax(result[idx])
            gt_class = argmax(gt_class)
            if gt_class == pred_class:
                correct += 1
    return float(correct)
Exemplo n.º 2
0
def evaluate_all(model, data):
    model.eval()
    with torch.no_grad():
        # Slowly evaluates sample-by-sample
        correct = 0
        total = len(data)
        for sentence, gt_class in data:
            pred_class = argmax(model([sentence]))
            gt_class = argmax(gt_class)
            if gt_class == pred_class:
                correct += 1

    return float(correct) / float(total)
Exemplo n.º 3
0
def evaluate_all(model, data):
    with torch.no_grad():
        correct = 0
        total = len(data)
        input_data = process_input(data)
        for sentence, gt_class in input_data:
            precheck_sent = Variable(sentence)
            pred_class = argmax(model(precheck_sent.unsqueeze(0)))
            if gt_class == pred_class:
                correct += 1
    return float(correct) / float(total)
Exemplo n.º 4
0
    def _viterbi_decode(self, feats):
        backpointers = []

        # Initialize the viterbi variables in log space
        init_vvars = torch.Tensor(1, self.tagset_size).fill_(-10000.)
        init_vvars[0][self.tag_to_ix[START_TAG]] = 0

        # forward_var at step i holds the viterbi variables for step i-1
        forward_var = init_vvars.cuda() if self.is_cuda else init_vvars

        for feat in feats:
            next_tag_var = forward_var.view(1, -1).expand(
                self.tagset_size, self.tagset_size) + self.transitions
            _, bptrs_t = torch.max(next_tag_var, dim=1)
            bptrs_t = bptrs_t.squeeze().data.cpu().numpy()
            next_tag_var = next_tag_var.data.cpu().numpy()
            viterbivars_t = next_tag_var[range(len(bptrs_t)), bptrs_t]
            viterbivars_t = torch.FloatTensor(viterbivars_t)

            if self.is_cuda:
                viterbivars_t = viterbivars_t.cuda()

            forward_var = viterbivars_t + feat
            backpointers.append(bptrs_t)

        # Transition to STOP_TAG
        terminal_var = forward_var + self.transitions[self.tag_to_ix[STOP_TAG]]
        terminal_var.data[self.tag_to_ix[STOP_TAG]] = -10000.
        terminal_var.data[self.tag_to_ix[START_TAG]] = -10000.

        best_tag_id = argmax(terminal_var.unsqueeze(0))
        path_score = terminal_var[best_tag_id]

        # Follow the back pointers to decode the best path.
        best_path = [best_tag_id]
        for bptrs_t in reversed(backpointers):
            best_tag_id = bptrs_t[best_tag_id]
            best_path.append(best_tag_id)
        # Pop off the start tag (we dont want to return that to the caller)
        start = best_path.pop()
        assert start == self.tag_to_ix[START_TAG]  # Sanity check
        best_path.reverse()
        return path_score, best_path
Exemplo n.º 5
0
    def generate(self, input, beam_width=0, k=5000):
        with torch.no_grad():
            if beam_width == 0:  # Eager
                decoded, _, _ = self.forward(input)
                decoded = decoded.detach().cpu()
                result = []
                for idx in range(len(decoded)):
                    token = argmax(decoded[idx])
                    result.append(token)
                    if token == SOS_token:
                        break

                return result[::-1]
            else:  # Beam search
                encoder_hidden, mean, logv = self._encode_to_latent(input)

                std = torch.exp(0.5 * logv)
                z = torch.randn((self.max_length, self.latent_size))
                if self.is_cuda:
                    z = z.cuda()
                z = z * std + mean

                prev_beam = Beam(beam_width)
                prev_beam.add(0., False, [EOS_token], encoder_hidden)

                hidden = self.latent2hidden(z)

                while True:
                    curr_beam = Beam(beam_width)

                    for (prefix_prob, complete), (prefix,
                                                  hidden_state) in prev_beam:
                        if complete:
                            curr_beam.add(prefix_prob, True, prefix,
                                          hidden_state)
                        else:
                            decoder_input = torch.LongTensor([prefix[-1]])
                            if self.is_cuda:
                                decoder_input = decoder_input.cuda()
                            decoder_output, decoder_hidden, decoder_attention = self.decoder(
                                decoder_input, hidden_state, hidden)

                            if k == -1:
                                k = self.vocab_size

                            topv, topi = decoder_output.topk(k)
                            topv, topi = topv.squeeze(), topi.squeeze()

                            for idx, next_prob in enumerate(topv):
                                next_prob = next_prob.cpu().detach().item()
                                token = topi[idx].cpu().detach().item()
                                complete = (token == SOS_token)

                                # Adding probs because this is actually log probs (from log softmax)
                                curr_beam.add(prefix_prob + next_prob,
                                              complete, prefix + [token],
                                              decoder_hidden)
                    (best_prob, best_complete), (best_prefix,
                                                 _) = max(curr_beam)
                    if best_complete or len(best_prefix) - 1 == -1:
                        print('Found best candidate with probability: %s' %
                              best_prob)
                        return best_prefix[1:][::-1]
                    prev_beam = curr_beam