Skip to content

Commit

Permalink
Merge pull request #266 from nabenabe0928/code-fix/organize-dir-struc…
Browse files Browse the repository at this point in the history
…ture

Organize the directory structure
  • Loading branch information
eukaryo authored Jul 9, 2024
2 parents 715b6b6 + f131325 commit dacba8a
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 19 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ jobs:
pip install -r requirements.txt
- name: Run examples
run: |
python max_trials_callback.py
python enqueue_trial.py
python quadratic_simple.py
python simple_pruning.py
python -m py_compile rapids_simple.py # due to difficulty importing, compile only
jupyter nbconvert --to notebook --execute quickstart.ipynb
env:
OMP_NUM_THREADS: 1
42 changes: 42 additions & 0 deletions .github/workflows/basic_and_faq_usages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: basic_and_faq_usages

on:
schedule:
- cron: '0 15 * * *'
pull_request:
paths:
- 'basic_and_faq_usages/**'
- '.github/workflows/basic_and_faq_usages.yml'

jobs:
examples:
if: (github.event_name == 'schedule' && github.repository == 'optuna/optuna-examples') || (github.event_name != 'schedule')
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v4
- name: setup-python${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install (Python)
run: |
python -m pip install --upgrade pip
pip install --progress-bar off -U setuptools
pip install git+https://github.com/optuna/optuna.git
python -c 'import optuna'
pip install -r basic_and_faq_usages/requirements.txt
- name: Run examples
run: |
python basic_and_faq_usages/max_trials_callback.py
python basic_and_faq_usages/enqueue_trial.py
python basic_and_faq_usages/quadratic_simple.py
python basic_and_faq_usages/simple_pruning.py
python basic_and_faq_usages/quadratic_simple_constraint.py
python basic_and_faq_usages/quadratic_simple_multi_objective.py
env:
OMP_NUM_THREADS: 1
36 changes: 36 additions & 0 deletions .github/workflows/rapids.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: base

on:
schedule:
- cron: '0 15 * * *'
pull_request:
paths:
- 'rapids/**'
- '.github/workflows/rapids.yml'

jobs:
examples:
if: (github.event_name == 'schedule' && github.repository == 'optuna/optuna-examples') || (github.event_name != 'schedule')
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v4
- name: setup-python${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install (Python)
run: |
python -m pip install --upgrade pip
pip install --progress-bar off -U setuptools
pip install git+https://github.com/optuna/optuna.git
python -c 'import optuna'
- name: Run examples
run: |
python -m py_compile rapids/rapids_simple.py # due to difficulty in importing, compile only
env:
OMP_NUM_THREADS: 1
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ The examples below provide codeblocks similar to the example above for various d

### Simple Black-box Optimization

* [Quadratic function](./quadratic_simple.py)
* [Quadratic multi-objective function](./multi_objective/quadratic_simple.py)
* [Quadratic function with constraints](./quadratic_simple_constraint.py)
* [Quadratic function](./basic_and_faq_usages/quadratic_simple.py)
* [Quadratic multi-objective function](./basic_and_faq_usages/quadratic_simple_multi_objective.py)
* [Quadratic function with constraints](./basic_and_faq_usages/quadratic_simple_constraint.py)

### Examples with ML Libraries

Expand All @@ -47,7 +47,7 @@ The examples below provide codeblocks similar to the example above for various d
* [PyTorch Ignite](./pytorch/pytorch_ignite_simple.py)
* [PyTorch Lightning](./pytorch/pytorch_lightning_simple.py)
* [PyTorch Lightning (DDP)](./pytorch/pytorch_lightning_ddp.py)
* [RAPIDS](./rapids_simple.py)
* [RAPIDS](./rapids/rapids_simple.py)
* [Scikit-learn](./sklearn/sklearn_simple.py)
* [Scikit-learn OptunaSearchCV](./sklearn/sklearn_optuna_search_cv_simple.py)
* [Scikit-image](./skimage/skimage_lbp_simple.py)
Expand All @@ -72,7 +72,7 @@ The following example demonstrates how to implement an objective function that u

The following example demonstrates how to implement pruning logic with Optuna.

* [Simple pruning (scikit-learn)](./simple_pruning.py)
* [Simple pruning (scikit-learn)](./basic_and_faq_usages/simple_pruning.py)

In addition, integration modules are available for the following libraries, providing simpler interfaces to utilize pruning.

Expand Down Expand Up @@ -116,7 +116,7 @@ In addition, integration modules are available for the following libraries, prov

### An example to enqueue trials with given parameter values

* [Enqueuing trials with given parameters](./enqueue_trial.py)
* [Enqueuing trials with given parameters](./basic_and_faq_usages/enqueue_trial.py)

### Examples of aim

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def constraints(trial):
else:
best_trial_number = sorted(feasible_trial_numbers, key=lambda i: study.trials[i].value)[0]
best_trial = study.trials[best_trial_number]
print(f"Best trial was found at Trial#{best_trial_number}")
print(f" Params: {best_trial.params}")
print(f" Value: {best_trial.value}")
print("Best trial was found at Trial#{}".format(best_trial_number))
print(" Params: {}".format(best_trial.params))
print(" Value: {}".format(best_trial.value))
c1, c2 = best_trial.system_attrs["constraints"]
print(f" Constraints: {c1=}, {c2=}")
print(" Constraints: c1={}, c2={}".format(c1, c2))
34 changes: 34 additions & 0 deletions basic_and_faq_usages/quadratic_simple_multi_objective.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Optuna example that optimizes simple quadratic functions.
In this example, we optimize two objective values.
Unlike single-objective optimization, an optimization gives a trade-off between two objectives.
As a result, we get the best trade-offs between two objectives, a.k.a Pareto solutions.
"""

import optuna


# Define two objective functions.
# We would like to minimize f1 and maximize f2.
def objective(trial):
x = trial.suggest_float("x", -100, 100)
y = trial.suggest_categorical("y", [-1, 0, 1])
f1 = x**2 + y
f2 = -((x - 2) ** 2 + y)
return f1, f2


if __name__ == "__main__":
# We minimize the first objective value and maximize the second objective value.
study = optuna.create_study(directions=["minimize", "maximize"])
study.optimize(objective, n_trials=500, timeout=1)

print("Number of finished trials: ", len(study.trials))

for i, best_trial in enumerate(study.best_trials):
print("The {}-th Pareto solution was found at Trial#{}.".format(i, best_trial.number))
print(" Params: {}".format(best_trial.params))
f1, f2 = best_trial.values
print(" Values: f1={}, f2={}".format(f1, f2))
3 changes: 3 additions & 0 deletions basic_and_faq_usages/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
optuna
scikit-learn>=0.19.0
pandas
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions rapids/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
optuna
scikit-learn>=0.19.0
cudf
cuml
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jupyter # used in quickstart.ipynb
pandas # used in max_trials_callback.py
plotly # used in quickstart.ipynb
scikit-learn # used in enqueue_trial.py and quickstart.ipynb
# Used in quickstart.ipynb
jupyter
plotly
scikit-learn

0 comments on commit dacba8a

Please sign in to comment.