Optuna: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(Created page with "{{Draft}} [https://optuna.org/ Optuna] is an automatic hyperparameter optimization (HPO) software framework, particularly designed for machine learning. Please refer to the...")
 
Line 24: Line 24:


# Specify a path in your home, or on project.
# Specify a path in your home, or on project.
OPTUNA_DB=$HOME/${OPTUNA_STUDY}.db
OPTUNA_DB=$HOME/${OPTUNA_STUDY_NAME}.db


# Launch your script, giving it as arguments the database file and the study name
# Launch your script, giving it as arguments the database file and the study name
Line 32: Line 32:
In <code>train.py</code>, create and launch the Optuna study like the following. For the rest of the code, see the [https://optuna.readthedocs.io/en/stable/ Optuna Documentation].
In <code>train.py</code>, create and launch the Optuna study like the following. For the rest of the code, see the [https://optuna.readthedocs.io/en/stable/ Optuna Documentation].


# args.optuna_db and args.optuna_study_name are command line arguments
  study = optuna.create_study(
  study = optuna.create_study(
     storage='sqlite:///' + args.optuna_db,
     storage='sqlite:///' + args.optuna_db,
Line 38: Line 40:
  )
  )
  ...
  ...
  study.optimize(objective, n_trials=1)  # Only execute a single trial at a time
  study.optimize(objective, n_trials=1)  # Only execute a single trial at a time, to avoid wasting compute

Revision as of 20:19, 29 November 2021


This article is a draft

This is not a complete article: This is a draft, a work in progress that is intended to be published into an article, which may or may not be ready for inclusion in the main wiki. It should not necessarily be considered factual or authoritative.




Optuna is an automatic hyperparameter optimization (HPO) software framework, particularly designed for machine learning.

Please refer to the Optuna Documentation for a definition of terms, tutorial, API, etc.

Using Optuna on Compute Canada

Here is a sketch of an SBATCH script for an HPO using Optuna:


File : hpo_with_optuna.sh

#!/bin/bash 
#SBATCH -A def-account
#SBATCH --array 1-N%M   # This will launch N jobs, but only allow M to run in parallel
#SBATCH --time TIME     # Each of the N jobs will have the time limit defined in here.
... other SBATCH arguments ...

# Each trial in the study will be run in a separate job.
# The Optuna study_name has to be set to be able to continue an existing study.
OPTUNA_STUDY_NAME=my_optuna_study1

# Specify a path in your home, or on project.
OPTUNA_DB=$HOME/${OPTUNA_STUDY_NAME}.db

# Launch your script, giving it as arguments the database file and the study name
python train.py --optuna-db $OPTUNA_DB --optuna-study-name $OPTUNA_STUDY_NAME


In train.py, create and launch the Optuna study like the following. For the rest of the code, see the Optuna Documentation.

# args.optuna_db and args.optuna_study_name are command line arguments

study = optuna.create_study(
    storage='sqlite:///' + args.optuna_db,
    study_name=args.optuna_study_name,
    load_if_exists=True
)
...
study.optimize(objective, n_trials=1)  # Only execute a single trial at a time, to avoid wasting compute