Пример #1
0
        private void LearnSession(int bestof)
        {
            if (bestNet == null)
            {
                bestNet = new NeuralNet(11, 6, NUM_NEURONS, 1, new Random());
            }
            bestNet.LearnSession(lessonplan, bestof);

            Console.WriteLine("-\t-\t- FINAL -\t-\t-\n\nSUCCESS\t\tACC [MIN " + NeuralNet.ACC_THRESHOLD.ToString("f2") + "]");
            Console.WriteLine(bestNet.Test(new float[] { 4, 3, 2, 3, 4, 5 }, new float[] { 0 }));
            Console.WriteLine(bestNet.Test(new float[] { 4, 4, 2, 2, 3, 3 }, new float[] { 0 }));
            Console.WriteLine(bestNet.Test(new float[] { 9, 9, 9, 9, 9, 9 }, new float[] { 0 }));
            Console.WriteLine(bestNet.Test(new float[] { 7, 8, 2, 3, 4, 5 }, new float[] { 1 }));
            Console.WriteLine(bestNet.Test(new float[] { 0, 8, 2, 3, 4, 1 }, new float[] { 1 }));
            Console.WriteLine(bestNet.Test(new float[] { 5, 3, 2, 0, 9, 1 }, new float[] { 1 }));

            Console.WriteLine(bestNet.Test(new float[] { 1, 2, 9, 8, 4, 5 }, new float[] { 1 }));
            Console.WriteLine(bestNet.Test(new float[] { 1, 0, 2, 3, 4, 6 }, new float[] { 1 }));
            Console.WriteLine(bestNet.Test(new float[] { 8, 7, 7, 8, 8, 7 }, new float[] { 0 }));

            Console.WriteLine("\nDone! Type 'help' for commands.\n");
            string cmd;

            while ((cmd = Console.ReadLine().ToLower()) != "")
            {
                string[] args = cmd.Split(' ');
                Commands(args);
            }
        }
Пример #2
0
        private void Commands(string[] args)
        {
            string path = "";

            switch (args[0].ToLower())
            {
            case "new":
                bestNet = null;
                //net.bestScore = 0;
                Console.WriteLine("Best network cleared.");
                break;

            case "test":
                float[] vals   = new float[args.Length - 1];
                bool    failed = false;
                for (int i = 0; i < vals.Length; i++)
                {
                    if (failed = !float.TryParse(args[i + 1], out vals[i]))
                    {
                        break;
                    }
                }
                if (failed)
                {
                    Console.WriteLine("Parsing inputs failed.");
                    break;
                }
                float[] result = bestNet.Test(vals);
                Console.Write("Result: ");
                for (int i = 0; i < result.Length; i++)
                {
                    Console.WriteLine(result[i]);
                }
                break;

            case "info":
                if (bestNet == null)
                {
                    Console.WriteLine("Neural network is empty.");
                    break;
                }
                Console.Write("\nInputs: {0}\nOutputs: {1}\nLayers: ", bestNet.array[0].array.Length, bestNet.array[bestNet.array.Length - 1].array.Length);
                for (int i = 1; i < bestNet.array.Length - 2; i++)
                {
                    Console.Write(bestNet.array[i].array.Length + " ");
                }
                Console.WriteLine("\nCurrent best score: {0}\nCurrent %ACC: {1}", bestNet.bestScore, bestNet.learnLen != 0 ? (bestNet.bestScore / (bestNet.learnLen * 2) * 100) + "%" : "Unknown");
                break;

            case "save":
                if (args.Length < 2)
                {
                    Console.WriteLine("Please specify a file path. Example: C:\\dir\\MyNeuralNet");
                    break;
                }
                if (bestNet == null)
                {
                    Console.WriteLine("Cannot save an empty network.");
                    break;
                }
                args[0] = "";
                path    = string.Join(" ", args).Trim();
                if (!path.EndsWith(".znn"))
                {
                    path += ".znn";
                }

                bestNet.SaveNet(path);

                Console.WriteLine("Saved to file {0}\n", path);
                break;

            case "load":
                args[0] = "";
                bool noBest = false;
                if (args[1] == "--noscore" || args[1] == "-ns")
                {
                    noBest  = true;
                    args[1] = "";
                }
                path = string.Join(" ", args).Trim();
                if (!path.EndsWith(".znn"))
                {
                    path += ".znn";
                }
                if (NeuralNet.LoadNet(ref bestNet, path))
                {
                    if (noBest)
                    {
                        bestNet.bestScore = 0;
                    }
                    Console.WriteLine(path + " loaded.\n");
                }
                else
                {
                    Console.WriteLine("File does not exist! {0}\n", path);
                }
                break;

            case "learn":
                int bestof;
                Console.WriteLine("Best of " + args[1]);
                if (int.TryParse(args[1], out bestof))
                {
                    LearnSession(bestof);
                }
                break;

            case "help":
                Console.WriteLine("\nhelp\n- Shows this help dialog.\n\n" +
                                  "new\n- Clears the current best neural network so a new one can be created.\n\n" +
                                  "save [file]\n- Saves the 'best' neural network to [file].\n\n" +
                                  "load [-ns] [file]\n- Loads the 'best' neural network from [file].\n-ns : Load without the 'best score.'\n\n" +
                                  "test [inputs]\n- Tests the neural network against the inputs and returns its outputs. Missing inputs are 0. Excess inputs are truncated.\n\n" +
                                  "learn [x]\n- Sends the current neural network through [x] training sessions, or creates one.\n\n" +
                                  "info\n- Gives information about the current neural network.\n\n" +
                                  "exit\n- Quits the program.\n");
                break;

            case "exit":
                Environment.Exit(0);
                break;

            default:
                Console.WriteLine("Unknown command.");
                break;
            }
        }