Skip to content

Instantly share code, notes, and snippets.

@toshihikoyanase
Created July 14, 2020 05:21
Show Gist options
  • Save toshihikoyanase/f839e9ab9b2cbba23366b635f12b79da to your computer and use it in GitHub Desktop.
Save toshihikoyanase/f839e9ab9b2cbba23366b635f12b79da to your computer and use it in GitHub Desktop.
Validation code for optuna/optuna PR1514
import logging
import os
import optuna
logging.basicConfig()
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
def f(x):
return (x - 3) ** 2
# Objective function
def optuna_objective(trial):
x = trial.suggest_uniform("x", -10, 10)
y = f(x)
nodename = os.uname().nodename
trial.set_user_attr("nodename", nodename)
print("x=%8.3f, y=%8.3f" % (x, y))
return y
# Entry point
def main():
# Create a study
study = optuna.create_study(
study_name="test_optuna_collision_01",
storage=os.environ["OPTUNA_SQL"], # Set a remote PostgreSQL server
load_if_exists=True,
direction="minimize",
)
# Run optimization
study.optimize(optuna_objective, n_trials=20)
# Visualize the result
print(" # %8s %8s %8s %28s" % ("x", "y", "NODENAME", "STARTED"))
for trial in study.trials:
try:
x = "%8.3f" % trial.params["x"]
y = "%8.3f" % (trial.value or -1)
if trial.value is None:
print("NO_VALUE: ", trial.number)
nodename = trial.user_attrs["nodename"]
started = trial.datetime_start
except (KeyError, AttributeError):
x = ""
y = ""
nodename = ""
started = ""
print("%3d: %s %s %8s %28s" % (trial.number, x, y, nodename, started))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment