Beispiel #1
0
    def train(self):
        logger = logging.getLogger("brc")
        logger.info("====== training ======")
        logger.info('Load data_set and vocab...')
        with open(os.path.join(self.config.get_filepath().vocab_dir, 'vocab.data'), 'rb') as fin:
            vocab = pickle.load(fin)

        # print(vocab.get_char_size())

        brc_data = Propress(self.config.get_default_params().max_p_num,
                            self.config.get_default_params().max_p_len,
                            self.config.get_default_params().max_q_len,
                            self.config.get_default_params().max_ch_len,
                            train_files=self.dev_files, dev_files=self.dev_files)
        logger.info('Converting text into ids...')
        brc_data.convert_to_ids(vocab)
        logger.info('Initialize the model...')
        rc_model = Model(vocab, trainable=True)
        logger.info('Training the model...')
        rc_model.train(brc_data,
                       self.config.get_default_params().epoch,
                       self.config.get_default_params().batch_size,
                       save_dir=self.config.get_filepath().model_dir,
                       save_prefix=self.algo)
        logger.info('====== Done with model training! ======')
Beispiel #2
0
    def predict(self):
        logger = logging.getLogger("brc")

        logger.info('Load data_set and vocab...')
        with open(os.path.join(self.config.get_filepath().vocab_dir, 'vocab.data'), 'rb') as fin:
            vocab = pickle.load(fin)

        assert len(self.test_files) > 0, 'No test files are provided.'
        dataloader = Propress(self.config.get_default_params().max_p_num,
                              self.config.get_default_params().max_p_len,
                              self.config.get_default_params().max_q_len,
                              self.config.get_default_params().max_ch_len,
                              test_files=self.test_files)

        logger.info('Converting text into ids...')
        dataloader.convert_to_ids(vocab)
        logger.info('Restoring the model...')

        model = Model(vocab, trainable=False)
        model.restore(self.config.get_filepath().model_dir, self.algo)
        logger.info('Predicting answers for test set...')
        test_batches = dataloader.next_batch('test',
                                             self.config.get_default_params().batch_size,
                                             vocab.get_word_id(vocab.pad_token),
                                             vocab.get_char_id(vocab.pad_token),
                                             shuffle=False)

        model.evaluate(test_batches, 'test', result_dir=self.config.get_filepath().output_dir, result_prefix='test.predicted')
Beispiel #3
0
def evaluate(args):
    logger = logging.getLogger("brc")
    logger.info("====== evaluating ======")
    logger.info('Load data_set and vocab...')
    with open(os.path.join(args.vocab_dir, 'vocab.data'), 'rb') as fin:
        vocab = pickle.load(fin)

    assert len(args.dev_files) > 0, 'No dev files are provided.'
    dataloader = Propress(args.max_p_num,
                          args.max_p_len,
                          args.max_q_len,
                          args.max_ch_len,
                          dev_files=args.dev_files)

    logger.info('Converting text into ids...')
    dataloader.convert_to_ids(vocab)

    logger.info('Restoring the model...')
    model = Model(vocab, args)
    model.restore(args.model_dir, args.algo)
    logger.info('Evaluating the model on dev set...')
    dev_batches = dataloader.next_batch('dev',
                                        args.batch_size,
                                        vocab.get_word_id(vocab.pad_token),
                                        vocab.get_char_id(vocab.pad_token),
                                        shuffle=False)

    dev_loss, dev_bleu_rouge = model.evaluate(dev_batches,
                                              result_dir=args.result_dir,
                                              result_prefix='dev.predicted')

    logger.info('Loss on dev set: {}'.format(dev_loss))
    logger.info('Result on dev set: {}'.format(dev_bleu_rouge))
    logger.info('Predicted answers are saved to {}'.format(
        os.path.join(args.result_dir)))
Beispiel #4
0
    def evaluate(self):
        logger = logging.getLogger("brc")
        logger.info("====== evaluating ======")
        logger.info('Load data_set and vocab...')
        with open(os.path.join(self.config.get_filepath().vocab_dir, 'vocab.data'), 'rb') as fin:
            vocab = pickle.load(fin)

        assert len(self.dev_files) > 0, 'No dev files are provided.'
        dataloader = Propress(self.config.get_default_params().max_p_num,
                              self.config.get_default_params().max_p_len,
                              self.config.get_default_params().max_q_len,
                              self.config.get_default_params().max_ch_len,
                              dev_files=self.dev_files)

        logger.info('Converting text into ids...')
        dataloader.convert_to_ids(vocab)

        logger.info('Restoring the model...')
        model = Model(vocab, trainable=False)
        model.restore(self.config.get_filepath().model_dir, self.algo)
        logger.info('Evaluating the model on dev set...')
        dev_batches = dataloader.next_batch('dev',
                                            self.config.get_default_params().batch_size,
                                            vocab.get_id_byword(vocab.pad_token),
                                            vocab.get_id_bychar(vocab.pad_token),
                                            shuffle=False)

        dev_loss, dev_bleu_rouge, summ = model.evaluate(
            dev_batches, 'dev', result_dir=self.config.get_filepath().output_dir, result_prefix='dev.predicted')

        logger.info('Loss on dev set: {}'.format(dev_loss))
        logger.info('Result on dev set: {}'.format(dev_bleu_rouge))
        logger.info('Predicted answers are saved to {}'.format(os.path.join(self.config.get_filepath().output_dir)))
Beispiel #5
0
def train(args):
    logger = logging.getLogger("QANet")
    logger.info("====== training ======")

    logger.info('Load data_set and vocab...')
    with open(os.path.join(args.vocab_dir, 'vocab.data'), 'rb') as fin:
        vocab = pickle.load(fin)

    dataloader = Propress(args.max_p_num, args.max_p_len, args.max_q_len,
                          args.max_ch_len, args.train_files, args.dev_files)

    logger.info('Converting text into ids...')
    dataloader.convert_to_ids(vocab)

    logger.info('Initialize the model...')
    model = Model(vocab, args)

    logger.info('Training the model...')
    model.train(dataloader,
                args.epochs,
                args.batch_size,
                save_dir=args.model_dir,
                save_prefix=args.algo,
                dropout=args.dropout)

    logger.info('====== Done with model training! ======')