-
Notifications
You must be signed in to change notification settings - Fork 1
/
hptuning.py
156 lines (133 loc) · 4.9 KB
/
hptuning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import copy
import json
import os
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Dict
import click
import numpy as np
import optuna
import torch
from attrdict import AttrDict
from logzero import logger
from optuna import Trial
from optuna.study.study import Study
from ruamel.yaml import YAML
import dkt.trainer as trainer
from dkt.dataloader import Preprocess
from dkt.utils import log_elapsed_time, set_logger, setSeeds
def get_hp_params(trial: Trial, hp_params: Dict):
p = {}
for key, value in hp_params.items():
if value["type"] == "categorical":
p[key] = trial.suggest_categorical(key, value["value"])
elif value["type"] == "float":
p[key] = trial.suggest_float(
key, *value["value"], step=value.get("step", None)
)
elif value["type"] == "int":
p[key] = trial.suggest_int(key, *value["value"])
return p
def objective(trial: Trial, params: Dict, hp_params: Dict, data: np.ndarray):
p = copy.deepcopy(params)
p.update(get_hp_params(trial, hp_params))
return trainer.run(p, data)
@click.command(context_settings={"show_default": True})
@click.option(
"--model",
type=click.Choice(["lstm", "sakt", "saint", "akt"]),
default="sakt",
help="model",
)
@click.option("--output-root-dir", type=click.Path(), help="output root directory")
@click.option("--output-dir", type=click.Path(exists=True), help="Set output directory")
@click.option(
"--config-file-path",
type=click.Path(exists=True),
default="config/hp_params.yaml",
help="hp params config file path",
)
@click.option(
"--default-param-file-path",
type=click.Path(exists=True),
default="config/default_args.json",
help="default param file path",
)
@click.option("--seed", type=click.INT, default=42, help="seed")
@click.option("--n-trials", type=click.INT, default=20, help="# of trials")
@click.option("--study-name", type=click.STRING, default="study", help="Set study name")
@click.option(
"--storage-name",
type=click.STRING,
default="storage",
help="Set storage name to save study",
)
@click.option("--n-epochs", type=click.INT, default=100, help="# of epochs")
@click.option("--patience", type=click.INT, default=8, help="early stop patience")
@click.option("--clip-grad", type=click.FLOAT, default=5.0, help="clipping value")
@click.option("--lr", type=click.FLOAT, default=1e-3, help="learning rate")
@click.option("--batch-size", type=click.INT, default=128, help="batch size")
@click.option("--model-name", type=click.STRING, default="model.pt", help="model name")
@click.option(
"--logfile-name", type=click.STRING, default="train.log", help="logfile name"
)
@log_elapsed_time
def main(**args):
args = AttrDict(args)
yaml = YAML(typ="safe")
hp_params = AttrDict(yaml.load(Path(args.config_file_path)))
with open(args.default_param_file_path, "r", encoding="utf-8") as f:
params = AttrDict(json.load(f))
if args.output_dir:
params.output_dir = args.output_dir
else:
if args.output_root_dir:
params.output_dir = args.output_root_dir
params.output_dir = os.path.join(
params.output_dir, datetime.now().strftime("%Y%m%d_%H%M%S") + "_hptuning"
)
params.device = "cuda" if torch.cuda.is_available() else "cpu"
params.model_dir = os.path.join(params.output_dir, "model")
params.model_name = args.model_name
params.seed = args.seed
params.model = args.model
params.patience = args.patience
params.clip_grad = args.clip_grad
params.batch_size = args.batch_size
params.n_epochs = args.n_epochs
params.lr = args.lr
params.k_folds = 1
setSeeds(args.seed)
logfile_path = os.path.join(params.output_dir, args.logfile_name)
set_logger(logfile_path)
optuna.logging.get_logger("optuna").addHandler(logger.handlers[-1])
preprocess = Preprocess(params)
preprocess.load_train_data(params.file_name)
data = preprocess.get_train_data()
storage_name = f"{args.storage_name}.db"
study: Study = optuna.create_study(
study_name=args.study_name,
storage=f"sqlite:///{os.path.join(params.output_dir, storage_name)}",
load_if_exists=True,
direction="maximize",
)
try:
study.optimize(
partial(objective, params=params, hp_params=hp_params, data=data),
n_trials=args.n_trials,
)
except KeyboardInterrupt:
logger.info("Stop tuning.")
all_trials = sorted(
study.trials, key=lambda x: x.value if x.value else 0, reverse=True
)
best_trial = all_trials[0]
best_exp_num = best_trial.number
best_score = best_trial.value
best_params = best_trial.params
logger.info(f"best_exp_num: {best_exp_num}")
logger.info(f"best_score: {best_score}")
logger.info(f"best_params:\n{best_params}")
if __name__ == "__main__":
main()