예제 #1
0
        /// <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]);
            }
        }
예제 #2
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]);
		}
	}
예제 #3
0
        public static BitmapImage GetEdges(BitmapImage binaryBitmapImage, FeedforwardNetwork network, 
            BackgroundWorker backgroundWorker)
        {
            Bitmap sourceBitmap = GetBitmap(binaryBitmapImage);

            int width = sourceBitmap.Width;
            int height = sourceBitmap.Height;

            if (width < 2 || height < 2)
            {
                return null;
            }

            //todo:переделать
            if (width % 2 != 0 || height % 2 != 0)
            {
                return null;
            }

            Bitmap edgeBitmap = new Bitmap(width, height);
            for (int y = 0; y < height; y += 1)
            {
                for (int x = 0; x < width; x += 1)
                {
                    edgeBitmap.SetPixel(x, y, Color.White);
                }
            }

            /*using (StreamWriter file = new StreamWriter("debug.txt"))
            {*/
                for (int y = 0; y < height - 1; y += 1)
                {
                    for (int x = 0; x < width - 1; x += 1)
                    {
                        double[] values =
                        {
                            (sourceBitmap.GetPixel(x, y).R) / 255,
                            (sourceBitmap.GetPixel(x + 1, y).R) / 255,
                            (sourceBitmap.GetPixel(x, y + 1).R) / 255,
                            (sourceBitmap.GetPixel(x + 1, y + 1).R) / 255
                        };

                        double[] output = network.ComputeOutputs(values);

                        backgroundWorker.ReportProgress(0, new[] {values, output});
                        //Thread.Sleep(300);

                        double[,] outputValues =
                        {
                            {
                                output[0], output[1]
                            },
                            {
                                output[2], output[3]
                            }
                        };

                        /*file.Write(x + "," + y + " " + (x + 1) + "," + y + " " + x + "," + (y + 1) + " " + (x + 1) + "," + (y + 1) + ": "
                            + (sourceBitmap.GetPixel(x, y).R) + " " + (sourceBitmap.GetPixel(x + 1, y).R)
                            + " " + (sourceBitmap.GetPixel(x, y + 1).R) + " " + (sourceBitmap.GetPixel(x + 1, y + 1).R) + " -> ");

                        for (int y1 = 0; y1 < 2; y1++)
                        {
                            for (int x1 = 0; x1 < 2; x1++)
                            {
                                file.Write((x + x1) + "," + (y + y1));
                                if (x1 == 1 && y1 == 1)
                                {
                                    file.Write(": ");
                                }
                                else
                                {
                                    file.Write(" ");
                                }
                            }
                        }*/

                        for (int y1 = 0; y1 < 2; y1++)
                        {
                            for (int x1 = 0; x1 < 2; x1++)
                            {
                                double value = outputValues[y1, x1];
                                int intValue = (int)value;
                                if (value - intValue == 0.5)
                                {
                                    value += 0.01;
                                }

                                int pixelValue = (int)(Math.Round(value) * 255);
                                Color color = Color.FromArgb(pixelValue, pixelValue, pixelValue);

                                if (edgeBitmap.GetPixel(x + x1, y + y1).R == 255)
                                {
                                    edgeBitmap.SetPixel(x + x1, y + y1, color);
                                }

                               /* file.Write(color.R);
                                if (x1 == 1 && y1 == 1)
                                {
                                    file.Write("\r\n");
                                }
                                else
                                {
                                    file.Write(" ");
                                }*/
                            }
                        }

                    }
                }
               // }

            edgeBitmap.Save("edges.jpg");

            BitmapImage resultBitmapImage = GetBitmapImage(edgeBitmap);
            resultBitmapImage.Freeze();
            return resultBitmapImage;
        }
예제 #4
0
        /// <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();
        }
예제 #5
0
        /// <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();
            }  */
        }