def tune_model_weights():
    parser = generate.get_parser_with_args()
    parser = add_tune_args(parser)
    args = options.parse_args_and_arch(parser)
    print(args.model_weights)
    n_models = len(args.path.split(CHECKPOINT_PATHS_DELIMITER))
    print(n_models)

    weight_grid = np.linspace(
        args.weight_lower_bound, args.weight_upper_bound, args.n_grid + 1
    )
    weight_vec_aux = list(itertools.product(weight_grid, weight_grid))
    weight_vec = []
    for w1, w2 in weight_vec_aux:
        weight_sum = w1 + w2
        if weight_sum <= 1:
            w3 = 1 - weight_sum
            weight_vec.append(str(w1) + "," + str(w2) + "," + str(w3))

    print(len(weight_vec))
    output = pd.DataFrame()
    for weight in weight_vec:
        args.model_weights = weight
        print(args.model_weights)
        generate.validate_args(args)
        score = generate.generate(args)
        print(score)
        output = output.append(
            {"weight": args.model_weights, "bleu_score": score}, ignore_index=True
        )
        output.to_csv(args.output_file_name)
    return output
Example #2
0
def generate_main(data_dir, extra_flags=None):
    parser = generate.get_parser_with_args()
    args = options.parse_args_and_arch(
        parser,
        [
            "--source-vocab-file",
            os.path.join(data_dir, "dictionary-in.txt"),
            "--target-vocab-file",
            os.path.join(data_dir, "dictionary-out.txt"),
            "--source-text-file",
            os.path.join(data_dir, "test.in"),
            "--target-text-file",
            os.path.join(data_dir, "test.out"),
            "--path",
            os.path.join(data_dir, "checkpoint_last.pt"),
            "--beam",
            "3",
            "--length-penalty",
            "0.0",
            "--batch-size",
            "64",
            "--max-len-b",
            "5",
            "--no-progress-bar",
        ] + (extra_flags or []),
    )
    generate.validate_args(args)
    generate.generate(args)
def tune_model_weights():
    parser = generate.get_parser_with_args()
    parser = add_tune_args(parser)
    args = options.parse_args_and_arch(parser)
    n_models = len(args.path.split(CHECKPOINT_PATHS_DELIMITER))
    print(n_models)
    print(args.weight_lower_bound)
    print(args.weight_upper_bound)
    print(args.output_json_best_parameters)
    print(args.output_json_best_value)
    print(args.num_trails_ax_opt)

    def evaluation_function(parameterization):
        w1 = parameterization.get("w1")
        w2 = parameterization.get("w2")
        w3 = parameterization.get("w3")
        weight = str(w1) + "," + str(w2) + "," + str(w3)
        args.model_weights = weight
        generate.validate_args(args)
        score = generate.generate(args)
        return {"bleu_score": (score, 0.0)}

    lower_bound = args.weight_lower_bound
    upper_bound = args.weight_upper_bound
    best_parameters, values, experiment, model = optimize(
        parameters=[
            {
                "name": "w1",
                "type": "range",
                "bounds": [lower_bound, upper_bound],
                "value_type": "float",
            },
            {"name": "w2", "type": "range", "bounds": [lower_bound, upper_bound]},
            {"name": "w3", "type": "range", "bounds": [lower_bound, upper_bound]},
        ],
        experiment_name="tune_model_weights",
        objective_name="bleu_score",
        evaluation_function=evaluation_function,
        minimize=True,  # Optional, defaults to False.
        parameter_constraints=[
            "w1 + w2 + w3 <= 1",
            "w1 + w2 + w3 >= 0.99",
        ],  # Optional.
        total_trials=args.num_trails_ax_opt,  # Optional.
    )

    json_file = json.dumps(best_parameters)
    with open(args.output_json_best_parameters, "w") as f:
        f.write(json_file)
        f.close()

    json_file = json.dumps(values)
    with open(args.output_json_best_value, "w") as f:
        f.write(json_file)
        f.close()
    return best_parameters, values