Exemple #1
0
        // CALCULATING TRAINING EXAMPLES ERROR
        public string CalcTrainingError(string tmpString)
        {
            if (network == null)
            {
                return("\n-> Error clearing examples - no network exists.");
            }

            tmpString = tmpString.Trim();

            string result;

            if (tmpString.Length == 0)
            {
                result = network.CalcError();
            }
            else
            {
                int  testExamples = -1;
                bool isLast       = true;

                result = Parsing.ParseInt(ref tmpString, ref testExamples, ref isLast, "showDetails", Parsing.AfterParsingCheck.NonNegative);
                if (result.Length > 0)
                {
                    return(result);
                }

                bool trainTest = testExamples == 0 ? false : true;

                result = network.CalcError(trainTest);
            }

            if (result.Length > 0)
            {
                return(result);
            }

            return("");
        }
Exemple #2
0
        // RUN EXAMPLE
        public string RunExample(string tmpString)
        {
            tmpString = tmpString.Trim();

            // CHECKING EXAMPLES, NETWORK AND INPUT
            if (network == null)
            {
                return("\n-> Error running an example - the network doesn't exist.");
            }

            if (network.examples.SayExamplesNum() == 0)
            {
                return("\n-> Error running an example - no existing examples.");
            }

            if (tmpString.Length == 0)
            {
                return("\n-> Error running an example - not enough input data.");
            }

            // PARSING EXAMPLE INDEX
            int    exampleInd = -1;
            bool   isLast     = true;
            string result     = Parsing.ParseInt(ref tmpString, ref exampleInd, ref isLast, "examplesInd", Parsing.AfterParsingCheck.NonNegative);

            if (result.Length > 0)
            {
                return(result);
            }

            bool allLayersOutputs = false;

            // PARSING OPTION OF PRINTING ALL LAYERS OUTPUTS
            if (!isLast)
            {
                int allLOutputs = -1;
                result = Parsing.ParseInt(ref tmpString, ref allLOutputs, ref isLast, "printAllLayersOutputs", Parsing.AfterParsingCheck.NonNegative);
                if (result.Length > 0)
                {
                    return(result);
                }

                allLayersOutputs = allLOutputs == 0 ? false : true;
            }

            float[][] outputs = null;
            result = network.examples.GiveExampleLink(exampleInd, out Example theExample);
            if (result.Length < 0)
            {
                return(result);
            }

            result = network.React(theExample.SayX(), ref outputs, out int networkAnswer);
            if (result.Length > 0)
            {
                return(result);
            }

            PrintClass.Print("\n\n-> On example N " + exampleInd.ToString() + "\n(" + theExample.SayX(0));
            for (int inputInd = 1; inputInd < theExample.SayXLength(); inputInd++)
            {
                PrintClass.Print(", " + theExample.SayX(inputInd));
            }

            PrintClass.Print(")\n network replied: " + networkAnswer.ToString() + " (" + outputs[outputs.Length - 1][0].ToString());
            for (int outputInd = 1; outputInd < outputs[outputs.Length - 1].Length; outputInd++)
            {
                PrintClass.Print(", " + outputs[outputs.Length - 1][outputInd].ToString());
            }

            PrintClass.Print(").\n while expected is: " + theExample.SayExpected().ToString() + " (" + theExample.SayY(0).ToString());
            for (int outputInd = 1; outputInd < theExample.SayYLength(); outputInd++)
            {
                PrintClass.Print(", " + theExample.SayY(outputInd).ToString());
            }

            PrintClass.Print(").\n");

            if (allLayersOutputs)
            {
                for (int layerInd = 0; layerInd < outputs.Length; layerInd++)
                {
                    PrintClass.Print("\n  - Layer " + layerInd.ToString() + " outputs:\n" + outputs[layerInd][0].ToString());
                    for (int outputInd = 1; outputInd < outputs[layerInd].Length; outputInd++)
                    {
                        PrintClass.Print(", " + outputs[layerInd][outputInd].ToString());
                    }
                }
            }

            return("");
        }
