This article shows how to create an XOR solver artificial neural network with DeepLearning4J.

Prerequisites:

Training samples:

INDArray inputs = Nd4j.create(
  new double[]{
    0,0,  0,1,  1,0,  1,1
  },
  new int[]{4,2}
);
        
INDArray expectedOutputs = Nd4j.create(
  new double[]{
    0,  1,  1,  0 //4 expected outputs
  },
  new int[]{4,1}
);

DataSet samples = new DataSet(inputs,expectedOutputs);

Create the network (in the output layer, only cross-entropyXENT loss function works well with Activation.SIGMOID):

//config
MultiLayerConfiguration config = 
new NeuralNetConfiguration.Builder().
iterations(1000).learningRate(0.1).biasInit(1).list().

//2 neurons
layer(
  0,
  new DenseLayer.Builder().nIn(2).nOut(2).
  activation(Activation.SIGMOID).build()
).

//1 neuron
layer(
  1,
  new OutputLayer.Builder(LossFunction.XENT).
  nIn(2).nOut(1).activation(Activation.SIGMOID).build()
).        
backprop(true).build();
        
//neural net
MultiLayerNetwork ann = new MultiLayerNetwork(config);
ann.init();
ann.setListeners(new ScoreIterationListener(100));

Train the network:

ann.fit(samples);

Check outputs of the training samples:

System.out.println(
  ann.output(samples.getFeatureMatrix()).toString()
);

Check any output:

System.out.println(ann.output(new double[]{0,0}));
System.out.println(ann.output(new double[]{0,1}));
System.out.println(ann.output(new double[]{1,0}));
System.out.println(ann.output(new double[]{1,1}));
Advertisements