Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Neural Network Class in WEKA in Java code

Tags:

java

weka

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?

like image 301
Fajri Koto Avatar asked Feb 24 '15 11:02

Fajri Koto


People also ask

How can we use neural network in Weka?

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.

Does Weka support neural network?

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.

Can Weka be used for deep learning?

Weka IO is a cloud-native platform that provides all of these features, and more, to support your machine and deep learning workloads.

What is weka in Java?

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.


2 Answers

Following steps might be able to help you:

  1. Add Weka libraries

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

  1. 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.
  1. 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

    eval.crossValidateModel(mlp, train, kfolds, new Random(1));
  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();
    
like image 133
Hitanshu Tiwari Avatar answered Sep 28 '22 19:09

Hitanshu Tiwari


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!

like image 40
Fajri Koto Avatar answered Sep 28 '22 18:09

Fajri Koto