Exemple #3
0
        ////////// *******************************************************************************************************************************
        ////////// *******************************************************************************************************************************
        ////////// NETWORK FUNCTIONS

        // CREATE NEW NETWORK
        public string CreateNewANN(string tmpString, ref ANNetwork externalNetworkLink)
        {
            /* newANN <inputsNumber> <layersNumber> <L1NeuronNumber>...
             * (<alpha> <CostFunctionIndex> {<L1ActivationFunctionIndex>...} {<L1LowerBoundary>... <L1UpperBoundary>...} <scaleBoolean>)*/

            bool   isLast = true;
            string result = "";

            // PARSING INPUTS NUMBER
            if (tmpString.Length == 0)
            {
                return("\n-> Error creating new network: no <inputsNumber> argument entered.");
            }

            int inputsNumber = -1;

            result = Parsing.ParseInt(ref tmpString, ref inputsNumber, ref isLast, "inputsNumber", Parsing.AfterParsingCheck.Positive);

            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            if (isLast)
            {
                return("\n-> Error creating new network: no <layersNumber> argument entered.");
            }


            // PARSING LAYER NUMBER
            int layersNumber = -1;

            result = Parsing.ParseInt(ref tmpString, ref layersNumber, ref isLast, "inputsNumber", Parsing.AfterParsingCheck.Positive);

            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            // PARSING LAYER NEURONS NUMBER FOR EACH LAYER
            int[] neuronNumbers = new int[layersNumber];
            for (int layerInd = 0; layerInd < layersNumber; layerInd++)
            {
                result = Parsing.ParseInt(ref tmpString, ref neuronNumbers[layerInd], ref isLast, "<LxNeuronNumber>", Parsing.AfterParsingCheck.Positive);
                if (result.Length > 0)
                {
                    return("\n-> Error creating new network. " + result);
                }

                if ((isLast) && (layerInd < layersNumber - 1))
                {
                    return("\n-> Error creating new network: no <LxNeuronNumber> for layer " + layerInd.ToString() + " .");
                }
            }

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber);
                externalNetworkLink = network;
                return("");
            }

            // PARSING TRAINING SPEED IF ABLE
            float learningSpeed = -1;

            result = Parsing.ParseFloat(ref tmpString, ref learningSpeed, ref isLast, "trainingSpeed", Parsing.AfterParsingCheck.Positive);
            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed);
                externalNetworkLink = network;
                return("");
            }

            // PARSING ALPHA IF ABLE
            float alpha = -1;

            result = Parsing.ParseFloat(ref tmpString, ref alpha, ref isLast, "alpha", Parsing.AfterParsingCheck.Positive);
            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed, alpha);
                externalNetworkLink = network;
                return("");
            }

            // PARSING LYAMBDA IF ABLE
            float lyambda = -1;

            result = Parsing.ParseFloat(ref tmpString, ref lyambda, ref isLast, "lyambda", Parsing.AfterParsingCheck.NonNegative);
            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed, alpha, lyambda);
                externalNetworkLink = network;
                return("");
            }

            // PARSING COST FUNCTION
            CFTypeBase CFType = null;

            result = Parsing.ParseCFType(ref tmpString, ref CFType, ref isLast);

            if (result.Length > 0)
            {
                return(result);
            }

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed, alpha, lyambda, CFType);
                externalNetworkLink = network;
                return("");
            }

            // PARSING ACTIVATION FUNCTIONS FOR ALL LAYERS
            AFTypeBase[] AFTypes = new AFTypeBase[layersNumber];
            result = Parsing.ParseAFTypesArray(ref tmpString, ref AFTypes, ref isLast);

            if (result.Length > 0)
            {
                return(result);
            }

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed, alpha, lyambda, CFType, AFTypes);
                externalNetworkLink = network;
                return("");
            }

            // PARSING INIT SCALE
            bool initScale;
            int  initScaleInt = -1;

            result = Parsing.ParseInt(ref tmpString, ref initScaleInt, ref isLast, "scaleBoolean", Parsing.AfterParsingCheck.NonNegative);
            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            if ((initScaleInt != 0) && (initScaleInt != 1))
            {
                return("\n-> Error creating new network: non-boolean initScale " + initScaleInt.ToString() + " .");
            }

            initScale = initScaleInt == 0 ? false : true;

            if (isLast)
            {
                network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed, alpha, lyambda, CFType, AFTypes, initScale);
                externalNetworkLink = network;
                return("");
            }

            // PARSING LOWER BOUNDARY AND UPPER BOUNDARY
            float[] lowerBoundaries = new float[layersNumber];
            result = Parsing.ParseFloatArray(ref tmpString, ref lowerBoundaries, ref isLast, "lower boundaries", Parsing.AfterParsingCheck.NoCheck);

            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            if (isLast)
            {
                return("\n-> Error creating new network: no text for upper boundaties ");
            }

            float[] upperBoundaries = new float[layersNumber];
            result = Parsing.ParseFloatArray(ref tmpString, ref upperBoundaries, ref isLast, "upper boundaries", Parsing.AfterParsingCheck.NoCheck);

            if (result.Length > 0)
            {
                return("\n-> Error creating new network. " + result);
            }

            network             = ANNetwork.NewNetwork(neuronNumbers, inputsNumber, learningSpeed, alpha, lyambda, CFType, AFTypes, initScale, lowerBoundaries, upperBoundaries);
            externalNetworkLink = network;

            return("");
        }