简体   繁体   English

在 sci-kit 中保存和加载模型优化

[英]saving and loading models in sci-kit optimize

I used Bayesian Optimization with gp_minimize and a Random search with dummy_minimize in the skopt package.我在 skopt 包中使用了带有 gp_minimize 的贝叶斯优化和带有 dummy_minimize 的随机搜索。

I wanted to save both models by the load option in scikit-opt:我想通过 scikit-opt 中的 load 选项保存两个模型:

skopt.dump(gp_100, 'gp_100.pkl')

According to my knowledge, it should be possible to load the model with the following line:据我所知,应该可以使用以下行加载模型:

gp_100 = skopt.load('gp_100.pkl')

Unfortunately, this raises an error:不幸的是,这引发了一个错误:

<ipython-input-18-a24ad5907175> in <module>
      1 from skopt import load
----> 2 gpall_1000 = skopt.load('gp_100.pkl')

/opt/conda/lib/python3.7/site-packages/skopt/utils.py in load(filename, **kwargs)
    170         Reconstructed OptimizeResult instance.
    171     """
--> 172     return load_(filename, **kwargs)
    173 
    174 

/opt/conda/lib/python3.7/site-packages/joblib/numpy_pickle.py in load(filename, mmap_mode)
    603                     return load_compatibility(fobj)
    604 
--> 605                 obj = _unpickle(fobj, filename, mmap_mode)
    606 
    607     return obj

/opt/conda/lib/python3.7/site-packages/joblib/numpy_pickle.py in _unpickle(fobj, filename, mmap_mode)
    527     obj = None
    528     try:
--> 529         obj = unpickler.load()
    530         if unpickler.compat_mode:
    531             warnings.warn("The file '%s' has been generated with a "

/opt/conda/lib/python3.7/pickle.py in load(self)
   1083                     raise EOFError
   1084                 assert isinstance(key, bytes_types)
-> 1085                 dispatch[key[0]](self)
   1086         except _Stop as stopinst:
   1087             return stopinst.value

/opt/conda/lib/python3.7/pickle.py in load_global(self)
   1371         module = self.readline()[:-1].decode("utf-8")
   1372         name = self.readline()[:-1].decode("utf-8")
-> 1373         klass = self.find_class(module, name)
   1374         self.append(klass)
   1375     dispatch[GLOBAL[0]] = load_global

/opt/conda/lib/python3.7/pickle.py in find_class(self, module, name)
   1425             return _getattribute(sys.modules[module], name)[0]
   1426         else:
-> 1427             return getattr(sys.modules[module], name)
   1428 
   1429     def load_reduce(self):

AttributeError: module '__main__' has no attribute 'objective'

How to load these models correctly?如何正确加载这些模型?

I want to load them and plot both via plot_convergence.我想加载它们并通过 plot_convergence 绘制它们。

Thank you in advance !先感谢您 !

Kind regards亲切的问候

Before you call load, you need to import all the libraries and other class definitions that were used in the model that was pickled.在调用 load 之前,您需要导入在被pickle 的模型中使用的所有库和其他类定义。

For example, if you call load inside the same function that you call dump (right after the dump), you probably won't get this error because the libraries are already loaded.例如,如果您在调用 dump 的同一函数中调用 load(在 dump 之后),您可能不会收到此错误,因为库已经加载。 And so it can identify the attributes in the object loaded.因此它可以识别加载的对象中的属性。

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

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