Skip to content

Commit d04be01

Browse files
authored
Merge branch 'optuna:main' into Add-Comet-Example
2 parents e4af51e + 70be131 commit d04be01

File tree

7 files changed

+24
-22
lines changed

7 files changed

+24
-22
lines changed

.github/workflows/fastai.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: ['3.9', '3.10', '3.11', '3.12']
17+
# TODO(c-bata): Add Python 3.9 here after fixing https://github.com/optuna/optuna-examples/issues/307
18+
python-version: ['3.10', '3.11', '3.12']
1819

1920
steps:
2021
- uses: actions/checkout@v4

.github/workflows/pytorch.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ jobs:
7777
python pytorch/pytorch_lightning_ddp.py -p
7878
env:
7979
OMP_NUM_THREADS: 1
80+
- name: Run skorch example
81+
run: |
82+
python pytorch/skorch_simple.py
83+
env:
84+
OMP_NUM_THREADS: 1

keras/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
keras
22
numpy<2.0.0 # TODO(nabe): Remove this constraint once this issue is resolved. https://github.com/numpy/numpy/issues/26710
33
optuna
4-
tensorflow-cpu
4+
tensorflow-cpu<2.19.0 # TODO(y0z): remove the version constraint <2.19.0 after resolve the CI issue (cf. #304)
55
tensorflow-datasets

pytorch/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mpi4py
2+
pandas
23
plotly
34
pytorch-ignite
45
lightning

pytorch/skorch_simple.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
"""
22
Optuna example that optimizes multi-layer perceptrons using skorch.
33
4-
In this example, we optimize the validation accuracy of hand-written digit recognition using
5-
skorch and MNIST. We optimize the neural network architecture. As it is too time
4+
In this example, we optimize the validation accuracy of hand-written digit recognition
5+
using skorch and MNIST. We optimize the neural network architecture. As it is too time
66
consuming to use the whole MNIST dataset, we here use a small subset of it.
77
8-
You can run this example as follows, pruning can be turned on and off with the `--pruning`
9-
argument.
8+
You can run this example as follows, pruning can be turned on and off with the
9+
`--pruning` argument.
1010
$ python skorch_simple.py [--pruning]
1111
1212
"""
1313

1414
import argparse
15-
import urllib
1615

1716
import numpy as np
1817
import optuna
1918
from optuna.integration import SkorchPruningCallback
19+
import pandas as pd
2020
import skorch
2121
import torch
2222
import torch.nn as nn
@@ -27,22 +27,15 @@
2727
from sklearn.model_selection import train_test_split
2828

2929

30-
# Register a global custom opener to avoid HTTP Error 403: Forbidden when downloading MNIST.
31-
# This is a temporary fix until torchvision v0.9 is released.
32-
opener = urllib.request.build_opener()
33-
opener.addheaders = [("User-agent", "Mozilla/5.0")]
34-
urllib.request.install_opener(opener)
35-
36-
3730
SUBSET_RATIO = 0.4
3831

3932
mnist = fetch_openml("mnist_784", cache=False)
4033

41-
X = mnist.data.astype("float32")
34+
X = pd.DataFrame(mnist.data)
4235
y = mnist.target.astype("int64")
4336
indices = np.random.permutation(len(X))
4437
N = int(len(X) * SUBSET_RATIO)
45-
X = X[indices][:N]
38+
X = X.iloc[indices][:N].astype(np.float32)
4639
y = y[indices][:N]
4740

4841
X /= 255.0
@@ -72,6 +65,8 @@ def __init__(self, trial: optuna.Trial) -> None:
7265
self.model = nn.Sequential(*layers)
7366

7467
def forward(self, x):
68+
if isinstance(x, dict):
69+
x = x["data"]
7570
return F.softmax(self.model(x), dim=-1)
7671

7772

@@ -84,9 +79,9 @@ def objective(trial: optuna.Trial) -> float:
8479
callbacks=[SkorchPruningCallback(trial, "valid_acc")],
8580
)
8681

87-
net.fit(X_train, y_train)
82+
net.fit(X_train.to_numpy().astype(np.float32), y_train)
8883

89-
return accuracy_score(y_test, net.predict(X_test))
84+
return accuracy_score(y_test.to_numpy(), net.predict(X_test.to_numpy().astype(np.float32)))
9085

9186

9287
if __name__ == "__main__":
@@ -110,8 +105,8 @@ def objective(trial: optuna.Trial) -> float:
110105
print("Best trial:")
111106
trial = study.best_trial
112107

113-
print(" Value: {}".format(trial.value))
108+
print("Value: {}".format(trial.value))
114109

115-
print(" Params: ")
110+
print("Params: ")
116111
for key, value in trial.params.items():
117112
print(" {}: {}".format(key, value))

tensorflow/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
tensorflow-estimator
2-
tensorflow-cpu>=2.11.0
2+
tensorflow-cpu>=2.11.0,<2.19.0 # TODO(y0z): remove the version constraint <2.19.0 after resolve the CI issue (cf. #304)
33
tensorflow-datasets
44
optuna

tfkeras/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
optuna
2-
tensorflow-cpu
2+
tensorflow-cpu<2.19.0 # TODO(y0z): remove the version constraint <2.19.0 after resolve the CI issue (cf. #304)
33
tensorflow-datasets

0 commit comments

Comments
 (0)