在我的 java 代码中使用 WEKA SVM 模型时出错(在 WEKA 中保存后为空 SVM 模型)
Error in using WEKA SVM Model in my java code (Empty SVM model after saving it in WEKA)
我已经在 WEKA (3.7.3) 中训练了 LIBSVM 模型,现在想在我的 java 代码中使用它。但是,我遇到了一个例外。
Exception:Attempt to invoke interface method 'double weka.classifiers.Classifier.classifyInstance(weka.core.Instance)' on a null object reference
所有其他模型在此 java 代码中工作正常。此外,WEKA jar 的版本与我用于训练模型的版本完全相同。我的应用程序中没有任何 LIBSVM jar,因为我使用的是经过训练的模型。我需要在我的应用程序中放置 LIBSVM jar 吗?
我在这里错过了什么?
inputStream = getApplicationContext().getAssets().open("svm.model");
classifier = (Classifier) weka.core.SerializationHelper.read(inputStream);
这是模型的内容:
=== Model information ===
Filename: svm.model
Scheme: weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.001 -P 0.1 -model "D:\Program Files (x86)\Weka-3-7" -seed 1
Relation: Sho_gsw30SVRNULL-weka.filters.unsupervised.attribute.Remove-R5-13,18-26,31-39,44-130
Attributes: 17
F1
F2
F3
F4
F14
F15
F16
F17
F27
F28
F29
F30
F40
F41
F42
F43
class
=== Classifier model ===
LibSVM wrapper, original code by Yasser EL-Manzalawy (= WLSVM)
更新:
我尝试了 WEKA SMO(支持向量机分类器),它在我的代码中运行良好,但在 LIBSVM 中运行良好。
如果您显示的模型内容是 "svm.model" 文件的实际内容,那就是罪魁祸首。
WEKA 中的模型文件是序列化的 Java 个对象。它们可以通过训练一个相当于模型的分类器来创建,serializing that Java WEKA classifier object into a file。该文件可能不是人类可读的。
这是基于我链接的 WEKA 站点序列化模型的代码:
// classifier will be your SVM classifier here
// and instances the training instances
classifier.buildClassifier(instances);
// serialize model
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("svm.model"));
oos.writeObject(classifier);
oos.flush();
oos.close();
然后您在问题中发布的代码可以加载和使用该模型。
更新:我尝试了 WEKA SMO(支持向量机分类器),它在我的代码中运行良好,但在 LIBSVM 中运行良好。
我已经在 WEKA (3.7.3) 中训练了 LIBSVM 模型,现在想在我的 java 代码中使用它。但是,我遇到了一个例外。
Exception:Attempt to invoke interface method 'double weka.classifiers.Classifier.classifyInstance(weka.core.Instance)' on a null object reference
所有其他模型在此 java 代码中工作正常。此外,WEKA jar 的版本与我用于训练模型的版本完全相同。我的应用程序中没有任何 LIBSVM jar,因为我使用的是经过训练的模型。我需要在我的应用程序中放置 LIBSVM jar 吗?
我在这里错过了什么?
inputStream = getApplicationContext().getAssets().open("svm.model");
classifier = (Classifier) weka.core.SerializationHelper.read(inputStream);
这是模型的内容:
=== Model information ===
Filename: svm.model
Scheme: weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.001 -P 0.1 -model "D:\Program Files (x86)\Weka-3-7" -seed 1
Relation: Sho_gsw30SVRNULL-weka.filters.unsupervised.attribute.Remove-R5-13,18-26,31-39,44-130
Attributes: 17
F1
F2
F3
F4
F14
F15
F16
F17
F27
F28
F29
F30
F40
F41
F42
F43
class
=== Classifier model ===
LibSVM wrapper, original code by Yasser EL-Manzalawy (= WLSVM)
更新: 我尝试了 WEKA SMO(支持向量机分类器),它在我的代码中运行良好,但在 LIBSVM 中运行良好。
如果您显示的模型内容是 "svm.model" 文件的实际内容,那就是罪魁祸首。
WEKA 中的模型文件是序列化的 Java 个对象。它们可以通过训练一个相当于模型的分类器来创建,serializing that Java WEKA classifier object into a file。该文件可能不是人类可读的。
这是基于我链接的 WEKA 站点序列化模型的代码:
// classifier will be your SVM classifier here
// and instances the training instances
classifier.buildClassifier(instances);
// serialize model
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("svm.model"));
oos.writeObject(classifier);
oos.flush();
oos.close();
然后您在问题中发布的代码可以加载和使用该模型。
更新:我尝试了 WEKA SMO(支持向量机分类器),它在我的代码中运行良好,但在 LIBSVM 中运行良好。