Exemplo n.º 1
0
        // CHANGE ACTIVATION FUNCTIONS
        public string SetAFTypes(string tmpString)
        {
            // CHECKING NETWORK
            if (network == null)
            {
                return(noNetworkMessage);
            }

            // CHECKING INPUTED STRING
            if (tmpString.Length == 0)
            {
                return("\n-> Error changing network's cost function type: no input text.");
            }

            AFTypeBase[] newAFTypes = new AFTypeBase[network.SayLayerNum()];
            bool         isLast     = false;

            string result = Parsing.ParseAFTypesArray(ref tmpString, ref newAFTypes, ref isLast);

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

            result = network.SetAFTypes(newAFTypes);
            if (result.Length > 0)
            {
                return(result);
            }

            return("\n-> Network layers' activation function types changed successfuly.");
        }
Exemplo n.º 2
0
 /// <summary>
 /// Sets new layer's AFTypeBase successor for activation function realization.
 /// </summary>
 /// <param name="newAFType">New AFTypeBase successor.</param>
 public void SetAFType(AFTypeBase newAFType)
 {
     // IF NEW AFTYPE IS NOT NOCHANGE - SAVE IT
     if (newAFType.ToString() != AFTypeNoChange.name)
     {
         AFType = newAFType;
     }
 }
Exemplo n.º 3
0
        ////////// PRIVATE METHODS


        ////////// PROTECTED METHODS


        ////////// CONSTRUCTORS
        /// <summary>
        /// Constractor of a network layer.
        /// </summary>
        /// <param name="neuronNum">Number of neurons in new layer.</param>
        /// <param name="inputNum">Number of input in new layer.</param>
        /// <param name="AFType">Activation function AFTypeBase successor for new layer.</param>
        /// <param name="lowerBoundary">Lower boundary for coefficient initialization, <i>default</i> is 0.</param>
        /// <param name="upperBoundary">Lower boundary for coefficient initialization, <i>default</i> is 0.5.</param>
        /// <param name="scale">A scale for coefficients while initialization, <i>default</i> is 0 for no scaling.</param>
        public ANNLayer(int neuronNum, int inputNum, AFTypeBase AFType, float lowerBoundary = 0, float upperBoundary = 0.5f, float scale = 1)
        {
            SetNeuronsNum(neuronNum);
            SetInputsNum(inputNum);
            SetAFType(AFType);
            string result = Initialize(lowerBoundary, upperBoundary, scale);

            if (result.Length > 0)
            {
                throw new NotImplementedException(result);
            }
        }
Exemplo n.º 4
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("");
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads layer parameters from the given file stream (StreamReader).
        /// </summary>
        /// <param name="fileForReading">File stream for reading parameters (StreamReader).</param>
        /// <returns>Empty string on success or error message.</returns>
        public string LoadFromFile(System.IO.StreamReader fileForReading)
        {
            try
            {
                // READING GENERAL LAYER INFO
                string tmpString = "";
                tmpString = fileForReading.ReadLine(); // Reading AFType
                switch (int.Parse(tmpString))
                {
                case 1:
                    AFType = new AFTypeStep();
                    break;

                case 2:
                    AFType = new AFTypeLinear();
                    break;

                case 3:
                    AFType = new AFTypeSigmoid();
                    break;

                case 4:
                    AFType = new AFTypeTanh();
                    break;

                case 5:
                    AFType = new AFTypeSoftMax();
                    break;

                case 6:
                    AFType = new AFTypeRectifiedLinear();
                    break;

                default:
                    return("Error loading a layer info from file. Unknown AFType: " + tmpString + " .");
                }

                tmpString = fileForReading.ReadLine(); // Reading neurons number
                if (int.Parse(tmpString) <= 0)
                {
                    return("Error loading a layer info from file. Not positive neurons number: " + tmpString + " .");
                }
                else
                {
                    SetNeuronsNum(int.Parse(tmpString));
                }

                tmpString = fileForReading.ReadLine(); // Reading inputs number
                if (int.Parse(tmpString) <= 0)
                {
                    return("Error loading a layer info from file. Not positive inputs number: " + tmpString + " .");
                }
                else
                {
                    SetInputsNum(int.Parse(tmpString));
                }

                // INITIALIZING LAYER PARAMETERS FOR DELTAS INIT - NO NEED IN MANUAL INIT AFTER BIASES AND COEFFICIENTS READ
                Initialize(0, 1, 0);

                // READING BIASES
                tmpString = fileForReading.ReadLine(); // Reading all biases in a line, devided by " "
                for (int i = 0; i < neuronDeltas.Length; i++)
                {
                    biases[i] = float.Parse(tmpString.Remove(tmpString.IndexOf(" ")));
                    tmpString = tmpString.Remove(0, tmpString.IndexOf(" ") + 1);
                }

                // READING COEFFICIENTS
                for (int i = 0; i < neuronDeltas.Length; i++)
                {
                    tmpString = fileForReading.ReadLine(); // Reading all i-th neuron coefficients in a line, devided by " "
                    for (int j = 0; j < coeffs[i].Length; j++)
                    {
                        coeffs[i][j] = float.Parse(tmpString.Remove(tmpString.IndexOf(" ")));
                        tmpString    = tmpString.Remove(0, tmpString.IndexOf(" ") + 1);
                    }
                }
            }
            catch (Exception e)
            {
                return("Error loading a layer info from file. Exeption raised with message: " + e.Message);
            }
            return("");
        }