-
Notifications
You must be signed in to change notification settings - Fork 2
Tutorial #2 XOR
Maximilian Wittmer edited this page Aug 15, 2018
·
3 revisions
XOR is a logic gate. It stands for Exclusive Or: Only when either one of both inputs are on, the output will be true.
Truth table:
- 1 XOR 1: 0
- 1 XOR 0: 1
- 0 XOR 1: 1
- 0 XOR 0: 0
XOR is a non-linearly separable problem: you cannot draw one single line to separate the two solutions! Thus, we need at the very least a Neural Network with one Hidden Layer.
Let's create a Network like this:
#include "zeneural/NeuralNetwork.hpp"
class xorWrapper {
public:
ZNN::NeuralNetwork<double> xorNN;
xorWrapper() {
xorNN.setInputLayerSize(2);
xorNN.addHiddenLayer(10);
xorNN.setOutputLayerSize(1);
}
};Our Neural Network expects inputs as std::vector. Thus, we have to expand our class with a dataset. In our case, we will use a vector that contains vectors of double.
class xorWrapper {
std::vector<std::vector<double>> input{{1, 0}, {0, 1}, {0, 0}, {1, 1}};
std::vector<std::vector<double>> target{{1}, {1}, {0}, {0}};
}Now we are all set to train our NN! To train it, we give it a std::vector of input values and the expected results.
class xorWrapper {
public:
void trainNN(size_t iterations) {
for (size_t i = 0; i < iterations; i++)
{
xorNN.train(input[0], target[0]);
xorNN.train(input[1], target[1]);
xorNN.train(input[2], target[2]);
xorNN.train(input[3], target[3]);
}
}
};The full class now looks like this:
class xorWrapper {
std::vector<std::vector<double>> input{{1, 0}, {0, 1}, {0, 0}, {1, 1}};
std::vector<std::vector<double>> target{{1}, {1}, {0}, {0}};
public:
ZNN::NeuralNetwork<double> xorNN;
xorWrapper() {
xorNN.setInputLayerSize(2);
xorNN.addHiddenLayer(10);
xorNN.setOutputLayerSize(1);
}
void trainNN(size_t iterations) {
for (size_t i = 0; i < iterations; i++)
{
xorNN.train(input[0], target[0]);
xorNN.train(input[1], target[1]);
xorNN.train(input[2], target[2]);
xorNN.train(input[3], target[3]);
}
}
};We can now use this class like this:
int main () {
xorWrapper NN{};
NN.train(100);
/*ask for two numbers and put them into a vector called userInput*/
std::cout << NN.xorNN.guess(userInput)[0];
}