Last active
November 15, 2023 16:53
-
-
Save toshihikoyanase/7e75b054395ec0a6dbb144a300862f60 to your computer and use it in GitHub Desktop.
Tuning MLP by using Optuna.
This file contains hidden or 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 optuna | |
import sklearn | |
import sklearn.datasets | |
import sklearn.neural_network | |
def objective(trial): | |
# ネットワーク構造の決定 | |
n_layers = trial.suggest_int('n_layers', 1, 4) | |
layers = [] | |
for i in range(n_layers): | |
layers.append(trial.suggest_int(f'n_units_{i}', 1, 100)) | |
# 学習・評価用データの取得 | |
mnist = sklearn.datasets.fetch_mldata('MNIST original') | |
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split( | |
mnist.data, mnist.target) | |
# モデルの学習 | |
clf = sklearn.neural_network.MLPClassifier(hidden_layer_sizes=tuple(layers)) | |
clf.fit(x_train, y_train) | |
# 学習したモデルの評価 | |
return clf.score(x_test, y_test) | |
study = optuna.create_study(direction='maximize') | |
study.optimize(objective, n_trials=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment