Ejemplo n.º 1
0
def run(args):

    print("Arguments in args:\n{}".format(pprint.pformat(vars(args))),
          flush=True)

    # load configurations
    with open(args.conf, "r") as f:
        conf = yaml.load(f, Loader=yaml.FullLoader)
    print("Arguments in yaml:\n{}".format(pprint.pformat(conf)), flush=True)

    checkpoint_dir = Path(conf['train']['checkpoint'])
    checkpoint_dir.mkdir(exist_ok=True, parents=True)

    random.seed(conf['train']['seed'])
    np.random.seed(conf['train']['seed'])
    th.cuda.manual_seed_all(conf['train']['seed'])

    # if exist, resume training
    last_checkpoint = checkpoint_dir / "last.pt.tar"
    if last_checkpoint.exists():
        print(f"Found old checkpoint: {last_checkpoint}", flush=True)
        conf['train']['resume'] = last_checkpoint.as_posix()

    # dump configurations
    with open(checkpoint_dir / "train.yaml", "w") as f:
        yaml.dump(conf, f)

    #build nnet
    nnet = model(**conf["nnet_conf"])
    # build optimizer
    optimizer = make_optimizer(nnet.parameters(), conf)
    # build dataloader
    train_loader, valid_loader = make_dataloader(conf)
    # build scheduler
    scheduler = ReduceLROnPlateau(optimizer,
                                  mode="min",
                                  factor=conf['scheduler']['factor'],
                                  patience=conf['scheduler']['patience'],
                                  min_lr=conf['scheduler']['min_lr'],
                                  verbose=True)

    device = th.device('cuda' if conf['train']['use_cuda']
                       and th.cuda.is_available() else 'cpu')

    trainer = Trainer(nnet, optimizer, scheduler, device, conf)

    trainer.run(
        train_loader,
        valid_loader,
        num_epoches=conf['train']['epoch'],
    )
Ejemplo n.º 2
0
def train(config):
    fields = rstdt_fields()
    dataset = Dataset(config, fields)
    train_iter, valid_iter = dataset.load_train_valid()

    model = SpanBasedParser.build_model(config, fields)
    print(model)
    optimizer = {
        'adam': optim.Adam,
        'sgd': optim.SGD
    }[config.optimizer](model.parameters(),
                        lr=config.lr,
                        weight_decay=config.weight_decay)
    scheduler = optim.lr_scheduler.ExponentialLR(optimizer, config.lr_decay)
    trainer = Trainer(config, model, optimizer, scheduler, train_iter,
                      valid_iter, fields)
    trainer.run()

    return