def predict(self, seq2seq):
        predicts = []
        ids = []
        sentences = []
        lengths = []
        seq2seq = Seq2seq(self.config, device=self.device, load_emb=True)
        # gold = []
        data = prepare.load_data(self.mode)
        if mode == 'test':
            data = prepare.test_process(data)
        else:
            data = prepare.process(data)
        data = data_prepare.Data(data, config.batch_size, config)
        for batch_i in range(data.batch_number):
            batch_data = data.next_batch(is_random=False)
            pred_action_list, _ = self.test_step(batch_data, seq2seq)
            pred_action_list = pred_action_list.cpu().numpy()

            sentences.extend(batch_data.sentence_fw)
            predicts.extend([
                pred_action_list[:, i]
                for i in range(pred_action_list.shape[1])
            ])
            # print(len(predicts))
            ids.extend(batch_data.standard_outputs)
            lengths.extend(batch_data.input_sentence_length)

        evaluation.get_result(ids, sentences, lengths, predicts, config)
        # gold.extend(batch_data.all_triples)

        # (r_f1, r_precision, r_recall), (e_f1, e_precision, e_recall) = evaluation.rel_entity_compare(predicts, gold, self.config)
        data.reset()
Beispiel #2
0
    def __init__(self, config: const.Config, mode: str, device: torch.device) -> None:

        self.config = config

        self.device = device

        self.seq2seq = Seq2seq(config, device=device)

        data = prepare.load_data(mode)
        data = prepare.process(data)
        self.data = data_prepare.Data(data, config.batch_size, config)
