codes.tune package#
Submodules#
codes.tune.evaluate_study module#
- codes.tune.evaluate_study.load_model_test_losses(model_path)#
Load the test losses from the model checkpoint.
- Parameters:
model_path (str) – Path to the model checkpoint.
- Returns:
Test losses.
- Return type:
np.ndarray
- codes.tune.evaluate_study.load_study_config(study_name)#
Load the YAML config used by the study (optuna_config.yaml).
- Return type:
dict
- codes.tune.evaluate_study.main()#
Main function to evaluate an Optuna study and its top models. Usually, viewing the study database with Optuna Dashboard is more informative.
- codes.tune.evaluate_study.moving_average(data, window_size)#
Compute the moving average of a 1D array.
- Parameters:
data (np.ndarray) – 1D array to compute the moving average.
window_size (int) – Size of the window for the moving average.
- Returns:
Moving average of the input data.
- Return type:
np.ndarray
- Raises:
ValueError – If the window size is not a positive integer.
- codes.tune.evaluate_study.parse_arguments()#
Parse command-line arguments.
- codes.tune.evaluate_study.plot_test_losses(test_losses, labels, study_name, window_size=5)#
Plot the test losses of the top models.
- Parameters:
test_losses (list[np.ndarray]) – List of test losses.
labels (list[str]) – List of labels for the test losses.
study_name (str) – Name of the study.
window_size (int, optional) – Size of the window for the moving average. Defaults to 5.
- Return type:
None
codes.tune.evaluate_tuning module#
- codes.tune.evaluate_tuning.evaluate_tuning(study_prefix, top_n=10, storage_name='optuna_db')#
For all surrogate studies named ‘<study_prefix>_<surrogate>’, plot the top_n test-loss trajectories.
- Return type:
None
- codes.tune.evaluate_tuning.get_best_trials(study, top_n)#
Return trial numbers of best ‘top_n’ trials. Single-objective: lowest value. Multi-objective: Pareto front; if fewer than top_n, pad with closest-to-origin among the rest.
- Return type:
list
[int
]
- codes.tune.evaluate_tuning.load_loss_history(model_path)#
Load loss histories from a saved model file (.pth). Returns (train_loss, test_loss, n_epochs).
- Return type:
tuple
[ndarray
,ndarray
,int
]
- codes.tune.evaluate_tuning.main()#
- codes.tune.evaluate_tuning.parse_args()#
- codes.tune.evaluate_tuning.plot_losses(loss_histories, epochs, labels, title='Losses', save=False, out_dir=None, mode='')#
Plot multiple loss trajectories on a log scale.
codes.tune.optuna_fcts module#
- codes.tune.optuna_fcts.create_objective(config, study_name, device_queue)#
Create the objective function for Optuna.
- Parameters:
config (dict) – Configuration dictionary.
study_name (str) – Name of the study.
device_queue (queue.Queue) – Queue of available devices.
- Returns:
Objective function for Optuna.
- Return type:
function
- codes.tune.optuna_fcts.load_yaml_config(config_path)#
Load a YAML configuration file.
- Parameters:
config_path (str) – Path to the YAML configuration file.
- Returns:
Configuration dictionary.
- Return type:
dict
- codes.tune.optuna_fcts.make_optuna_params(trial, optuna_params)#
- Return type:
dict
- codes.tune.optuna_fcts.maybe_set_runtime_threshold(study, warmup_target, include_pruned=False)#
- Return type:
None
- codes.tune.optuna_fcts.training_run(trial, device, slot_id, config, study_name)#
Run the training for a single Optuna trial and return the loss. In multi-objective mode, also returns the mean inference time.
- Parameters:
trial (optuna.Trial) – Optuna trial object.
device (str) – Device to run the training on.
slot_id (int) – Slot ID for the position of the progress bar.
config (dict) – Configuration dictionary.
study_name (str) – Name of the study.
- Returns:
Loss value in single objective mode. tuple[float, float]: (loss, mean_inference_time) in multi objective mode.
- Return type:
float
codes.tune.postgres_fcts module#
- codes.tune.postgres_fcts.initialize_optuna_database(config, study_folder_name)#
- Return type:
str
codes.tune.tune_utils module#
- codes.tune.tune_utils.build_study_names(config, main_study_name)#
- Return type:
list
[str
]
- codes.tune.tune_utils.copy_config(src, dst_folder)#
- Return type:
None
- codes.tune.tune_utils.delete_studies_if_requested(config, main_study_name, db_url)#
Delete studies if user overwrote run folder.
- codes.tune.tune_utils.prepare_workspace(master_cfg_path, config)#
- Return type:
None
- codes.tune.tune_utils.yes_no(prompt, default=False)#
Simple Y/N prompt. default=False -> [y/N], True -> [Y/n].
- Return type:
bool
Module contents#
- codes.tune.build_study_names(config, main_study_name)#
- Return type:
list
[str
]
- codes.tune.copy_config(src, dst_folder)#
- Return type:
None
- codes.tune.create_objective(config, study_name, device_queue)#
Create the objective function for Optuna.
- Parameters:
config (dict) – Configuration dictionary.
study_name (str) – Name of the study.
device_queue (queue.Queue) – Queue of available devices.
- Returns:
Objective function for Optuna.
- Return type:
function
- codes.tune.delete_studies_if_requested(config, main_study_name, db_url)#
Delete studies if user overwrote run folder.
- codes.tune.initialize_optuna_database(config, study_folder_name)#
- Return type:
str
- codes.tune.load_model_test_losses(model_path)#
Load the test losses from the model checkpoint.
- Parameters:
model_path (str) – Path to the model checkpoint.
- Returns:
Test losses.
- Return type:
np.ndarray
- codes.tune.load_study_config(study_name)#
Load the YAML config used by the study (optuna_config.yaml).
- Return type:
dict
- codes.tune.load_yaml_config(config_path)#
Load a YAML configuration file.
- Parameters:
config_path (str) – Path to the YAML configuration file.
- Returns:
Configuration dictionary.
- Return type:
dict
- codes.tune.make_optuna_params(trial, optuna_params)#
- Return type:
dict
- codes.tune.maybe_set_runtime_threshold(study, warmup_target, include_pruned=False)#
- Return type:
None
- codes.tune.moving_average(data, window_size)#
Compute the moving average of a 1D array.
- Parameters:
data (np.ndarray) – 1D array to compute the moving average.
window_size (int) – Size of the window for the moving average.
- Returns:
Moving average of the input data.
- Return type:
np.ndarray
- Raises:
ValueError – If the window size is not a positive integer.
- codes.tune.plot_test_losses(test_losses, labels, study_name, window_size=5)#
Plot the test losses of the top models.
- Parameters:
test_losses (list[np.ndarray]) – List of test losses.
labels (list[str]) – List of labels for the test losses.
study_name (str) – Name of the study.
window_size (int, optional) – Size of the window for the moving average. Defaults to 5.
- Return type:
None
- codes.tune.prepare_workspace(master_cfg_path, config)#
- Return type:
None
- codes.tune.training_run(trial, device, slot_id, config, study_name)#
Run the training for a single Optuna trial and return the loss. In multi-objective mode, also returns the mean inference time.
- Parameters:
trial (optuna.Trial) – Optuna trial object.
device (str) – Device to run the training on.
slot_id (int) – Slot ID for the position of the progress bar.
config (dict) – Configuration dictionary.
study_name (str) – Name of the study.
- Returns:
Loss value in single objective mode. tuple[float, float]: (loss, mean_inference_time) in multi objective mode.
- Return type:
float
- codes.tune.yes_no(prompt, default=False)#
Simple Y/N prompt. default=False -> [y/N], True -> [Y/n].
- Return type:
bool