Skip to content

Commit fbfb405

Browse files
committed
updating hyperoptplot for new hyperopt keys
1 parent 3ebb674 commit fbfb405

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

validphys2/src/validphys/hyperoptplot.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,18 @@ def parse_statistics(trial):
301301
testing loss
302302
status of the run
303303
"""
304+
hypr_keys = ["hyper_losses_chi2", "hyper_losses_phi2", "hyper_losses_logp", "trvl_losses_chi2exp"]
304305
dict_out = {}
305306
results = trial["result"]
306307
validation_loss = results[KEYWORDS["vl"]]
307308
testing_loss = results[KEYWORDS["tl"]]
308-
# was this a ok run?
309-
ok = bool(results["status"] == "ok")
310309

311-
dict_out[KEYWORDS["good"]] = ok
310+
dict_out[KEYWORDS["good"]] = bool(results["status"] == "ok")
312311
dict_out[KEYWORDS["vl"]] = validation_loss
313312
dict_out[KEYWORDS["tl"]] = testing_loss
314313

315-
# Kfolding information
316-
# average = results["kfold_meta"]["hyper_avg"]
317-
# std = results["kfold_meta"]["hyper_std"]
318-
# dict_out["avg"] = average
319-
# dict_out["std"] = std
320-
dict_out["hlosses"] = results["kfold_meta"]["hyper_losses"]
321-
dict_out["vlosses"] = results["kfold_meta"]["validation_losses"]
314+
for key in hypr_keys: # K-folding information
315+
dict_out[key] = results["kfold_meta"][key]
322316
return dict_out
323317

324318

@@ -344,27 +338,34 @@ def evaluate_trial(trial_dict, validation_multiplier, fail_threshold, loss_targe
344338
"""
345339
Read a trial dictionary and compute the true loss and decide whether the run passes or not
346340
"""
347-
test_f = 1.0 - validation_multiplier
348-
val_loss = float(trial_dict[KEYWORDS["vl"]])
341+
hypr_metric_keys = ["chi2", "phi2", "logp"]
349342
if loss_target == "average":
350-
test_loss = np.array(trial_dict["hlosses"]).mean()
343+
for hypr_key in hypr_metric_keys:
344+
trial_dict[f"hyper_loss_{hypr_key}"] = np.array(
345+
trial_dict[f"hyper_losses_{hypr_key}"]
346+
).mean()
351347
elif loss_target == "best_worst":
352-
test_loss = np.array(trial_dict["hlosses"]).max()
348+
for hypr_key in hypr_metric_keys:
349+
trial_dict[f"hyper_loss_{hypr_key}"] = np.array(
350+
trial_dict[f"hyper_losses_{hypr_key}"]
351+
).max()
353352
elif loss_target == "std":
354-
test_loss = np.array(trial_dict["hlosses"]).std()
355-
loss = val_loss * validation_multiplier + test_loss * test_f
353+
for hypr_key in hypr_metric_keys:
354+
trial_dict[f"hyper_loss_{hypr_key}"] = np.array(
355+
trial_dict[f"hyper_losses_{hypr_key}"]
356+
).std()
357+
else:
358+
raise ValueError(f"Loss target {loss_target} is not valid.")
356359

357-
if (
358-
loss > fail_threshold
359-
or val_loss > fail_threshold
360-
or test_loss > fail_threshold
361-
or np.isnan(loss)
362-
):
363-
trial_dict["good"] = False
364-
# Set the loss an order of magnitude above the result so it shows obviously on the plots
365-
loss *= 10
360+
for hypr_key in hypr_metric_keys:
361+
if np.isnan(trial_dict[f"hyper_loss_{hypr_key}"]):
362+
trial_dict[f"hyper_loss_{hypr_key}"] *= 100
363+
364+
trial_dict["trvl_loss_chi2exp"] = np.array(
365+
trial_dict["trvl_losses_chi2exp"]
366+
).mean()
366367

367-
trial_dict["loss"] = loss
368+
return
368369

369370

370371
def generate_dictionary(
@@ -543,10 +544,6 @@ def hyperopt_dataframe(commandline_args):
543544
# Make into a dataframe and transpose or the plotting code will complain
544545
best_trial = best_trial_series.to_frame().T
545546

546-
log.info("Best setup:")
547-
with pd.option_context("display.max_rows", None, "display.max_columns", None):
548-
log.info(best_trial)
549-
550547
return dataframe, best_trial
551548

552549

@@ -585,8 +582,13 @@ def hyperopt_table(hyperopt_dataframe):
585582
Generates a table containing complete information on all the tested setups that passed the
586583
filters set in the commandline arguments.
587584
"""
585+
drop_keys = ["hyper_losses_chi2", "hyper_losses_phi2", "hyper_losses_logp"]
586+
drop_keys += [f"layer_{idx}" for idx in range(1, 5)]
587+
drop_keys += ["activation_per_layer_0"]
588+
588589
dataframe, _ = hyperopt_dataframe
589-
dataframe = dataframe.sort_values(by=["loss"])
590+
dataframe.drop(columns=drop_keys, inplace=True)
591+
dataframe.sort_values(by=["hyper_loss_chi2"], inplace=True)
590592
return dataframe
591593

592594

0 commit comments

Comments
 (0)