Prerequisite:

Code:

//training data
DataSet samples = new DataSet(2,1);
        
samples.addRow(new double[]{0,0},new double[]{0});
samples.addRow(new double[]{0,1},new double[]{1});
samples.addRow(new double[]{1,0},new double[]{1});
samples.addRow(new double[]{1,1},new double[]{0});
      
//train ann
MultiLayerPerceptron ann = new MultiLayerPerceptron(2,2,1);        
ann.learn(samples);
        
//check results
for (int index=0; index<samples.size(); index++) {
  DataSetRow sample = samples.getRowAt(index);
  ann.setInput(sample.getInput());
  ann.calculate();
            
  double[] outputs = ann.getOutput();
  System.out.println(outputs[0]);
}
Advertisements