public void ShouldTrainForXor() { var brain = new NeuralNetwork(2, new int[3] { 5, 5, 5 }, 1, ActivationFunction.LogSigmoid); var deepLearning = new DeepLearning(brain, 0.00001); var inputs = new Matrix[4]; inputs[0] = new Matrix(2, 1); inputs[0][0, 0] = 0; inputs[0][1, 0] = 0; inputs[1] = new Matrix(2, 1); inputs[1][0, 0] = 1; inputs[1][1, 0] = 0; inputs[2] = new Matrix(2, 1); inputs[2][0, 0] = 0; inputs[2][1, 0] = 1; inputs[3] = new Matrix(2, 1); inputs[3][0, 0] = 1; inputs[3][1, 0] = 1; var targets = new Matrix[4]; targets[0] = new Matrix(1, 1); targets[0][0, 0] = 0; targets[1] = new Matrix(1, 1); targets[1][0, 0] = 1; targets[2] = new Matrix(1, 1); targets[2][0, 0] = 1; targets[3] = new Matrix(1, 1); targets[3][0, 0] = 0; var rand = new Random(); for (int i = 0; i < 10000; i++) { var t = rand.Next(4); deepLearning.Train(inputs[t], targets[t]); } var i0 = brain.Prediction(inputs[0]); // epected 0 var i2 = brain.Prediction(inputs[1]); // expected 1 var i3 = brain.Prediction(inputs[2]); // expected 1 var i4 = brain.Prediction(inputs[3]); //expected 0 Assert.IsFalse(false); }
static void Main(string[] args) { int nInput = 2; int nOutput = 1; int neuronsPerLayers = 10; int nLayers = 3; DeepLearning slp = new DeepLearning(nInput, neuronsPerLayers, nOutput, nLayers); //SingleLayerPerceptron slp = new SingleLayerPerceptron(nInput, neuronsPerLayers, nOutput); var nPoints = 10000; for (int i = 0; i < nPoints; i++) { Random random = new Random(Guid.NewGuid().GetHashCode()); int value1 = random.Next() % 2; random = new Random(Guid.NewGuid().GetHashCode()); int value2 = random.Next() % 2; random = new Random(Guid.NewGuid().GetHashCode()); int value3 = random.Next() % 2; //int xor = XOR(value1, value2); int correctValue = ComplexeBoolMethod(value1, value2, value3); slp.Train(new List <double>() { value1, value2 }, new List <double>() { correctValue }); var percent = Math.Round(i * 100.0 / nPoints, 4); //if (percent == (int)percent) //{ // Console.Clear(); // Console.WriteLine(percent); //} } double n = 0; int nTestCase = 200; for (int i = 0; i < nTestCase; i++) { Random random = new Random(Guid.NewGuid().GetHashCode()); int value1 = random.Next() % 2; random = new Random(Guid.NewGuid().GetHashCode()); int value2 = random.Next() % 2; random = new Random(Guid.NewGuid().GetHashCode()); int value3 = random.Next() % 2; //int xor = XOR(value1, value2); int correctValue = ComplexeBoolMethod(value1, value2, value3); var guess = slp.Predict(new List <double>() { value1, value2 }); double result = guess[0] > 0.5 ? 1 : 0; bool correct = result == correctValue; Console.WriteLine("correct value : " + correctValue); Console.WriteLine("guess : " + guess[0]); Console.WriteLine("--"); if (correct) { n++; } } var path = "C:\\Users\\Boudart Loic\\source\\repos\\MachineLearning\\MachineLearning\\brain.json"; slp.Save(path); slp.Load(path); //to debug { Random random = new Random(Guid.NewGuid().GetHashCode()); int value1 = random.Next() % 2; random = new Random(Guid.NewGuid().GetHashCode()); int value2 = random.Next() % 2; int xor = XOR(value1, value2); slp.Train(new List <double>() { value1, value2 }, new List <double>() { xor }); //if (percent == (int)percent) //{ // Console.Clear(); // Console.WriteLine(percent); //} } Console.WriteLine(n / nTestCase); Console.Read(); }