【问题标题】:Serializing a h2o model with pickle - python用pickle-python序列化一个h2o模型
【发布时间】:2018-06-01 11:23:55
【问题描述】:

我目前正在尝试将 h2o gb 模型序列化为 pickle 对象并重用它。由于一些限制,我不能使用http://docs.h2o.ai/h2o/latest-stable/h2o-docs/productionizing.html 给出的默认方法或 POJO 和 MOJO。模型被腌制,但是在 unpickling(pickle.loads) 时,出现以下错误 -

__new__() missing 1 required positional argument: 'keyvals'

以下代码供参考-

import h2o as h2o
import pickle as pickle
from h2o.estimators.gbm import H2OGradientBoostingEstimator
h2o.init()

csv_url = "https://h2o-public-test-data.s3.amazonaws.com/smalldata/wisc/wisc-diag-breast-cancer-shuffled.csv"
data = h2o.import_file(csv_url)
y = 'diagnosis'
x = data.columns
del x[0:1]
train, test = data.split_frame(ratios=[0.75], seed=1)


model = H2OGradientBoostingEstimator(distribution='bernoulli',
                                ntrees=100,
                                max_depth=4,
                                learn_rate=0.1)
model.train(x=x, y=y, training_frame=train, validation_frame=test)

loaded_model = pickle.loads(saved_model)
perf = loaded_model.model_performance(test)
perf.auc()

我试图了解 pickle 模块并进行一些更改,但没有成功。任何解决方法/帮助将不胜感激。谢谢。

【问题讨论】:

  • 我也在寻找一种方法来挑选 h2o 模型。你找到怎么做了吗?我们需要一些跨不同 ml 包的保存/加载标准

标签: python serialization machine-learning h2o


【解决方案1】:

你不用pickle,h2o提供了自己的持久化方法:http://docs.h2o.ai/h2o/latest-stable/h2o-docs/save-and-load-model.html

# build the model
model = H2ODeepLearningEstimator(params)
model.train(params)

# save the model
model_path = h2o.save_model(model=model, path="/tmp/mymodel", force=True)

print(model_path)
# outputs: /tmp/mymodel/DeepLearning_model_python_1441838096933

# load the model
saved_model = h2o.load_model(model_path)

但请注意,即使是最细微的版本更改,持久化模型也兼容,即如果您在 3.18.0.1 中训练并保存模型,您将不会能够在 3.18.0.2 中加载它

【讨论】:

  • 这种方法以前对我有用。不过谢谢。如果可能的话,希望通过 pickle 解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 2018-08-12
  • 2017-02-08
相关资源
最近更新 更多