简体   繁体   English

如何在serialVersionUID发生变化后在java中加载libsvm模型

[英]how to load libsvm model in java after serialVersionUID has changed

I have trained libsvm model on weka and then I saved the model. 我在weka上训练了libsvm模型,然后我保存了模型。 Now I want to use this model in java. 现在我想在java中使用这个模型。

Classifier cls = (Classifier)weka.core.SerializationHelper.read(this.modelPath);

I get this error 我收到这个错误

"java.io.InvalidClassException: libsvm.svm_model; local class incompatible: stream classdesc serialVersionUID = -8642637231196646922, local class serialVersionUID = 2709444323471798245" “java.io.InvalidClassException:libsvm.svm_model;本地类不兼容:stream classdesc serialVersionUID = -8642637231196646922,local class serialVersionUID = 2709444323471798245”

when I run above code. 当我运行上面的代码。 I wanna know how i can load and use model. 我想知道如何加载和使用模型。

Chances are you changed the class definition between the time you saved the model and the time you are trying to load it (see eg What is a serialVersionUID and why should I use it? for a good explanation). 您可能在保存模型的时间和尝试加载模型的时间之间更改了类定义(请参阅例如什么是serialVersionUID以及我为什么要使用它?以获得一个很好的解释)。 Think of serialVersionUID as a sort of checksum which makes sure you don't load an outdated version of your classes. serialVersionUID视为一种校验和,确保您不会加载过时的类版本。 Unless that's what you want because you know better than Java that you can still use the old models - in that case you can manually set that ID. 除非那是你想要的,因为你比Java更了解你仍然可以使用旧模型 - 在这种情况下你可以手动设置该ID。 In order to tell Java that your current classifier is still up-to-date, add 为了告诉Java您当前的分类器仍然是最新的,请添加

static final long serialVersionUID = -8642637231196646922;

to the code of the class. 到班级的代码。

Now I'm however wondering: Classifier looks like a Weka built-in class. 现在我想知道: Classifier看起来像Weka内置类。 I'm not sure how easily the serialVersionUID can be changed in that case. 我不确定在这种情况下可以轻松更改serialVersionUID。 Did you maybe update the Weka version? 你有没有更新Weka版本? If you're really invested in your model file, you might wanna go into the source code of Weka and change the serialVersionUID right there. 如果你真的投资了你的模型文件,你可能想进入Weka的源代码并在那里更改serialVersionUID

Don't have to mess around with serialVersionUID. 不必乱用serialVersionUID。 Use SerializationHelper to save/load LibSVM model files (as described in this other answer ): 使用SerializationHelper保存/加载LibSVM模型文件(如另一个答案中所述 ):

weka.core.SerializationHelper.write("/some/where/svm.model", svmModel);

LibSVM svm = (LibSVM) weka.core.SerializationHelper.read("/some/where/svm.model");

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

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