python optuna-trial.py
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python optuna-trial.py相关的知识,希望对你有一定的参考价值。
import optuna
from keras import models
from keras import optimizers
from keras import layers
from keras.datasets import boston_housing
# データ読み込み
(train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()
mean = train_data.mean(axis=0)
std = train_data.std(axis=0)
# 正規化
train_data -= mean
train_data /= std
test_data -= mean
test_data /= std
# モデル作成
def create_model(trial):
n_layers = trial.suggest_int('n_layers', 1, 10)
lr = trial.suggest_loguniform('learning_rate', 1e-5, 1e-2)
model = models.Sequential()
for i in range(n_layers):
n_units = trial.suggest_int("n_units{}".format(i), 20, 100)
if i == 0:
model.add(layers.Dense(activation='relu', units=n_units, input_shape=(train_data.shape[1],)))
else:
model.add(layers.Dense(activation='relu', units=n_units))
model.add(layers.Dense(1))
model.compile(optimizers.rmsprop(lr=lr), loss='mse', metrics=['mae'])
return model
# 最適化対象
def objective(trial):
model = create_model(trial)
batch_size = trial.suggest_int('batch_size', 32, 128)
epochs = trial.suggest_int('epochs', 50, 200)
model.fit(train_data, train_targets, batch_size=batch_size, epochs=epochs, verbose=False)
result = model.evaluate(test_data, test_targets, verbose=False)
return result[0]
if __name__ == '__main__':
study = optuna.create_study()
study.optimize(objective, n_trials=100)
print(study.best_value)
print(study.best_params)
以上是关于python optuna-trial.py的主要内容,如果未能解决你的问题,请参考以下文章
001--python全栈--基础知识--python安装
Python代写,Python作业代写,代写Python,代做Python
Python开发
Python,python,python
Python 介绍
Python学习之认识python