/// <summary> /// Create, train and use a neural network for XOR. /// </summary> /// <param name="args">Not used</param> 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 Train train = new HeatonResearchNeural.Feedforward.Train.Backpropagation.Backpropagation(network, XOR_INPUT, XOR_IDEAL, 0.7, 0.9); int epoch = 1; do { train.Iteration(); Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error); epoch++; } while ((epoch < 5000) && (train.Error > 0.001)); // 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]); } }
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]); } }
public static FeedforwardNetwork createNetwork() { FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(9)); network .AddLayer(new FeedforwardLayer(NeuralTicTacToe.NEURONS_HIDDEN_1)); if (NeuralTicTacToe.NEURONS_HIDDEN_2 > 0) { network.AddLayer(new FeedforwardLayer( NeuralTicTacToe.NEURONS_HIDDEN_2)); } network.AddLayer(new FeedforwardLayer(1)); network.Reset(); return network; }
public static FeedforwardNetwork CreateNetwork() { ActivationFunction threshold = new ActivationSigmoid(); FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(threshold, Config.INPUT_SIZE)); network.AddLayer(new FeedforwardLayer(threshold, Config.NEURONS_HIDDEN_1)); if (Config.NEURONS_HIDDEN_2 > 0) { network.AddLayer(new FeedforwardLayer(threshold, Config.NEURONS_HIDDEN_2)); } network.AddLayer(new FeedforwardLayer(threshold, Config.OUTPUT_SIZE)); network.Reset(); return network; }
/// <summary> /// Create, train and use a neural network for XOR. /// </summary> /// <param name="args">Not used</param> static void Main(string[] args) { /*XOR_INPUT = new double[256][]; XOR_IDEAL = new double[256][]; String[] lines = File.ReadAllLines("input.txt"); for (int i = 0; i < lines.Length; i++) { String str = lines[i]; double[] input = new double[8]; for (int j = 0; j < 8; j++) { input[j] = Double.Parse(str[j].ToString()); } XOR_INPUT[i] = input; } lines = File.ReadAllLines("output.txt"); for (int i = 0; i < lines.Length; i++) { String str = lines[i]; double[] output = new double[1]; output[0] = Double.Parse(str); XOR_IDEAL[i] = output; } */ FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(4)); network.AddLayer(new FeedforwardLayer(12)); network.AddLayer(new FeedforwardLayer(4)); network.Reset(); // train the neural network Train train = new Backpropagation(network, XOR_INPUT, XOR_IDEAL, 0.2, 0.95); int epoch = 1; Stopwatch watch = new Stopwatch(); watch.Start(); do { train.Iteration(); Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error); epoch++; } while ((epoch < 40000) && (train.Error > 0.001)); watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds / 1000.0); // test the neural network #if DEBUG 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] + "," + XOR_INPUT[i][2] + "," + XOR_INPUT[i][3] + ", actual=" + actual[0] + "," + actual[1] + "," + actual[2] + "," + actual[3] + ",ideal=" + XOR_IDEAL[i][0] + "," + XOR_IDEAL[i][1] + "," + XOR_IDEAL[i][2] + "," + XOR_IDEAL[i][3]); } #endif FeedforwardLayer inputLayer = network.InputLayer; Console.WriteLine("Input layer matrix:"); Matrix layerMatrix = inputLayer.LayerMatrix; using (StreamWriter file = new StreamWriter("weights.txt")) { for (int i = 0; i < layerMatrix.Rows; i++) { for (int j = 0; j < layerMatrix.Cols; j++) { Console.Write(Math.Round(layerMatrix[i, j], 3) + " "); file.Write(layerMatrix[i, j]); if (j != layerMatrix.Cols - 1) { file.Write(" "); } } Console.WriteLine(); file.WriteLine(); } file.WriteLine(); FeedforwardLayer hiddenLayer = network.HiddenLayers.ToList()[0]; Console.WriteLine("Hidden layer matrix:"); layerMatrix = hiddenLayer.LayerMatrix; for (int i = 0; i < layerMatrix.Rows; i++) { for (int j = 0; j < layerMatrix.Cols; j++) { Console.Write(Math.Round(layerMatrix[i, j], 3) + " "); file.Write(layerMatrix[i, j]); if (j != layerMatrix.Cols - 1) { file.Write(" "); } } Console.WriteLine(); file.WriteLine(); } } #if SHOW_MATRIX FeedforwardLayer outputLayer = network.OutputLayer; Console.WriteLine("Output layer matrix:"); layerMatrix = outputLayer.LayerMatrix; for (int i = 0; i < layerMatrix.Rows; i++) { for (int j = 0; j < layerMatrix.Cols; j++) { Console.Write(layerMatrix[i, j] + " "); } Console.WriteLine(); } foreach (FeedforwardLayer feedforwardLayer in network.Layers) { Matrix layerMatrix = feedforwardLayer.LayerMatrix; Console.WriteLine(feedforwardLayer.Next); for (int i = 0; i < layerMatrix.Rows; i++) { for (int j = 0; j < layerMatrix.Cols; j++) { Console.Write(layerMatrix[i, j] + " "); } Console.WriteLine(); } } #endif Console.ReadKey(); }
/// <summary> /// Create, train and use a neural network for XOR. /// </summary> /// <param name="args">Not used</param> static void Main(string[] args) { List<String> lines = File.ReadAllLines("weights.txt").ToList(); double[,] inputLayerWeights = new double[5, 12]; double[,] hiddenLayerWeights = new double[13, 4]; bool forInput = true; int counter = 0; foreach (string line in lines) { if (String.IsNullOrWhiteSpace(line)) { forInput = false; counter = 0; } else { if (forInput) { string[] strings = line.Split(' '); for (int i = 0; i < strings.Count(); i++) { inputLayerWeights[counter, i] = double.Parse(strings[i]); } } else { string[] strings = line.Split(' '); for (int i = 0; i < strings.Count(); i++) { hiddenLayerWeights[counter, i] = double.Parse(strings[i]); } } counter++; } } Matrix inputLayerMatrix = new Matrix(inputLayerWeights); Matrix hiddenLayermatrix = new Matrix(hiddenLayerWeights); FeedforwardNetwork network = new FeedforwardNetwork(); network.AddLayer(new FeedforwardLayer(4)); network.AddLayer(new FeedforwardLayer(12)); network.AddLayer(new FeedforwardLayer(4)); network.Reset(); FeedforwardLayer inputLayer = network.InputLayer; inputLayer.LayerMatrix = inputLayerMatrix; FeedforwardLayer hiddenLayer = network.HiddenLayers.ToList()[0]; hiddenLayer.LayerMatrix = hiddenLayermatrix; 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] + "," + XOR_INPUT[i][2] + "," + XOR_INPUT[i][3] + ", actual=" + actual[0] + "," + actual[1] + "," + actual[2] + "," + actual[3] + ",ideal=" + XOR_IDEAL[i][0] + "," + XOR_IDEAL[i][1] + "," + XOR_IDEAL[i][2] + "," + XOR_IDEAL[i][3]); } Console.ReadKey(); /*Console.WriteLine("Input layer matrix:"); Matrix layerMatrix = inputLayer.LayerMatrix; for (int i = 0; i < layerMatrix.Rows; i++) { for (int j = 0; j < layerMatrix.Cols; j++) { Console.Write(Math.Round(layerMatrix[i, j], 3) + " "); } Console.WriteLine(); } Console.WriteLine("Hidden layer matrix:"); layerMatrix = hiddenLayer.LayerMatrix; for (int i = 0; i < layerMatrix.Rows; i++) { for (int j = 0; j < layerMatrix.Cols; j++) { Console.Write(Math.Round(layerMatrix[i, j], 3) + " "); } Console.WriteLine(); } */ }