Hi I want to do simple training and testing using Neural Network in WEKA library.
But, I find it is not trivial, and its different with NaiveBayes class in its library.
Anyone have example how to use this class in java code?
Neural Network has several components including the Input Layer, Hidden Layers and Output Layer: (1) Input Layer denotes the input variables that will be fed into the network, (2) Hidden Layers are the computation layers (or parameters) that will be trained, (3) Output Layer denotes the output of the model.
We present WekaDeeplearning4j, a Weka package that makes deep learning accessible through a graphical user interface (GUI). The package uses Deeplearning4j as its backend, provides GPU support, and enables GUI-based training of deep neural networks such as convolutional and recurrent neural networks.
Weka IO is a cloud-native platform that provides all of these features, and more, to support your machine and deep learning workloads.
Weka is a collection of machine learning algorithms for data mining tasks. It contains tools for data preparation, classification, regression, clustering, association rules mining, and visualization.
Following steps might be able to help you:
Download Weka from http://www.cs.waikato.ac.nz/ml/weka/downloading.html.
From the package find 'Weka.jar' and add in the project.
Java Code Snippet
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();
}
}
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.
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
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();
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"
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
Ps. Please correct if I am being wrong!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With