java/C++中的如何与C、C++和Python链接的类解错?

2024-06-07 09:07:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个基于机器学习和统计的分类器,称为EVM(极值机器),使用linkhttps://pypi.org/project/EVM/。我用python训练模型,并用下面的代码将其保存为pickle文件

import EVM
import pickle
mevm = EVM.MultipleEVM(tailsize=300, cover_threshold = 0.7, distance_multiplier = 0.7, distance_function = scipy.spatial.distance.euclidean)
mevm.train([trainX[i] for i in list(trainX.keys())])

#Save the trained model
with open('model.pkl','wb') as f:
  pickle.dump(mevm,f)

我可以使用相同的Pickle模块轻松地在Python中加载相同的模型。当我保存时,所有的权重都保存在pickle文件中,我可以进行如下推断

import pickle
import EVM
with open('model.pkl','rb') as b:
     mevm = pickle.load(b)
print("Probability of given input", mevm.max_probabilities([embeds[i]]))

但是我无法在Android/Java中读取相同的pickle文件(我想在我的Android应用程序中使用这个保存的模型)。当我检查EVM类时,有一个C文件和一个C++文件,它使用'.Pyx文件连接。所有这3个文件都是使用python EVM(即导入EVM)类读取的。请参考下面的链接了解文件 https://pypi.org/project/EVM/#files

我的问题是

  1. 如何在Android/Java中读取此保存的模型<> LI>因为它与C、C++和Python链接,在java中可以读取吗?李>
  2. 还有其他更好的方法来保存这个,这样我就可以使用Andriod/Java了吗

任何线索都很感激

提前感谢:)


Tags: 文件org模型importprojectpypi机器model
1条回答
网友
1楼 · 发布于 2024-06-07 09:07:43

根据python文档here

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” 1 or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”

这意味着给定的字节流在Python中仅在语法上有效,除非您想编写对象传输程序

你以错误的方式处理你的问题。ML模型只是一组系数/参数,您已对其进行优化,以获得目标变量的近似值。您应该能够获取该组系数/参数,并在任何语言中使用它

相关问题 更多 >

    热门问题