|
16 | 16 | from sklearn.metrics import f1_score, precision_score, recall_score, log_loss, accuracy_score |
17 | 17 | from sklearn.model_selection import train_test_split |
18 | 18 | from sklearn.utils import compute_class_weight |
| 19 | +import optuna |
| 20 | +from optuna.samplers import TPESampler |
| 21 | +from optuna.pruners import HyperbandPruner |
19 | 22 |
|
20 | 23 | from experiment.plot import save_plot |
21 | 24 | from experiment.src.data_loader import read_detected_data, read_metadata, join_label, get_y_labels |
|
25 | 28 | from experiment.src.prepare_data import prepare_train_data, data_checksum |
26 | 29 |
|
27 | 30 |
|
| 31 | +def objective(trial, x_train, y_train, x_test, y_test, hp, device): |
| 32 | + params = {} |
| 33 | + for param_name, ((low, high, step), default) in hp.items(): |
| 34 | + params[param_name] = trial.suggest_float(param_name, low, high, step=step) |
| 35 | + |
| 36 | + model = MlModel(*[x.shape for x in x_train], params).to(device) |
| 37 | + optimizer = optim.Adam(model.parameters(), lr=0.001) |
| 38 | + criterion = nn.BCELoss() |
| 39 | + |
| 40 | + dataset = TensorDataset(*[torch.tensor(x, dtype=torch.float32) for x in x_train], |
| 41 | + torch.tensor(y_train, dtype=torch.float32)) |
| 42 | + data_loader = DataLoader(dataset, batch_size=1024, shuffle=True) |
| 43 | + |
| 44 | + model.train() |
| 45 | + for _ in range(5): |
| 46 | + for batch in data_loader: |
| 47 | + x_tensors = [x.to(device) for x in batch[:-1]] |
| 48 | + y_batch = batch[-1].to(device) |
| 49 | + optimizer.zero_grad() |
| 50 | + outputs = model(*x_tensors).squeeze() |
| 51 | + loss = criterion(outputs, y_batch) |
| 52 | + loss.backward() |
| 53 | + optimizer.step() |
| 54 | + |
| 55 | + predictions = model(*[torch.tensor(x, dtype=torch.float32, device=device) |
| 56 | + for x in x_test]).cpu().detach().numpy().ravel() |
| 57 | + val_loss = criterion(y_test, predictions) |
| 58 | + return val_loss |
| 59 | + |
| 60 | + |
28 | 61 | def evaluate_model(thresholds: dict, |
29 | 62 | model: nn.Module, |
30 | 63 | x_data: List[np.ndarray], |
@@ -169,9 +202,16 @@ def main(cred_data_location: str, |
169 | 202 | x_train = [x_train_line, x_train_variable, x_train_value, x_train_features] |
170 | 203 | x_test = [x_test_line, x_test_variable, x_test_value, x_test_features] |
171 | 204 |
|
172 | | - param_kwargs = {k: v[1] for k, v in hp_dict.items()} |
| 205 | + if use_tuner: |
| 206 | + print(f"Start model train with optimization") |
| 207 | + study = optuna.create_study(sampler=TPESampler(), pruner=HyperbandPruner(), direction="minimize") |
| 208 | + study.optimize(lambda trial: objective(trial, x_train, y_train, x_test, y_test, hp_dict, device), n_trials=20) |
| 209 | + param_kwargs = study.best_params |
| 210 | + print(f"Best hyperparameters: {param_kwargs}") |
| 211 | + else: |
| 212 | + param_kwargs = {k: v[1] for k, v in hp_dict.items()} |
173 | 213 |
|
174 | | - print(f"Model is trained with params from dict:{param_kwargs}") |
| 214 | + print(f"Model will be trained using the following params:{param_kwargs}") |
175 | 215 |
|
176 | 216 | # repeat train step to obtain actual history chart |
177 | 217 | ml_model = MlModel(x_full_line.shape, x_full_variable.shape, x_full_value.shape, x_full_features.shape, |
|
0 commit comments