我已经创建了一个模型并使用optuna进行了调优
def mymodel(hp):
clear_session()
imageModel = Sequential()
imageModel.add(Conv2D(hp.suggest_categorical("kernel1", [32,64,128]),
hp.suggest_categorical("filter1", [3,5]),
activation='relu', padding='valid', strides=2,input_shape=(300, 300, 1)))
imageModel.add(MaxPool2D(pool_size=2, strides=2))
imageModel.add(Flatten())
imageModel.add(Dense(hp.suggest_categorical("dense", [32,64,128]), activation='relu'))
imageModel.add(Dense(1, activation='sigmoid'))
imageModel.compile(optimizer=RMSprop(),loss='binary_crossentropy',metrics=['accuracy'])
imageModel.fit(XTrain,YTrain,epochs = 20,validation_data=(XVal,YVal),verbose=0)
result=imageModel.evaluate(XTest,YTest)
return result[1]
这样调谐
if __name__ == "__main__":
study = optuna.create_study(direction="maximize")
study.optimize(mymodel, n_trials=100, timeout=6000)
print("Number of finished trials: {}".format(len(study.trials)))
print("Best trial:")
trial = study.best_trial
print(" Value: {}".format(trial.value))
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
现在让我们说它调整并给我一些参数,我把它们放在架构和训练模型再次使用keras fit函数,它不会重现结果。
def mymodel():
clear_session()
imageModel = Sequential()
imageModel.add(Conv2D(32,3,activation='relu', padding='valid', strides=2,input_shape=(300, 300, 1)))
imageModel.add(MaxPool2D(pool_size=2, strides=2))
imageModel.add(Flatten())
imageModel.add(Dense(64, activation='relu'))
imageModel.add(Dense(1, activation='sigmoid'))
imageModel.compile(optimizer=RMSprop(),loss='binary_crossentropy',metrics=['accuracy'])
imageModel.fit(XTrain,YTrain,epochs = 20,validation_data=(XVal,YVal),verbose=0)
result=imageModel.evaluate(XTest,YTest)
return result[1]
mymodel()
在笔记本的开头,我放了以下几行
import os
import numpy as np
import tensorflow as tf
import random
seed=0
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
random.seed(seed)
tf.random.set_seed(seed)
我该如何处理这个问题
要修复optuna的结果,我们需要替换
study = optuna.create_study(direction="maximize")
study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(seed=seed))
看到https://optuna.readthedocs.io/en/stable/faq.html how-can-i-obtain-reproducible-optimization-results