Last active
January 15, 2021 07:12
-
-
Save hvy/67a279e843a43a1cfc04c2eb92ea605d to your computer and use it in GitHub Desktop.
Optuna storage benchmark script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import math | |
import time | |
import sqlalchemy | |
import optuna | |
class Profile: | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
self.end = time.time() | |
def get(self): | |
return self.end - self.start | |
def build_objective_func(n_param): | |
def objective(trial): | |
return sum( | |
[ | |
math.sin(trial.suggest_float("param-{}".format(i), 0, math.pi * 2)) | |
for i in range(n_param) | |
] | |
) | |
return objective | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("mysql_user", type=str) | |
parser.add_argument("mysql_host", type=str) | |
parser.add_argument("--n-params", type=int, nargs="+", default=[1, 2, 4, 8, 16, 32]) | |
parser.add_argument("--n-trials", type=int, nargs="+", default=[1, 10, 100, 1000]) | |
args = parser.parse_args() | |
storage_str = "mysql+pymysql://{}@{}/".format(args.mysql_user, args.mysql_host) | |
n_params = args.n_params | |
n_trials = args.n_trials | |
optuna.logging.set_verbosity(optuna.logging.CRITICAL) | |
print(f"| #params | #trials | time(sec) | time/trial(sec) |") | |
print(f"| ------- | ------- | --------- | --------------- |") | |
for n_param in n_params: | |
for n_trial in n_trials: | |
engine = sqlalchemy.create_engine(storage_str) | |
conn = engine.connect() | |
conn.execute("commit") | |
database_str = "profile_storage_t{}_p{}".format(n_trial, n_param) | |
try: | |
conn.execute("drop database {}".format(database_str)) | |
except Exception: | |
pass | |
conn.execute("create database {}".format(database_str)) | |
conn.close() | |
storage = optuna.storages.get_storage(storage_str + database_str) | |
study = optuna.create_study(storage=storage, sampler=optuna.samplers.RandomSampler()) | |
with Profile() as prof: | |
study.optimize( | |
build_objective_func(n_param), | |
n_trials=n_trial, | |
gc_after_trial=False, | |
) | |
print(f"| {n_param} | {n_trial} | {prof.get():.2f} | {prof.get() / n_trial:.3f} |") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
master
Flushing after
_CachedStorage.set_trial_param