Exemplo n.º 1
0
def run_everything(args=None, logger=None):
    if args is None:
        parser = argparse.ArgumentParser(
            'Code to Natural Language Generation',
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        add_test_args(parser)
        config.add_model_args(parser)
        #args = parser.parse_args()
        args, unknown = parser.parse_known_args()
        print(args.data_dir)
    set_defaults(args)

    # Set cuda
    args.cuda = torch.cuda.is_available()
    args.parallel = torch.cuda.device_count() > 1

    # Set random state
    np.random.seed(args.random_seed)
    torch.manual_seed(args.random_seed)
    if args.cuda:
        torch.cuda.manual_seed(args.random_seed)

    if logger is None:
        fmt_list = []  #[('lr', "3.4e"),]
        fmt = dict(fmt_list)
        path = args.model_file[:args.model_file.rfind("/")+1]+\
               "_".join(["test"]+([args.comment] if args.comment!="" else []))+"/"
        logger = logger_module.Logger(args.comment,
                                      fmt=fmt,
                                      base=args.dir,
                                      path=path)
    logger.print(" ".join(sys.argv))
    logger.print(args)

    # Run!
    main(args, logger)
Exemplo n.º 2
0
                model.save(args.model_file)
                stats['best_valid'] = result[args.valid_metric]
                stats['no_improvement'] = 0
            else:
                stats['no_improvement'] += 1
                if stats['no_improvement'] >= args.early_stop:
                    break


if __name__ == '__main__':
    # Parse cmdline args and setup environment
    parser = argparse.ArgumentParser(
        'Code to Natural Language Generation',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    add_train_args(parser)
    config.add_model_args(parser)
    args = parser.parse_args()
    set_defaults(args)

    # Set cuda
    args.cuda = torch.cuda.is_available()
    args.parallel = torch.cuda.device_count() > 1

    # Set random state
    np.random.seed(args.random_seed)
    torch.manual_seed(args.random_seed)
    if args.cuda:
        torch.cuda.manual_seed(args.random_seed)

    # Set logging
    logger.setLevel(logging.INFO)