Skip to content

Instantly share code, notes, and snippets.

Example code

import numpy as np
import optuna
import optunahub


def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_float("x", -50, -40)
import numpy as np
from datetime import datetime
import optuna
import cProfile
import time
from optuna.visualization._rank import _get_rank_info
def sphere(trial: optuna.Trial) -> float:
X = np.asarray([trial.suggest_float(f"x{i}", -5, 5) for i in range(10)])
return np.sum(X ** 2)
@c-bata
c-bata / generator.py
Last active October 28, 2023 01:29
PyCon APAC 2023 - ハイパーパラメータ最適化フレームワーク Optunaの最新機能紹介
# Requirements:
# $ pip install optuna optuna-dashboard diffusers transformers accelerate scipy safetensors xformers botorch
#
# Process A: Launch a process that suggest new params and generate images.
# $ python generator.py
#
# Process B: Launch an Optuna Dashboard process.
# $ optuna-dashboard sqlite:///db.sqlite3 --artifact-dir ./artifact
import os
import time
from __future__ import annotations
import os
import shutil
import uuid
import tempfile
from typing import NoReturn
import optuna
import streamlit as st
import sys
import numpy as np
from kurobako import problem
from kurobako.problem import Problem
from typing import List
from typing import Optional
import argparse
import os
import subprocess
def run(args: argparse.Namespace) -> None:
kurobako_cmd = os.path.join(args.path_to_kurobako, "kurobako")
subprocess.run(f"{kurobako_cmd} --version", shell=True)
if not (os.path.exists(args.data_dir) and os.path.isdir(args.data_dir)):
import math
import time
import optuna
import numpy as np
def objective(trial: optuna.Trial, n_params: int) -> float:
return sum([
math.sin(trial.suggest_float('param-{}'.format(i), 0, math.pi * 2))
for i in range(n_params)
@c-bata
c-bata / main.py
Last active October 28, 2023 01:04
W&B 東京Meetup #3 - Optunaを使ったHuman-in-the-loop最適化の紹介
# Requirements:
# $ pip install optuna optuna-dashboard[preferential] diffusers transformers accelerate scipy safetensors xformers
#
# Process A: Launch a process that suggest new params and generate images.
# $ python main.py
#
# Process B: Launch an Optuna Dashboard process.
# $ optuna-dashboard sqlite:///db.sqlite3 --artifact-dir ./artifact
import time
# $ docker run -d --rm --platform linux/amd64 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root -e POSTGRES_DB=optuna --name optuna-postgres postgres:12.10
# $ docker run -it --rm --platform linux/amd64 --network host -v $(pwd):/usr/src python:3.10 bash
# # cd /usr/src
# # pip install -U setuptools pip psycopg2
# # pip install -e .
from __future__ import annotations
import math
# docker run -d --rm -p 3306:3306 -e MYSQL_USER=optuna -e MYSQL_DATABASE=optuna -e MYSQL_PASSWORD=password -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --name optuna-mysql mysql:8.0
from __future__ import annotations
import math
import threading
import time
from sqlalchemy import event
from sqlalchemy.engine.base import Engine