Dan D.
Dan D.

Reputation: 8557

Why is this XOR neural network having 2 outputs?

Regularly, a simple neural network to solve XOR should have 2 inputs, 2 neurons in hidden layer, 1 neuron in output layer.

However, the following example implementation has 2 output neurons, and I don't get it:

https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/feedforward/xor/XorExample.java

Why did the author put 2 output neurons in there?

Edit: Author of the example noted that he is using 4 neurons in hidden layer, 2 neurons in output layer. But I still don't get it why, why a shape of {4,2} instead of {2,1}?

Upvotes: 2

Views: 468

Answers (3)

Adam Gerard
Adam Gerard

Reputation: 736

It might be helpful to think of it like this:

Training Set        Label Set

    0 | 1               0 | 1
0 | 0 | 0          0 |  0 | 1
1 | 1 | 0          1 |  1 | 0
2 | 0 | 1          2 |  1 | 0
3 | 1 | 1          3 |  0 | 1

So [[0,0], 0], [[0,1], 0], etc. for the Training Set.

If you're using the two column Label Set, 0 and 1 correspond to true or false.

Thus, [0,0] correctly maps to false, [1,0] correctly maps to true, etc.

A pretty good article that slightly modifies the original can be found here: https://medium.com/autonomous-agents/how-to-teach-logic-to-your-neuralnetworks-116215c71a49

Upvotes: 0

Martin Thoma
Martin Thoma

Reputation: 136187

This is called one hot encoding. The idea is that you have one neuron per class. Each neuron gives the probability of that class.

I don't know why he uses 4 hidden neurons. 2 should be enough (if I remember correctly).

Upvotes: 3

Shaido
Shaido

Reputation: 28322

The author uses the Evaluation class in the end (for stats of how often the network gives the correct result). This class needs one neuron per classification to work correctly, i.e. one output neuron for true and one for false.

Upvotes: 1

Related Questions