无法产生Optuna结果Keras

发布于 2025-01-30 18:45:49 字数 2267 浏览 3 评论 0原文

我已经创建了一个模型,并用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)

如何处理此问题

I have created a model and tuned with 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]

Tuned it this way

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))

Now let say it tuned and give me some parameters and i put them in architecture and train model again using keras fit function, it would not reproduce the results.

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()

In the start of notebook i have put following lines

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)

How can i handle this issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2025-02-06 18:45:49

要解决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-how-can-can-i-i--i-obtain-reproproducible-optimization-results-results

To fix optuna's result, we need to replace

study = optuna.create_study(direction="maximize")

with

study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(seed=seed))

See https://optuna.readthedocs.io/en/stable/faq.html#how-can-i-obtain-reproducible-optimization-results

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文