在 Java 代码中使用 WEKA 中的神经网络类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28694971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Neural Network Class in WEKA in Java code
提问by Fajri Koto
Hi I want to do simple training and testing using Neural Network in WEKA library.
嗨,我想使用 WEKA 库中的神经网络进行简单的训练和测试。
But, I find it is not trivial, and its different with NaiveBayes class in its library.
但是,我发现它不是微不足道的,它与库中的 NaiveBayes 类不同。
Anyone have example how to use this class in java code?
任何人都有如何在java代码中使用这个类的例子?
回答by Hitanshu Tiwari
Following steps might be able to help you:
以下步骤或许能帮到您:
- Add Weka libraries
- 添加 Weka 库
Download Weka from http://www.cs.waikato.ac.nz/ml/weka/downloading.html.
从http://www.cs.waikato.ac.nz/ml/weka/downloading.html下载 Weka 。
From the package find 'Weka.jar' and add in the project.
从包中找到“Weka.jar”并添加到项目中。
Java Code Snippet
Java 代码片段
Building a Neural Classifier
public void simpleWekaTrain(String filepath) { try{ //Reading training arff or csv file FileReader trainreader = new FileReader(filepath); Instances train = new Instances(trainreader); train.setClassIndex(train.numAttributes() – 1); //Instance of NN MultilayerPerceptron mlp = new MultilayerPerceptron(); //Setting Parameters mlp.setLearningRate(0.1); mlp.setMomentum(0.2); mlp.setTrainingTime(2000); mlp.setHiddenLayers(“3?); mlp.buildClassifier(train); } catch(Exception ex){ ex.printStackTrace(); } }
构建神经分类器
public void simpleWekaTrain(String filepath) { try{ //Reading training arff or csv file FileReader trainreader = new FileReader(filepath); Instances train = new Instances(trainreader); train.setClassIndex(train.numAttributes() – 1); //Instance of NN MultilayerPerceptron mlp = new MultilayerPerceptron(); //Setting Parameters mlp.setLearningRate(0.1); mlp.setMomentum(0.2); mlp.setTrainingTime(2000); mlp.setHiddenLayers(“3?); mlp.buildClassifier(train); } catch(Exception ex){ ex.printStackTrace(); } }
Another Way to set parameters,
另一种设置参数的方式,
mlp.setOptions(Utils.splitOptions(“-L 0.1 -M 0.2 -N 2000 -V 0 -S 0 -E 20 -H 3?));
Where,
在哪里,
L = Learning Rate
M = Momentum
N = Training Time or Epochs
H = Hidden Layers
etc.
- Neural Classifier Training Validation
- 神经分类器训练验证
For evaluation of training data,
为了评估训练数据,
Evaluation eval = new Evaluation(train);
eval.evaluateModel(mlp, train);
System.out.println(eval.errorRate()); //Printing Training Mean root squared Error
System.out.println(eval.toSummaryString()); //Summary of Training
To apply K-Fold validation
应用 K 折验证
eval.crossValidateModel(mlp, train, kfolds, new Random(1));
Evaluating/Predicting unlabelled data
Instances datapredict = new Instances( new BufferedReader( new FileReader(<Predictdatapath>))); datapredict.setClassIndex(datapredict.numAttributes() – 1); Instances predicteddata = new Instances(datapredict); //Predict Part for (int i = 0; i < datapredict.numInstances(); i++) { double clsLabel = mlp.classifyInstance(datapredict.instance(i)); predicteddata.instance(i).setClassValue(clsLabel); } //Storing again in arff BufferedWriter writer = new BufferedWriter( new FileWriter(<Output File Path>)); writer.write(predicteddata.toString()); writer.newLine(); writer.flush(); writer.close();
评估/预测未标记的数据
Instances datapredict = new Instances( new BufferedReader( new FileReader(<Predictdatapath>))); datapredict.setClassIndex(datapredict.numAttributes() – 1); Instances predicteddata = new Instances(datapredict); //Predict Part for (int i = 0; i < datapredict.numInstances(); i++) { double clsLabel = mlp.classifyInstance(datapredict.instance(i)); predicteddata.instance(i).setClassValue(clsLabel); } //Storing again in arff BufferedWriter writer = new BufferedWriter( new FileWriter(<Output File Path>)); writer.write(predicteddata.toString()); writer.newLine(); writer.flush(); writer.close();
回答by Fajri Koto
I read some sources on the internet and just realize that "if you want to use NeuralNetwork classifier in WEKA library, so the approach is NOT using the given NeuralNetwork class, but it should be "MultilayerPerceptron" class"
我在互联网上阅读了一些资料,并意识到“如果您想在 WEKA 库中使用 NeuralNetwork 分类器,那么该方法不使用给定的 NeuralNetwork 类,而应该是“MultilayerPerceptron”类”
It's a little bit tricky and consumed my hours.
这有点棘手,消耗了我的时间。
I hope it's useful for anyone who is struggling with this.
我希望它对任何为此苦苦挣扎的人有用。
http://weka.8497.n7.nabble.com/Multi-layer-perception-td2896.html
http://weka.8497.n7.nabble.com/Multi-layer-perception-td2896.html
Ps. Please correct if I am being wrong!
附言。如果我错了,请纠正!