static void Main(string[] args) { FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(2)); network.AddLayer(new FeedforwardLayer(3)); network.AddLayer(new FeedforwardLayer(1)); network.Reset(); // train the neural network TrainingSetNeuralGeneticAlgorithm train = new TrainingSetNeuralGeneticAlgorithm( network, true, XOR_INPUT, XOR_IDEAL, 5000, 0.1, 0.25); int epoch = 1; do { train.Iteration(); Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error); epoch++; } while ((epoch < 5000) && (train.Error > 0.001)); network = train.Network; // test the neural network Console.WriteLine("Neural Network Results:"); for (int i = 0; i < XOR_IDEAL.Length; i++) { double []actual = network.ComputeOutputs(XOR_INPUT[i]); Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1] + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]); } }
/// <summary> /// The constructor sets up to train the neural network with a GA. /// </summary> /// <param name="genetic">The genetic algorithm to use.</param> /// <param name="network">The neural network to use.</param> public TrainingSetNeuralChromosome( TrainingSetNeuralGeneticAlgorithm genetic, FeedforwardNetwork network) { this.GA = genetic; this.Network = network; InitGenes(network.MatrixSize); UpdateGenes(); }