简体   繁体   English

将 optuna 中的训练模型保存到变量中

[英]saving trained models in optuna to a variable

I have a project in which I do several optuna studies each having around 50 trials.我有一个项目,我在其中进行了几项 optuna 研究,每项研究都有大约 50 次试验。

The optuna documentation suggests saving each model to a file for later use on this FAQ section optuna 文档建议将每个 model 保存到一个文件中,以便以后在此常见问题解答部分中使用

What I want is to have all the best models of different studies in a python list.我想要的是在 python 列表中拥有所有不同研究的最佳模型。 How is that possible?这怎么可能?

This is somewhat similar to my code:这有点类似于我的代码:

def objective(trial, n_epochs, batch_size):
     params = {
              'learning_rate': trial.suggest_loguniform('learning_rate', 1e-5, 1e-1),
              'optimizer': trial.suggest_categorical("optimizer", ["Adam", "RMSprop", "SGD"]),
              'n_epochs': n_epochs,
              'batch_size': batch_size
              }
     model = clp_network(trial)
     accuracy, model = train_and_evaluate(params, model, trial) # THIS LINE
     return accuracy
     for i in range(50):
           study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(), pruner=optuna.pruners.MedianPruner())
           study.optimize(lambda trial: objective(trial, e, b), n_trials=50, show_progress_bar=True)

I would like to either save the model variable in the line marked THIS LINE , or somehow get the best model as a variable from the study.我想将model变量保存在标记为THIS LINE的行中,或者以某种方式从研究中获得最好的 model 作为变量。

The easiest way is to define a global variable to store a model for each trial as follows:最简单的方法是定义一个全局变量来存储每个试验的 model,如下所示:

import optuna
from collections import defaultdict


models = defaultdict(dict)

def objective(t):
    model = t.suggest_int("x", 0, 100)
    models[t.study.study_name][t.number] = model
    
    return model

for _ in range(10):
    s = optuna.create_study()
    s.optimize(objective, n_trials=10)

However I reckon this approach is not scalable in terms of memory space, so I'd suggest removing non-best models after each optimize call or saving models on an external file as mentioned in Optuna's FAQ.但是我认为这种方法在 memory 空间方面不可扩展,因此我建议在每次optimize调用后删除非最佳模型或将模型保存在 Optuna 的常见问题解答中提到的外部文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM