Ejemplo n.º 1
0
            vocabulary_name=hparams.vocabulary_name, 
            corpus_name=hparams.corpus_name, 
            cached_data_dir=args.cached_dataset_dir, 
            hparams=hparams)
        data.prepare_data()
    else:
        setup_tf_logging(args.model_dir)
        log_hparams(args.model_dir)

        warm_start = None
        if args.params_from_cudnn_rnn is not None:
            platform_independent_to_cudnn_variables = {
                "affine_projection_pseudo_cell/w": "affine_projection_layer/w",
                "affine_projection_pseudo_cell/b": "affine_projection_layer/b",
                "rnn/predict_next/multi_rnn_cell/cell_0/cudnn_compatible_lstm_cell/kernel": 
                  "cudnn_lstm/rnn/multi_rnn_cell/cell_0/cudnn_compatible_lstm_cell/kernel",
                "rnn/predict_next/multi_rnn_cell/cell_0/cudnn_compatible_lstm_cell/bias": 
                  "cudnn_lstm/rnn/multi_rnn_cell/cell_0/cudnn_compatible_lstm_cell/bias",
                "rnn/predict_next/multi_rnn_cell/cell_1/cudnn_compatible_lstm_cell/kernel": 
                  "cudnn_lstm/rnn/multi_rnn_cell/cell_1/cudnn_compatible_lstm_cell/kernel",
                "rnn/predict_next/multi_rnn_cell/cell_1/cudnn_compatible_lstm_cell/bias": 
                  "cudnn_lstm/rnn/multi_rnn_cell/cell_1/cudnn_compatible_lstm_cell/bias",
            }
            warm_start = tf.estimator.WarmStartSettings(
                    ckpt_to_initialize_from=args.params_from_cudnn_rnn,
                    var_name_to_prev_var_name=platform_independent_to_cudnn_variables,
                )

        logger.info("Running with parameters: {}".format(hparams.to_json()))
        train_and_eval(args.cached_dataset_dir, args.model_dir, hparams, warm_start)
Ejemplo n.º 2
0
            best_acc = acc
            torch.save({
                "epoch": current_epoch,
                "losses": losses,
                "model_state_dict": model.state_dict(),
                "optimizer_state_dict": optimizer.state_dict(),
            }, os.path.join(exp_dir, 'best.pth'))

        print("| Epoch: {:3d}, Eval loss: {:0.4f}, current acc: {:2.3f}%, the best: {:2.3f}%".format(current_epoch, eval_loss, acc*100, best_acc*100))

if __name__=="__main__":
    args = docopt(__doc__)
    db_path = args['<db_path>']
    feat_path = args['<feat_path>']
    exp_dir = args['<exp/task>']
    preset = args["--preset"]

    if preset is not None:
        with open(preset) as f:
            hparams.parse_json(f.read())

    # make a dir, like exp/2019task1b
    os.makedirs(exp_dir, exist_ok=True)

    fp = open(os.path.join(exp_dir, "config.json"),'w')
    fp.write(hparams.to_json(indent=' '*4))
    fp.close()

    print(hparams_debug_string())

    train(db_path, feat_path, exp_dir)
    parser.add_argument("model_dir")
    parser.add_argument("--cached_dataset_dir")
    parser.add_argument('--hparams',
                        type=str,
                        help='Comma separated list of "name=value" pairs.')
    args = parser.parse_args()
    if args.mode == "train":
        if args.hparams:
            hparams.parse(args.hparams)

        if args.cached_dataset_dir is None:
            train_lm_on_simple_examples_with_glove(args.model_dir)
        else:
            with open(Path(args.model_dir) / "hparams.json",
                      "wt") as params_file:
                print(hparams.to_json(), file=params_file)
            train_lm_on_cached_simple_examples_with_glove(
                args.cached_dataset_dir, args.model_dir, hparams)
    elif args.mode == "prepare":
        prepare_training_dataset(args.model_dir)
    elif args.mode == "predict":
        if args.hparams:
            hparams.parse(args.hparams)
        eval_lm_on_cached_simple_examples_with_glove(args.cached_dataset_dir,
                                                     args.model_dir, "train",
                                                     hparams)
    elif args.mode == "predict_with_lm_training_process":
        from lm_training_process import eval_lm_on_cached_simple_examples_with_glove_check
        if args.hparams:
            hparams.parse(args.hparams)
        eval_lm_on_cached_simple_examples_with_glove_check(