Esempio n. 1
0
        /// <summary>
        /// Creates Kohonen network architecture with specified number of neurons in
        /// input and map layer
        /// </summary>
        /// <param name="inputNeuronsCount">
        ///            number of neurons in input layer </param>
        /// <param name="outputNeuronsCount">
        ///            number of neurons in output layer </param>
        private void createNetwork(int inputNeuronsCount, int outputNeuronsCount)
        {
            // specify input neuron properties (use default: weighted sum input with
            // linear transfer)
            NeuronProperties inputNeuronProperties = new NeuronProperties();

            // specify map neuron properties
            NeuronProperties outputNeuronProperties = new NeuronProperties(typeof(Neuron), typeof(Difference), typeof(Linear));             // transfer function -  input function -  neuron type

            // set network type
            this.NetworkType = NeuralNetworkType.KOHONEN;

            // createLayer input layer
            Layer inLayer = LayerFactory.createLayer(inputNeuronsCount, inputNeuronProperties);

            this.addLayer(inLayer);

            // createLayer map layer
            Layer mapLayer = LayerFactory.createLayer(outputNeuronsCount, outputNeuronProperties);

            this.addLayer(mapLayer);

            // createLayer full connectivity between input and output layer
            ConnectionFactory.fullConnect(inLayer, mapLayer);

            // set network input and output cells
            NeuralNetworkFactory.DefaultIO = this;

            this.LearningRule = new KohonenLearning();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates MultiLayerPerceptron Network architecture - fully connected
        /// feed forward with specified number of neurons in each layer
        /// </summary>
        /// <param name="neuronsInLayers">  collection of neuron numbers in getLayersIterator </param>
        /// <param name="neuronProperties"> neuron properties </param>
        private void createNetwork(List <int> neuronsInLayers, NeuronProperties neuronProperties)
        {
            // set network type
            this.NetworkType = NeuralNetworkType.MULTI_LAYER_PERCEPTRON;

            // create input layer
            NeuronProperties inputNeuronProperties = new NeuronProperties(typeof(InputNeuron), typeof(Linear));
            Layer            layer = LayerFactory.createLayer(neuronsInLayers[0], inputNeuronProperties);

            bool useBias = true;             // use bias neurons by default

            if (neuronProperties.hasProperty("useBias"))
            {
                useBias = (bool)neuronProperties.getProperty("useBias");
            }

            if (useBias)
            {
                layer.addNeuron(new BiasNeuron());
            }

            this.addLayer(layer);

            // create layers
            Layer prevLayer = layer;

            //for(Integer neuronsNum : neuronsInLayers)
            for (int layerIdx = 1; layerIdx < neuronsInLayers.Count; layerIdx++)
            {
                int neuronsNum = neuronsInLayers[layerIdx];
                // createLayer layer
                layer = LayerFactory.createLayer(neuronsNum, neuronProperties);

                if (useBias && (layerIdx < (neuronsInLayers.Count - 1)))
                {
                    layer.addNeuron(new BiasNeuron());
                }

                // add created layer to network
                this.addLayer(layer);
                // createLayer full connectivity between previous and this layer
                if (prevLayer != null)
                {
                    ConnectionFactory.fullConnect(prevLayer, layer);
                }

                prevLayer = layer;
            }

            // set input and output cells for network
            NeuralNetworkFactory.DefaultIO = this;

            // set learnng rule
            //        this.setLearningRule(new BackPropagation());
            this.LearningRule = new MomentumBackpropagation();
            // this.setLearningRule(new DynamicBackPropagation());

            this.randomizeWeights(new RangeRandomizer(-0.7, 0.7));
        }
Esempio n. 3
0
        /// <summary>
        /// Creates perceptron architecture with specified number of neurons in input
        /// and output layer, specified transfer function
        /// </summary>
        /// <param name="inputNeuronsCount">
        ///            number of neurons in input layer </param>
        /// <param name="outputNeuronsCount">
        ///            number of neurons in output layer </param>
        /// <param name="transferFunctionType">
        ///            neuron transfer function type </param>
        private void createNetwork(int inputNeuronsCount, int outputNeuronsCount, TransferFunctionType transferFunctionType)
        {
            // set network type
            this.NetworkType = NeuralNetworkType.PERCEPTRON;

            // init neuron settings for input layer
            NeuronProperties inputNeuronProperties = new NeuronProperties();

            inputNeuronProperties.setProperty("transferFunction", TransferFunctionType.Linear.ToString());

            // create input layer
            Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, inputNeuronProperties);

            this.addLayer(inputLayer);

            NeuronProperties outputNeuronProperties = new NeuronProperties();

            outputNeuronProperties.setProperty("neuronType", typeof(ThresholdNeuron));
            outputNeuronProperties.setProperty("thresh", Math.Abs(new Random(1).NextDouble()));
            outputNeuronProperties.setProperty("transferFunction", transferFunctionType.ToString());
            // for sigmoid and tanh transfer functions set slope propery
            outputNeuronProperties.setProperty("transferFunction.slope", 1);

            // createLayer output layer
            Layer outputLayer = LayerFactory.createLayer(outputNeuronsCount, outputNeuronProperties);

            this.addLayer(outputLayer);

            // create full conectivity between input and output layer
            ConnectionFactory.fullConnect(inputLayer, outputLayer);

            // set input and output cells for this network
            NeuralNetworkFactory.DefaultIO = this;

            this.LearningRule = new BinaryDeltaRule();
            // set appropriate learning rule for this network
            //		if (transferFunctionType == TransferFunctionType.STEP) {
            //			this.setLearningRule(new BinaryDeltaRule(this));
            //		} else if (transferFunctionType == TransferFunctionType.SIGMOID) {
            //			this.setLearningRule(new SigmoidDeltaRule(this));
            //		} else if (transferFunctionType == TransferFunctionType.TANH) {
            //			this.setLearningRule(new SigmoidDeltaRule(this));
            //		} else {
            //			this.setLearningRule(new PerceptronLearning(this));
            //		}
        }
