Пример #1
0
def main():
    r"""
    Main function.
    """
    model = QANet()
    # initial parameters
    logging.info('Initial paramters...')
    if opt.load_trained_model:
        model.load_parameters(opt.trained_model_name, ctx=CTX)
    else:
        logging.info('Initial model parameters...')
        initial_model_parameters(model)
    print(model)
    if opt.is_train:
        loss_function = MySoftmaxCrossEntropy()

        ema = ExponentialMovingAverage(decay=opt.ema_decay)

        # initial trainer
        trainer = gluon.Trainer(
            model.collect_params(), 'adam', {
                'learning_rate': opt.init_learning_rate,
                'beta1': opt.beta1,
                'beta2': opt.beta2,
                'epsilon': opt.epsilon
            })

        if opt.load_trained_model:
            trainer.load_states(opt.trained_trainer_name)

        # initial dataloader
        train_data_loader = DataLoader(batch_size=opt.train_batch_size,
                                       dev_set=False)

        # train
        logging.info('Train')
        train(model, train_data_loader, trainer, loss_function, ema)
    else:
        logging.info('Evaluating dev set...')
        f1_score, em_score = evaluate(model, dataset_type='dev', ema=None)
        logging.debug('The dev dataset F1 is:%.5f, and EM is: %.5f', f1_score,
                      em_score)
Пример #2
0
def main():
    r"""
    Main function.
    """
    model = QANet()
    # initial parameters
    print('Initial paramters...')
    if NEED_LOAD_TRAINED_MODEL:
        model.load_parameters(TARGET_MODEL_FILE_NAME, ctx=CTX)
    else:
        print('Initial model parameters...')
        initial_model_parameters(model)
    print(model)
    if TRAIN_FLAG is True:
        loss_function = MySoftmaxCrossEntropy()

        ema = ExponentialMovingAverage(decay=EXPONENTIAL_MOVING_AVERAGE_DECAY)

        # initial trainer
        trainer = gluon.Trainer(
            model.collect_params(), 'adam', {
                'learning_rate': INIT_LEARNING_RATE,
                'beta1': BETA1,
                'beta2': BETA2,
                'epsilon': EPSILON
            })

        if NEED_LOAD_TRAINED_MODEL:
            trainer.load_states(TARGET_TRAINER_FILE_NAME)

        # initial dataloader
        train_data_loader = DataLoader(batch_size=TRAIN_BATCH_SIZE,
                                       dev_set=False)

        # train
        print('Train...')
        train(model, train_data_loader, trainer, loss_function, ema)
    else:
        print('Evaluating dev set...')
        f1_score, em_score = evaluate(model, dataset_type='dev', ema=None)
        print('The dev dataset F1 is:%s, and EM is: %s' % (f1_score, em_score))
Пример #3
0
def train(num_units, batch_size, sentence_size, embedding_size, ctx):
    net = QANet(num_units, batch_size, sentence_size, embedding_size)
    net.collect_params().initialize(ctx)
    loss = LogLoss()
    trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1})