Re: Inconsistencies between GUI and Java code

Peter Reutemann <[email protected]> Thu, 23 Nov 2023 11:05:42 +1300
Newsgroups gmane.comp.ai.weka
Message-ID <CAHoQ12Lo_-zkFzmqvfC0R8Xm7GMKHa2OkwCemFm=J7BWig9ioQ@mail.gmail.com>
> I am getting inconsistencies when trying to reproduce a result obtained
> via the Weka GUI with Java code. I am using the same ARFF file for the
> GUI and my Java code. It is a file of roughly 5.5MB, which contains 18
> attributes and a class attribute.
>
> I use no filters, however, the ARFF file is already normalised. I am
> using the Multilayer Perceptron for classification in its default
> settings except that I add some hidden layers (9, 15, 6).
>
> The GUI shows me this command line:
> weka.classifiers.functions.MultilayerPerceptron -L 0.3 -M 0.2 -N 500 -V
> 0 -S 0 -E 20 -H "9, 15, 6"
>
> My Java code:
> weka.classifiers.functions.MultilayerPerceptron classifier = new
> weka.classifiers.functions.MultilayerPerceptron();
> classifier.setHiddenLayers("9, 15, 6");
>
> The evaluation results for both methods are different!

[...]

Without seeing your code and knowing what exact evaluation parameters
you used, it is impossible to comment.
I've attached an example class with which I recreate the exact same
evaluation results and file size of the serialized model as generated
by the Explorer.

Cheers, Peter
-- 
Peter Reutemann
Dept. of Computer Science
University of Waikato, Hamilton, NZ
Mobile +64 22 190 2375
https://www.cs.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/

_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
MLP.java (text/x-java, 1.4 KB)
import weka.classifiers.Evaluation;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.Instances;
import weka.core.SerializationHelper;
import weka.core.SerializedObject;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;

import java.util.Random;

public class MLP {

  public static void main(String[] args) throws Exception {
    MultilayerPerceptron mlp = new MultilayerPerceptron();
    mlp.setHiddenLayers("9, 15, 6");
    System.out.println(Utils.toCommandLine(mlp));

    Instances data = DataSource.read("/some/where/iris.arff");
    data.setClassIndex(data.numAttributes() - 1);

    // evaluate
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(mlp, data, 10, new Random(1));
    // output summary
    System.out.println(eval.toSummaryString(false));

    // train model
    mlp.buildClassifier(data);
    // the next two lines aren't strictly necessary but the explorer
    // performs them to get a deep copy of the classifier object before
    // storing the model in its evaluation history
    // these two lines only ensure that the file sizes are the same
    // between output generated by the API calls and the Explorer
    SerializedObject so = new SerializedObject(mlp);
    mlp = (MultilayerPerceptron) so.getObject();
    // save model with data header
    SerializationHelper.writeAll("/some/where/mlp.model", new Object[]{mlp, new Instances(data, 0)});
  }
}