Esempio n. 4
0
        /// <summary>
        /// Creates RBFNetwork architecture with specified number of neurons in input
        /// layer, output layer and transfer function
        /// </summary>
        /// <param name="inputNeuronsCount">
        ///		number of neurons in input layer </param>
        /// <param name="rbfNeuronsCount">
        ///		number of neurons in rbf layer </param>
        /// <param name="outputNeuronsCount">
        ///		number of neurons in output layer </param>
        private void createNetwork(int inputNeuronsCount, int rbfNeuronsCount, int outputNeuronsCount)
        {
            // init neuron settings for this network
            NeuronProperties rbfNeuronProperties = new NeuronProperties();

            rbfNeuronProperties.setProperty("inputFunction", typeof(Difference));
            rbfNeuronProperties.setProperty("transferFunction", util.TransferFunctionType.Gaussian.ToString());

            // set network type code
            this.NetworkType = NeuralNetworkType.RBF_NETWORK;

            // create input layer
            Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, TransferFunctionType.Linear);

            this.addLayer(inputLayer);

            // create rbf layer
            Layer rbfLayer = LayerFactory.createLayer(rbfNeuronsCount, rbfNeuronProperties);

            this.addLayer(rbfLayer);

            // create output layer
            Layer outputLayer = LayerFactory.createLayer(outputNeuronsCount, TransferFunctionType.Linear);

            this.addLayer(outputLayer);

            // create full conectivity between input and rbf layer
            ConnectionFactory.fullConnect(inputLayer, rbfLayer);
            // create full conectivity between rbf and output layer
            ConnectionFactory.fullConnect(rbfLayer, outputLayer);

            // set input and output cells for this network
            NeuralNetworkFactory.DefaultIO = this;

            // set appropriate learning rule for this network
            this.LearningRule = new RBFLearning();
        }
        // build example network for student classification
        private void createStudentNFR(int inputNum, List <int> inputSets, int outNum, double[][] pointsSets, double[][] timeSets)
        {
            // set network type
            this.NetworkType = NeuralNetworkType.NEURO_FUZZY_REASONER;

            // createLayer input layer
            NeuronProperties neuronProperties = new NeuronProperties();
            Layer            inLayer          = LayerFactory.createLayer(inputNum, neuronProperties);

            this.addLayer(inLayer);

            // createLayer fuzzy set layer
            neuronProperties.setProperty("transferFunction", TransferFunctionType.Trapezoid.ToString());
            IEnumerator <int> e = inputSets.GetEnumerator();
            int fuzzySetsNum    = 0;

            while (e.MoveNext())
            {
                int i = e.Current;
                fuzzySetsNum = fuzzySetsNum + (int)i;
            }
            Layer setLayer = LayerFactory.createLayer(fuzzySetsNum, neuronProperties);

            this.addLayer(setLayer);

            // TODO: postavi parametre funkcija pripadnosti
            // nizove sa trning elementima iznesi van klase i prosledjuj ih kao
            // parametre
            //		Iterator<Neuron> ii = setLayer.getNeuronsIterator();
            IEnumerator <int> en;            // =setLayer.neurons();
            int c = 0;

            foreach (Neuron cell in setLayer.Neurons)
            {
                //		while (ii.hasNext()) {
                //			Neuron cell = ii.next();
                Trapezoid tf = (Trapezoid)cell.TransferFunction;

                if (c <= 3)
                {
                    tf.LeftLow   = pointsSets[c][0];
                    tf.LeftHigh  = pointsSets[c][1];
                    tf.RightLow  = pointsSets[c][3];
                    tf.RightHigh = pointsSets[c][2];
                }
                else
                {
                    tf.LeftLow   = timeSets[c - 4][0];
                    tf.LeftHigh  = timeSets[c - 4][1];
                    tf.RightLow  = timeSets[c - 4][3];
                    tf.RightHigh = timeSets[c - 4][2];
                }
                c++;
            }

            // povezi prvi i drugi sloj
            int s = 0;                         // brojac celija sloja skupova (fazifikacije)

            for (int i = 0; i < inputNum; i++) // brojac ulaznih celija
            {
                Neuron from = inLayer.getNeuronAt(i);
                int    jmax = (int)inputSets[i];
                for (int j = 0; j < jmax; j++)
                {
                    Neuron to = setLayer.getNeuronAt(s);
                    ConnectionFactory.createConnection(from, to, 1);
                    s++;
                }
            }

            // ----------------------------------------------------------

            // createLayer rules layer
            NeuronProperties ruleNeuronProperties = new NeuronProperties(typeof(Neuron), typeof(WeightedSum), typeof(Linear));

            en = inputSets.GetEnumerator();
            int fuzzyAntNum = 1;

            while (en.MoveNext())
            {
                int i = en.Current;
                fuzzyAntNum = fuzzyAntNum * (int)i;
            }
            Layer ruleLayer = LayerFactory.createLayer(fuzzyAntNum, ruleNeuronProperties);

            this.addLayer(ruleLayer);

            int scIdx = 0;                     // set cell index

            for (int i = 0; i < inputNum; i++) // brojac ulaza (grupa fuzzy
            {
                // skupova)
                int setsNum = (int)inputSets[i];

                for (int si = 0; si < setsNum; si++)                 // brojac celija fuzzy
                {
                    // skupova
                    if (i == 0)
                    {
                        Neuron from        = setLayer.getNeuronAt(si);
                        int    connPerCell = fuzzyAntNum / setsNum;
                        scIdx = si;

                        for (int k = 0; k < connPerCell; k++)                         // brojac celija
                        {
                            // hipoteza
                            Neuron to = ruleLayer.getNeuronAt(si * connPerCell + k);
                            ConnectionFactory.createConnection(from, to, 1);
                        }                 // for
                    }                     // if
                    else
                    {
                        scIdx++;
                        Neuron from        = setLayer.getNeuronAt(scIdx);
                        int    connPerCell = fuzzyAntNum / setsNum;

                        for (int k = 0; k < connPerCell; k++)                         // brojac celija
                        {
                            // hipoteza
                            int    toIdx = si + k * setsNum;
                            Neuron to    = ruleLayer.getNeuronAt(toIdx);
                            ConnectionFactory.createConnection(from, to, 1);
                        } // for k
                    }     // else
                }         // for si
            }             // for i

            // kreiraj izlazni sloj
            neuronProperties = new NeuronProperties();
            neuronProperties.setProperty("transferFunction", TransferFunctionType.Step.ToString());
            Layer outLayer = LayerFactory.createLayer(outNum, neuronProperties);

            this.addLayer(outLayer);

            ConnectionFactory.fullConnect(ruleLayer, outLayer);

            // inicijalizuj ulazne i izlazne celije
            NeuralNetworkFactory.DefaultIO = this;

            this.LearningRule = new LMS();
        }
        /// <summary>
        /// Creates custom NFR architecture
        /// </summary>
        /// <param name="inputNum">
        ///            number of getInputsIterator </param>
        /// <param name="inputSets">
        ///            input fuzzy sets </param>
        /// <param name="outNum">
        ///            number of outputs </param>
        private void createNetwork(int inputNum, List <int> inputSets, int outNum)
        {
            // set network type
            this.NetworkType = NeuralNetworkType.NEURO_FUZZY_REASONER;

            // CREATE INPUT LAYER
            NeuronProperties neuronProperties = new NeuronProperties();
            Layer            inLayer          = LayerFactory.createLayer(inputNum, neuronProperties);

            this.addLayer(inLayer);

            // CREATE FUZZY SET LAYER
            neuronProperties.setProperty("transferFunction", TransferFunctionType.Trapezoid.ToString());
            IEnumerator <int> e = inputSets.GetEnumerator();
            int fuzzySetsNum    = 0;

            while (e.MoveNext())
            {
                int i = e.Current;
                fuzzySetsNum = fuzzySetsNum + (int)i;
            }
            Layer setLayer = LayerFactory.createLayer(fuzzySetsNum, neuronProperties);

            this.addLayer(setLayer);

            // TODO: postavi parametre funkcija pripadnosti
            // nizove sa trning elementima iznesi van klase i prosledjuj ih kao
            // parametre
            //		Iterator<Neuron> ii = setLayer.getNeuronsIterator();
            IEnumerator <int> en;            // =setLayer.neurons();
            int c = 0;

            foreach (Neuron cell in setLayer.Neurons)
            {
                //		while (ii.hasNext()) {
                //			Neuron cell = ii.next();
                Trapezoid tf = (Trapezoid)cell.TransferFunction;

                /*
                 * if (c<=3) { tf.setLeftLow(pointsSets[c][0]);
                 * tf.setLeftHigh(pointsSets[c][1]); tf.setRightLow(pointsSets[c][3]);
                 * tf.setRightHigh(pointsSets[c][2]); } else { tf.setLeftLow(timeSets[c-4][0]);
                 * tf.setLeftHigh(timeSets[c-4][1]); tf.setRightLow(timeSets[c-4][3]);
                 * tf.setRightHigh(timeSets[c-4][2]); } c++;
                 */
            }

            // createLayer connections between input and fuzzy set getLayersIterator
            int s = 0;                         // brojac celija sloja skupova (fazifikacije)

            for (int i = 0; i < inputNum; i++) // brojac ulaznih celija
            {
                Neuron from = inLayer.getNeuronAt(i);
                int    jmax = (int)inputSets[i];
                for (int j = 0; j < jmax; j++)
                {
                    Neuron to = setLayer.getNeuronAt(s);
                    ConnectionFactory.createConnection(from, to, 1);
                    s++;
                }
            }

            // ----------------------------------------------------------

            // kreiraj sloj pravila
            neuronProperties.setProperty("inputFunction", typeof(Min));
            neuronProperties.setProperty("transferFunction", util.TransferFunctionType.Linear.ToString());
            en = inputSets.GetEnumerator();
            int fuzzyAntNum = 1;

            while (en.MoveNext())
            {
                int i = en.Current;
                fuzzyAntNum = fuzzyAntNum * (int)i;
            }
            Layer ruleLayer = LayerFactory.createLayer(fuzzyAntNum, neuronProperties);

            this.addLayer(ruleLayer);

            // povezi set i rule layer

            int scIdx = 0;                     // set cell index

            for (int i = 0; i < inputNum; i++) // brojac ulaza (grupa fuzzy
            {
                // skupova)
                int setsNum = (int)inputSets[i];

                for (int si = 0; si < setsNum; si++)                 // brojac celija fuzzy
                {
                    // skupova
                    if (i == 0)
                    {
                        Neuron from        = setLayer.getNeuronAt(si);
                        int    connPerCell = fuzzyAntNum / setsNum;
                        scIdx = si;

                        for (int k = 0; k < connPerCell; k++)                         // brojac celija
                        {
                            // hipoteza
                            Neuron to = ruleLayer.getNeuronAt(si * connPerCell + k);
                            ConnectionFactory.createConnection(from, to, 1);
                        }                 // for
                    }                     // if
                    else
                    {
                        scIdx++;
                        Neuron from        = setLayer.getNeuronAt(scIdx);
                        int    connPerCell = fuzzyAntNum / setsNum;

                        for (int k = 0; k < connPerCell; k++)                         // brojac celija
                        {
                            // hipoteza
                            int    toIdx = si + k * setsNum;
                            Neuron to    = ruleLayer.getNeuronAt(toIdx);
                            ConnectionFactory.createConnection(from, to, 1);
                        } // for k
                    }     // else
                }         // for si
            }             // for i

            // set input and output cells for this network
            neuronProperties = new NeuronProperties();
            neuronProperties.setProperty("transferFunction", TransferFunctionType.Step.ToString());
            Layer outLayer = LayerFactory.createLayer(outNum, neuronProperties);

            this.addLayer(outLayer);

            ConnectionFactory.fullConnect(ruleLayer, outLayer);

            // set input and output cells for this network
            NeuralNetworkFactory.DefaultIO = this;

            this.LearningRule = new LMS();
        }