Example #1
0
        static void Main(string[] args)
        {
            /*TemporalNetwork network = new TemporalNetwork(ActivationFunctions.Sine, 4, 4, 0, 1, 1);
             * network.AddInterConnectedLayer(0, 5);
             * network.AddInterConnectedLayer(0, 2);
             * network.AddInterConnectedLayer(1, 1);
             * network.AddInterConnectedLayer(0, 3);
             * double lastCost = 5;
             * for (int a = 0; a < 1000; a++)
             * {
             *  network.Train(new double[] { 1, 2, 1 }, new double[] { 1 }, .27, lastCost, out lastCost);
             * }*/

            NeuronalNetwork n = new NeuronalNetwork(2, ActivationFunctions.Sine, new int[] { 4, 5, 3, 1 });
            List <double[]> X = new List <double[]>();
            List <double[]> Y = new List <double[]>();

            X.Add(new double[] { 0, 0 });
            Y.Add(new double[] { 1 });

            X.Add(new double[] { 0, 1 });
            Y.Add(new double[] { 0 });

            X.Add(new double[] { 1, 1 });
            Y.Add(new double[] { 1 });

            X.Add(new double[] { 1, 0 });
            Y.Add(new double[] { 0 });

            List <double[]> inputs, expecteds = new List <double[]>();

            inputs = new List <double[]>();
            for (int g = 0; g < 200; g++)
            {
                int index = new Random(g).Next(X.Count);
                inputs.Add(X[index]);
                expecteds.Add(Y[index]);
            }
            n.Train(inputs, expecteds, 24);

            /*while (true)
             * {
             *  Console.WriteLine("Mutating...");
             *  for (int i = 0; i < 20; i++)
             *  {
             *      network.MutateNetwork();
             *  }
             *  Console.Clear();
             *  double[] exeRes = network.ExecuteNetwork(new double[] { 1, 2, 1, 5 });
             *  string exeResS = string.Empty;
             *  foreach (var item in exeRes)
             *  {
             *      exeResS += item + " ";
             *  }
             *  Console.WriteLine(exeResS);
             *  ConsoleKeyInfo key = Console.ReadKey();
             *  if (key == new ConsoleKeyInfo('r', ConsoleKey.R, false, false, false))
             *  {
             *      Console.WriteLine("\n" + network.GetRepresentation());
             *      Console.ReadKey();
             *  }
             * }*/
        }
Example #2
0
        static void Main(string[] args)
        {
            string location = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var    images   = GetImagesFromFile(location, "train", 60000);

            Stopwatch watch = new Stopwatch();

            watch.Start();

            long   iterations = 0;
            long   errors     = 0;
            double errorRate  = 1;
            double epsilon    = 1;

            Random rand = new Random();
            var    nn   = new NeuronalNetwork(784, 89, 10);

            double[] target = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            // Train Network until desired epsilon is reached
            while (iterations <= 60000)
            {
                iterations++;
                int randInd = rand.Next(images.Count);

                int label = images[randInd].GetLabel();

                target[label] = 1;
                var res = nn.Train(images[randInd].GetData(), target);
                target[label] = 0;

                if (label != res)
                {
                    errors++;
                }

                if (iterations % 1000 == 0)
                {
                    errorRate = errors / 1000.0;
                    errors    = 0;

                    if (label != res)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                    }

                    epsilon = nn.CalculateEpsilon(label);

                    Console.Write("Expected: " + label + " -  was: " + res + "   errorRate: " + errorRate.ToString("0.0000") + " Epsilon: " + epsilon.ToString("0.000000") + "\n");

                    Console.Write("\n");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }

            watch.Stop();
            var    ts          = watch.Elapsed;
            string elapsedTime = $"{ts.Hours} Hours, {ts.Minutes} Minutes, {ts.Seconds} Seconds, {ts.Milliseconds} ms";

            images.Clear();
            images = GetImagesFromFile(location, "t10k", 10000);

            errors    = 0;
            errorRate = 1;

            int[,] confusionMatrix = new int[10, 10];

            for (int i = 0; i < images.Count; i++)
            {
                int res   = nn.FeedForward(images[i].GetData());
                int label = images[i].GetLabel();

                if (res != label)
                {
                    errors++;
                }

                confusionMatrix[label, res]++;
            }

            string timePerPicture = (ts.TotalMilliseconds / iterations).ToString("N2");

            PrintResult(confusionMatrix, timePerPicture, elapsedTime);

            Console.ReadKey();
        }