Example #1
0
 public void Activation(Functions.ActivationFunction activation)
 {
     for (int el = 0; el < m_Data.Length; el++)
     {
         m_Data[el] = activation(m_Data[el]);
     }
 }
Example #2
0
        public Perceptron(
            int maxIter,
            double learningRate,
            double[] input,
            double[] targetVector,
            Functions.ActivationFunction activation)
        {
            m_MaxNumOfIterations = maxIter;

            m_TargetVector = new double[targetVector.Length];
            System.Array.Copy(targetVector, m_TargetVector, targetVector.Length);

            double[]      weights = new double[input.Length];
            System.Random rand    = new Random();
            for (int el = 0; el < input.Length; el++)
            {
                weights[el] = rand.NextDouble() / 4;
            }

            m_Neurons = new Neuron[input.Length];
            for (int el = 0; el < input.Length; el++)
            {
                m_Neurons[el] = new Neuron(learningRate, input[el], weights[el], activation);
            }
        }
Example #3
0
        public void Test(
            double learningRate,
            Functions.ActivationFunction activation)
        {
            m_LearningRate = learningRate;
            Array.Copy(m_Weights, m_OldWights, m_Weights.Length);
            Learn(activation);

            double output = 0.0;

            m_Outputs = new double[m_DataMatrix.m_NumberOfRows];
            for (int row = 0; row < m_DataMatrix.m_NumberOfRows; row++)
            {
                output = 0.0;
                for (int col = 0; col < (m_DataMatrix.m_NumberOfColumns); col++)
                {
                    output +=
                        m_DataMatrix[row +
                                     col * m_DataMatrix.m_NumberOfRows]
                        * m_Weights[col];
                }
                output         = activation(output);
                m_Outputs[row] = output;
            }
        }
Example #4
0
        public void Learn(Functions.ActivationFunction activation)
        {
            //xi*wi
            Matrix result = new Matrix(m_DataMatrix.m_NumberOfRows,
                                       m_DataMatrix.m_NumberOfColumns, m_DataMatrix.m_Data);

            m_NumberOfIterations = 0;
            double output = 0.0;
            double gerror = 1.0;

            while (gerror != 0)
            {
                if (m_NumberOfIterations >= m_MaxNumOfIterations)
                {
                    System.Console.WriteLine($"Number of iterations: {m_NumberOfIterations}");
                    return;
                }

                gerror = 0.0;
                if (m_DataMatrix.m_NumberOfColumns == m_Weights.Length)
                {
                    for (int row = 0; row < m_DataMatrix.m_NumberOfRows; row++)
                    {
                        output = 0.0;

                        for (int col = 0; col < (m_DataMatrix.m_NumberOfColumns); col++)
                        {
                            output +=
                                m_DataMatrix[row + col * m_DataMatrix.m_NumberOfRows]
                                * m_Weights[col];
                        }
                        output = activation(output);
                        double error = m_TargetVector[row] - output;

                        if (error != 0)
                        {
                            gerror += error;

                            for (int index = 0;
                                 index < m_Weights.Length;
                                 index++)
                            {
                                m_Weights[index] +=
                                    m_LearningRate * error
                                    * m_DataMatrix.m_Data[row + index * m_DataMatrix.m_NumberOfRows];
                                ;
                            }
                        }
                    }
                }
                else
                {
                    //
                }
                ++m_NumberOfIterations;
            }
            System.Console.WriteLine($"Number of iterations: {m_NumberOfIterations}");
        }
Example #5
0
 public Neuron(
     double learningRate,
     double input, double weight,
     Functions.ActivationFunction activation)
 {
     LearningRate = learningRate;
     Input        = input;
     Weight       = weight;
     d_Activation = activation;
 }
Example #6
0
        /// <summary>
        /// Initialisation des neurones de la couche
        /// </summary>
        /// <param name="pFonctionTransfert"></param>
        /// <remarks>Créée le29/12/2016 par : JF Enond </remarks>
        private void InitLstNeurones(Functions.ActivationFunction pFonctionTransfert)
        {
            // Liste des neurones de la couche
            _LstNeurones = new Neurone[_NbreNeurones];

            for (int i = 0; i < _LstNeurones.Length; i++)
            {
                _LstNeurones[i] = new Neurone(i, _NbreEntreesParNeurone, pFonctionTransfert);
            }
        }
Example #7
0
        public Couche(int pNum,
                      int pNbreNeurones,
                      int pNbreSortiesCouchePrecedente,
                      Functions.ActivationFunction pFonctionTransfert)
        {
            _Num                   = pNum;
            _NbreNeurones          = pNbreNeurones;
            _NbreEntreesParNeurone = pNbreSortiesCouchePrecedente;

            InitLstNeurones(pFonctionTransfert);
        }
Example #8
0
        public ReseauNeurones(int pNbreEntrees,
                              int[] pLstLongueursCouches,
                              Functions.ActivationFunction pFonctionTransfert)
        {
            _NbreEntrees          = pNbreEntrees;
            _NbreCouches          = pLstLongueursCouches.Length;
            _LstLongueursCouches  = pLstLongueursCouches;
            _NomFonctionTransfert = pFonctionTransfert.GetType().ToString();

            InitLstCouches(pLstLongueursCouches, pFonctionTransfert);

            _DossierFichiers = new Dossier();
        }
