-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
181 lines (127 loc) · 5.41 KB
/
train.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
本檔案包含了所有和「訓練」有關的函式。
詳細功能與輸入、輸出請見各函式的 docstring。
"""
from catboost import cv
from contextlib import redirect_stderr, redirect_stdout
from io import StringIO
from logging import getLogger
from optuna import Trial, create_study
from os.path import join
from pickle import dump
from sys import stderr, stdout
from typing import Any, Dict
from initialize import initialize_model, initialize_model_parameters
logger = getLogger(name=__name__)
def _cv_train_with_optuna(trial: Trial, params: Dict[str, Any]) -> float:
""" Do once cross validation training with Optuna.
Args:
trial (Trial): The trial object (Optuna required).
params (Dict[str, Any]): The parameters for training (Not equal to the model_params).
Returns:
float: The best test score.
"""
logger.info(msg=f"Cross validation training with Optuna has been started.")
model_params = initialize_model_parameters(trial=trial)
eval_metric = model_params["eval_metric"]
loss_func_name = model_params["loss_function"]
train_data = params["train_data"]
scores = cv(pool=train_data, params=model_params, fold_count=5)
best_iter = scores[f"test-{eval_metric}-mean"].idxmax()
best_test_loss = scores[f"test-{loss_func_name}-mean"][best_iter]
best_test_score = scores[f"test-{eval_metric}-mean"][best_iter]
best_train_loss = scores[f"train-{loss_func_name}-mean"][best_iter]
best_train_score = scores[f"train-{eval_metric}-mean"][best_iter]
logger.info(msg=f"Best Iteration: {best_iter}")
logger.info(msg=f"Best Iteration's testing loss: {best_test_loss}")
logger.info(msg=f"Best Iteration's testing score: {best_test_score}")
logger.info(msg=f"Best Iteration's training loss: {best_train_loss}")
logger.info(msg=f"Best Iteration's training score: {best_train_score}")
logger.info(
msg=f"Cross validation training with Optuna has been finished.")
return best_test_score
def _train_with_optuna(trial: Trial, params: Dict[str, Any]) -> float:
""" Do once training with Optuna.
Args:
trial (Trial): The trial object (Optuna required).
params (Dict[str, Any]): The parameters for training (Not equal to the model_params).
Returns:
float: The best training loss or validating F1.
"""
logger.info(msg=f"Training with Optuna has been started.")
model_params = initialize_model_parameters(trial=trial)
model = initialize_model(configs=None, model_params=model_params)
train_data = params["train_data"]
# validate_data = params["validate_data"]
model.fit(
train_data,
# use_best_model=True,
# eval_set=validate_data,
log_cout=stdout,
log_cerr=stderr)
params["model"] = model
# f1 = model.get_best_score()["validation"]["F1"]
loss = model.get_best_score()["learn"]["Logloss"]
# logger.info(msg=f"The best validation F1: {f1}")
logger.info(msg=f"The best training loss: {loss}")
# logger.info(msg=f"The model parameters: \n{model.get_all_params()}")
logger.info(msg=f"Training with Optuna has been finished.")
# return f1
return loss
def cv_train_with_optuna(params: Dict[str, Any]):
""" Do full cross validation training with Optuna.
Args:
params (Dict[str, Any]): The parameters for training.
"""
study = create_study(direction="maximize")
study.optimize(
func=lambda trial: _cv_train_with_optuna(trial=trial, params=params),
n_trials=30)
trial = study.best_trial
file = join("outputs", "studies",
f"{params['version']}_cv_optuna_study.pkl")
with open(file=file, mode="wb") as f:
dump(obj=study, file=f)
f.close()
model_params = initialize_model_parameters(trial=None)
logger.info(msg=f"The best trial: {trial.value}")
logger.info(msg="The best trial params: ")
for key, value in trial.params.items():
model_params[key] = value
logger.info(f"{key}: {value}")
params["model"] = initialize_model(configs=None, model_params=model_params)
train(params=params)
def train(params: Dict[str, Any]):
""" Do once training.
Args:
params (Dict[str, Any]): The parameters for training.
"""
fit_log = StringIO()
logger.info(msg=f"Training has been started.")
model = params["model"]
train_data = params["train_data"]
with redirect_stderr(new_target=fit_log), redirect_stdout(
new_target=fit_log):
model.fit(train_data, log_cout=stdout, log_cerr=stderr)
params["model"] = model
logger.info(msg=f"The training log: \n{fit_log.getvalue()}")
logger.info(msg=f"Training has been finished.")
def train_with_optuna(params: Dict[str, Any]):
""" Do full training with Optuna.
Args:
params (Dict[str, Any]): The parameters for training.
"""
study = create_study(direction="maximize")
study.optimize(
func=lambda trial: _train_with_optuna(trial=trial, params=params),
# n_trials=30)
n_trials=5)
trial = study.best_trial
file = join("outputs", "studies", f"{params['version']}_optuna_study.pkl")
with open(file=file, mode="wb") as f:
dump(obj=study, file=f)
f.close()
logger.info(msg=f"The best trial: {trial.value}")
logger.info(msg="The best trial params: ")
for key, value in trial.params.items():
logger.info(f"{key}: {value}")