gpt4 book ai didi

python - 如何在 Go 语言中使用经过训练的 Scikit Learn Python 模型?

转载 作者:行者123 更新时间:2023-12-05 04:33:29 24 4
gpt4 key购买 nike

我在 Python 环境中训练了一个 Scikit Learn 模型,我需要用它在 GoLang 中进行推理。您能帮我看看如何在 Python 中导出/保存我的模型,然后再在 GoLang 中使用它吗?

我找到了一个神经网络模型的解决方案,我可以在其中以 ONNX 格式保存 Tensorflow 模型,并使用 GoLang 中的 Onnx-go 加载它。但这是特定于神经网络模型的。但我无法为 scikit-learn 模型弄明白。

最佳答案

找到了以下解决方案。

方案一:

我们应该以 PMML 格式导出 python 模型,然后使用 GoScore 在 GoLang 中导入图书馆

这是使用 GoScore 加载 PMML 文件的 GoLang 代码片段

modelXml, err := ioutil.ReadFile("titanic_rf.pmml")
if (err != nil) {
panic(err)
}
var model goscore.RandomForest
xml.Unmarshal([]byte(modelXml), &model)

features := map[string]interface{}{
"Sex": "male",
"Parch": 0,
"Age": 30,
"Fare": 9.6875,
"Pclass": 2,
"SibSp": 0,
"Embarked": "Q",
}

score, _ := model.Score(features, "0") // scores 0.486
score, _ := model.Score(features, "1") // scores 0.514
score, _ := model.LabelScores(features) // map[0:243 1:257]

引用 this medium article 找到了这个解决方案. (虽然它的 R 模型到 GoLang,它仍然相关)

解决方案2:

SkLearn-Porter是 python 中的库,用于将模型移植到各种语言中。不幸的是,它目前并不支持 Go 的所有模型。

关于python - 如何在 Go 语言中使用经过训练的 Scikit Learn Python 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71403705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com