Beispiel #3
0
    def rel_test(self, seq2seq) -> Tuple[Tuple[float, float, float]]:
        predicts = []
        gold = []
        loss = 0.0
        data = prepare.load_data(self.mode)
        if mode == 'test':
            data = prepare.test_process(data)
        else:
            data = prepare.process(data)
        data = data_prepare.Data(data, config.batch_size, config)
        for batch_i in tqdm(range(data.batch_number)):

            batch_data = data.next_batch(is_random=False)

            pred_action_list, pred_logits_list = self.test_step(
                batch_data, seq2seq)

            predicts.extend(pred_action_list)
            gold.extend(batch_data.all_triples)
            mean_loss = 0.0
            if self.config.losstype == 1:
                ##1.原来###################################
                for t in range(seq2seq.decoder.decodelen):
                    # print(pred_logits_list[t])
                    mean_loss = mean_loss + F.nll_loss(
                        pred_logits_list[t],
                        torch.from_numpy(batch_data.standard_outputs).to(
                            self.device).to(torch.long)[:, t])
                    # print(pred_logits_list[t],
                    #                          torch.from_numpy(batch_data.standard_outputs).to(self.device).to(
                    #                              torch.long)[:, t])
                    # print(torch.from_numpy(batch_data.standard_outputs).to(self.device).to(
                    #                              torch.long)[:, t],pred_logits_list[t].shape,F.nll_loss(pred_logits_list[t],
                    #                          torch.from_numpy(batch_data.standard_outputs).to(self.device).to(
                    #                              torch.long)[:, t]),loss)
            mean_loss /= pred_logits_list[0].shape[0]
            if (batch_i < 1000):
                loss += mean_loss

        loss /= 1000
        f1, precision, recall = evaluation.compare(predicts,
                                                   gold,
                                                   self.config,
                                                   show_rate=None,
                                                   simple=True)
        (r_f1, r_precision,
         r_recall), (e_f1, e_precision,
                     e_recall) = evaluation.rel_entity_compare(
                         predicts, gold, self.config)

        return loss.item(), (f1, precision,
                             recall), (r_f1, r_precision,
                                       r_recall), (e_f1, e_precision, e_recall)
    def __init__(self, config: const.Config, device: torch.device) -> None:

        self.config = config

        self.device = device

        self.seq2seq = Seq2seq(config, device=device, load_emb=True)
        self.loss = nn.NLLLoss()
        self.optimizer = torch.optim.Adam(self.seq2seq.parameters())

        data = prepare.load_data('train')
        data = prepare.process(data)
        self.data = data_prepare.Data(data, config.batch_size, config)

        self.epoch_number = config.epoch_number + 1
    def rel_test(self) -> Tuple[Tuple[float, float, float]]:
        predicts = []
        gold = []
        data = prepare.load_data(mode)
        if mode == 'test':
            data = prepare.test_process(data)
        else:
            data = prepare.process(data)
        data = data_prepare.Data(data, config.batch_size, config)
        for batch_i in range(data.batch_number):
            batch_data = data.next_batch(is_random=False)
            pred_action_list, pred_logits_list = self.test_step(batch_data)
            pred_action_list = pred_action_list.cpu().numpy()

            predicts.extend(pred_action_list)
            gold.extend(batch_data.all_triples)

        (r_f1, r_precision,
         r_recall), (e_f1, e_precision,
                     e_recall) = evaluation.rel_entity_compare(
                         predicts, gold, self.config)
        self.data.reset()
        return (r_f1, r_precision, r_recall), (e_f1, e_precision, e_recall)
    def test(self) -> Tuple[float, float, float]:
        predicts = []
        gold = []
        data = prepare.load_data(mode)
        if mode == 'test':
            data = prepare.test_process(data)
        else:
            data = prepare.process(data)
        data = data_prepare.Data(data, config.batch_size, config)
        for batch_i in range(data.batch_number):
            batch_data = data.next_batch(is_random=False)
            pred_action_list, pred_logits_list = self.test_step(batch_data)
            pred_action_list = pred_action_list.cpu().numpy()

            predicts.extend(pred_action_list)
            gold.extend(batch_data.all_triples)

        f1, precision, recall = evaluation.compare(predicts,
                                                   gold,
                                                   self.config,
                                                   show_rate=None,
                                                   simple=True)
        data.reset()
        return f1, precision, recall
    def event_test(self, seq2seq) -> Tuple[Tuple[float, float, float]]:
        predicts = []
        gold = []
        data = prepare.load_data(self.mode)
        if mode == 'test':
            data = prepare.test_process(data)
        else:
            data = prepare.process(data)
        data = data_prepare.Data(data, config.batch_size, config)
        loss = 0.0
        # Loss=nn.NLLLoss()
        for batch_i in tqdm(range(data.batch_number)):
            batch_data = data.next_batch(is_random=False)
            pred_action_list, pred_logits_list = self.test_step(
                batch_data, seq2seq)
            pred_action_list = pred_action_list.cpu().numpy()
            gold.extend(batch_data.standard_outputs)
            # for i in range()
            predicts.extend([
                pred_action_list[:, i]
                for i in range(pred_action_list.shape[1])
            ])

            if decoder_type == 'onecrf':
                loss += pred_logits_list  #crf的时候输出的是loss
            else:
                if self.config.losstype == 1:
                    ##1.原来###################################
                    for t in range(seq2seq.decoder.decodelen):
                        # print(pred_logits_list[t])
                        loss = loss + F.nll_loss(
                            pred_logits_list[t],
                            torch.from_numpy(batch_data.standard_outputs).to(
                                self.device).to(torch.long)[:, t])
                elif self.config.losstype == 2:
                    ##2.loss2,排列组合###################################
                    all_events = torch.from_numpy(
                        batch_data.standard_outputs).to(self.device).to(
                            torch.long)
                    all_triples = batch_data.all_triples
                    lengths = batch_data.input_sentence_length
                    # print(pred_logits_list)
                    # print(pred_action_list)
                    for i in range(all_events.shape[0]):
                        # print(all_triples[i])
                        now_loss = 0.
                        triple_num = min(
                            len(all_triples[i]) // (lengths[i] + 1),
                            self.config.triple_number)
                        # print(pred_action_list[:self.max_sentence_length*triple_num,i].shape)
                        # pred_logits_list_event[:1+triple_num,i]
                        # print(pred_logits_list_entity[:self.max_sentence_length*triple_num,i].shape)
                        for j in range(triple_num):
                            glob = all_events[i,
                                              j * (self.max_sentence_length +
                                                   1):(j + 1) *
                                              (self.max_sentence_length + 1)]
                            # print(glob.shape)
                            for k in range(triple_num):
                                # print(pred_logits_list.shape,pred_logits_list[k*(self.max_sentence_length+1):(k+1)*(self.max_sentence_length+1),i].shape)
                                now_loss += F.nll_loss(
                                    pred_logits_list[
                                        k * (self.max_sentence_length +
                                             1):(k + 1) *
                                        (self.max_sentence_length + 1), i],
                                    glob)
                        if (triple_num != 0):
                            now_loss /= (triple_num * triple_num)
                        # print(pred_logits_list[triple_num*(self.max_sentence_length+1)+1,i],all_events[i,triple_num*(self.max_sentence_length+1)+1])
                        loss += now_loss + F.nll_loss(
                            pred_logits_list[
                                triple_num *
                                (self.max_sentence_length + 1):triple_num *
                                (self.max_sentence_length + 1) + 1, i],
                            all_events[i, triple_num *
                                       (self.max_sentence_length +
                                        1):triple_num *
                                       (self.max_sentence_length + 1) + 1])

            # for t in range(seq2seq.decoder.decodelen):
            #     # print(pred_logits_list[t], batch_data.standard_outputs[:, t])
            #     loss += F.nll_loss(pred_logits_list[t],torch.from_numpy(batch_data.standard_outputs).to(self.device).to(torch.long)[:, t]).item()
            # print(loss)
        loss /= batch_i
        # for g in gold:
        #     for i in range(5):
        #         l=g[i*(config.max_sentence_length+1):(i+1)*(config.max_sentence_length+1)]
        #         if(l[0]>30):
        #             print(l)

        require_f1, require_precision, require_recall = evaluation.event_entity_yaoqiu_compare(
            predicts, gold, self.config)
        f1, precision, recall = evaluation.compare(predicts,
                                                   gold,
                                                   self.config,
                                                   show_rate=None,
                                                   simple=True)
        (r_f1, r_precision,
         r_recall), (e_f1, e_precision,
                     e_recall) = evaluation.rel_entity_compare(
                         predicts, gold, self.config)
        (event_f1, event_precision,
         event_recall), (entity_f1, entity_precision,
                         entity_recall) = evaluation.event_entity_compare(
                             predicts, gold, self.config)
        data.reset()
        return loss.item(), (require_f1, require_precision, require_recall), (
            f1, precision, r_recall), (r_f1, r_precision, r_recall), (
                e_f1, e_precision,
                e_recall), (event_f1, event_precision,
                            event_recall), (entity_f1, entity_precision,
                                            entity_recall)
Beispiel #8
0
    else:
        print 'illegal dataset name: %s' % config.dataset_name
        exit()

    if is_train:
        if config.train_method == TrainMethod.NLL_METHOD:
            min_number = 1
        if config.train_method == TrainMethod.RL_METHOD:
            min_number = 1
    else:
        min_number = 1  # this is the min number of triples in a sentence

    logger.info('Prepare {} data'.format(train_test_valid))
    data = prepare.load_data(train_test_valid.lower())
    data = prepare.process(data, min_number)
    data = data_prepare.Data(data, config.batch_size, config)

    decoder, sess = get_model(train_method=config.train_method, config=config)
    # decoder, sess = None, None
    if is_train:
        if config.train_method == TrainMethod.NLL_METHOD:
            logger.info('****************************** NLL Train ******************************')
            train_NLL_model(data, epoch_range=range(1, config.epoch_number + 1), decoder=decoder,
                            sess=sess)
        if config.train_method == TrainMethod.RL_METHOD:
            logger.info('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RL Train %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
            train_RL_model(data, epoch_range=range(1, config.epoch_number + 1), decoder=decoder,
                           sess=sess)

    else:
        logger.info(