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

问题描述 投票:0回答:1

我在skopt软件包中使用了带gp_minimize的贝叶斯优化和带dummy_minimize的随机搜索。

我想通过scikit-opt中的load选项保存两个模型:

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

据我所知,应该可以在下面的行中加载模型:

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

不幸的是,这引发了一个错误:

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

如何正确加载这些模型?

我想加载它们并通过plot_convergence进行绘制。

先谢谢您!

亲切的问候

python scikit-learn load bayesian hyperparameters
1个回答
0
投票

在调用load之前,需要导入在腌制的模型中使用的所有库和其他类定义。

例如,如果您在调用转储的同一函数内(紧接转储之后)调用load,则可能不会收到此错误,因为库已被加载。因此,它可以识别已加载对象中的属性。

© www.soinside.com 2019 - 2024. All rights reserved.