Example #9
0
        public Neurone(int pId, int pNbreEntrees, Functions.ActivationFunction pFonctionTransfert)
        {
            _Id = pId;

            _NbreEntrees = pNbreEntrees;
            _LstPoids    = new List <double>(_NbreEntrees + BIAIS);
            InitPoids();

            _FonctionTransfert = pFonctionTransfert;

            _Sortie   = double.NaN;
            _DeltaErr = double.NaN;

            _iBiais = _NbreEntrees + BIAIS - 1;
        }
Example #10
0
        /// <summary>
        /// Initialisation de la liste des couches du réseau
        /// </summary>
        /// <param name="pLstLongueursCouches">Liste des longueurs des couches</param>
        /// <param name="pFonctionTransfert">Fonction de transfert</param>
        private void InitLstCouches(int[] pLstLongueursCouches, Functions.ActivationFunction pFonctionTransfert)
        {
            _LstCouches = new Couche[_NbreCouches];

            for (int i = 0; i < _NbreCouches; i++)
            {
                // 1 neurone = 1 sortie - 1 neurone a autant d'entrées qu'il de sorties de neurones sur la couche précédente.
                // Donc Nbre d'entrées par neurone = nombre de neurones de la couche précédente.
                int nbreEntreesParNeurone = 0;
                if (i == 0)       // Première couche
                {
                    nbreEntreesParNeurone = _NbreEntrees;
                }
                else          // Couches suivantes
                {
                    nbreEntreesParNeurone = pLstLongueursCouches[i - 1];
                }

                _LstCouches[i] = new Couche(i, pLstLongueursCouches[i], nbreEntreesParNeurone, pFonctionTransfert);
            }
        }
Example #11
0
        public void Test(double learningRate, Functions.ActivationFunction activation)
        {
            m_LearningRate = learningRate;

            for (int col = 0; col < m_Weights.Length; col++)
            {
                m_Weights[col].Assign(m_OldWights[col]);
            }

            Learn(activation);
            double output = 0.0;

            m_Output = new double[m_DataMatrix.m_NumberOfRows];

            for (int row = 0; row < m_DataMatrix.m_NumberOfRows; row++)
            {
                for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                {
                    m_Hidden[row + col * m_Hidden.m_NumberOfRows] +=
                        m_DataMatrix[row + col * m_DataMatrix.m_NumberOfRows]
                        * m_Weights[0][row + col * m_Weights[0].m_NumberOfRows];

                    m_Hidden[row + col * m_Hidden.m_NumberOfRows] =
                        activation(m_Hidden[row + col * m_Hidden.m_NumberOfRows]);
                }

                output = 0.0;
                for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                {
                    output +=
                        m_Hidden[row + col * m_DataMatrix.m_NumberOfRows] *
                        m_Weights[1][row + col * m_Weights[1].m_NumberOfRows];
                }
                output        = activation(output);
                m_Output[row] = output;
            }
        }
Example #12
0
        public void Learn(Functions.ActivationFunction activation)
        {
            //xi*wi
            Matrix result = new Matrix(m_DataMatrix.m_NumberOfRows,
                                       m_DataMatrix.m_NumberOfColumns, m_DataMatrix.m_Data);

            m_NumberOfIterations = 0;
            double output = 0.0;
            double gerror = 1.0;

            double[] errors = new double[m_DataMatrix.m_NumberOfColumns];

            while (gerror != 0)
            {
                if (m_NumberOfIterations >= m_MaxNumOfIterations)
                {
                    System.Console.WriteLine($"Number of iterations: {m_NumberOfIterations}");
                    return;
                }

                gerror = 0.0;
                for (int row = 0; row < m_DataMatrix.m_NumberOfRows; row++)
                {
                    for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                    {
                        m_Hidden[row + col * m_Hidden.m_NumberOfRows] +=
                            m_DataMatrix[row + col * m_DataMatrix.m_NumberOfRows]
                            * m_Weights[0][row + col * m_Weights[0].m_NumberOfRows];

                        m_Hidden[row + col * m_Hidden.m_NumberOfRows] =
                            activation(m_Hidden[row + col * m_Hidden.m_NumberOfRows]);
                    }

                    output = 0.0;
                    for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                    {
                        output +=
                            m_Hidden[row + col * m_DataMatrix.m_NumberOfRows] *
                            m_Weights[1][row + col * m_Weights[1].m_NumberOfRows];
                    }
                    output = activation(output);

                    errors = new double[m_DataMatrix.m_NumberOfColumns];

                    double lErr = m_Target[row] - output;
                    gerror += Math.Abs(lErr);

                    for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                    {
                        errors[col] = lErr * m_Weights[1][row + col * m_Weights[1].m_NumberOfRows];
                    }

                    for (int r = 0; r < m_DataMatrix.m_NumberOfColumns; r++)
                    {
                        for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                        {
                            m_Weights[0][r + col * m_Weights[0].m_NumberOfRows] +=
                                m_LearningRate *
                                errors[col] *
                                m_DataMatrix[row + r * m_DataMatrix.m_NumberOfRows];
                        }
                    }

                    for (int col = 0; col < m_DataMatrix.m_NumberOfColumns; col++)
                    {
                        m_Weights[1][row + col * m_Weights[1].m_NumberOfRows] +=
                            m_LearningRate *
                            lErr *
                            m_Hidden[row + col * m_Hidden.m_NumberOfRows];
                    }
                }

                ++m_NumberOfIterations;
            }
            System.Console.WriteLine($"Number of iterations: {m_NumberOfIterations}");
        }