Exemple #1
0
        public void BackPropagationThead()
        {
            try
            {
                uint   compteur = 0;
                double erreur   = 100.0;
                double eta      = 0.90; // learning rate - controls the maginitude of the increase in the change in weights. found by trial and error.
                double alpha    = 0.04;

                byte   Label = 0;
                double dMSE;

                NeuroneSortieListe _memorizedNeuronOutputs = new NeuroneSortieListe();

                double[] VecteurEntree  = new double[841];
                double[] VecteurCible   = new double[10];
                double[] VecteurCourant = new double[10];

                //archive.Serialisation(_RNN);

                //while (erreur > 0.10 * 0.1 && _iEpochsCompleted < 40)
                while (_iEpochsCompleted < 70)
                {
                    // initialisation

                    for (int i = 0; i < 841; i++)
                    {
                        VecteurEntree[i] = 0.0;
                    }

                    for (int i = 0; i < 10; i++)
                    {
                        VecteurCible[i]   = 0.0;
                        VecteurCourant[i] = 0.0;
                    }

                    // Boucle
                    if (Definitions.mnistApprentissageDatabase.indexNextPattern == 0)
                    {
                        m_HiPerfTime.Start();
                        Definitions.mnistApprentissageDatabase.RandomizePatternSequence();
                    }
                    // Randomize pattern
                    // Pad 28 ==> 29

                    byte[] padImage = new byte[29 * 29];

                    //byte[][] padImage = new byte[29][];
                    //for (int i = 0; i < padImage.Length; ++i)
                    //    padImage[i] = new byte[29];


                    uint iPattern = BaseApprentissage.GetNextPattern();
                    Definitions.mnistApprentissageDatabase.result[iPattern].pixels.CopyTo(padImage, 0);
                    Label = Definitions.mnistApprentissageDatabase.result[iPattern].label;


                    // Label = BaseApprentissage.

                    for (int i = 0; i < 841; i++)
                    {
                        VecteurEntree[i] = 1.0;  // 1 Blanc et 0 noir
                    }
                    for (int hauteur = 0; hauteur < 28; ++hauteur)
                    {
                        for (int largeur = 0; largeur < 28; ++largeur)
                        {
                            VecteurEntree[1 + largeur + 29 * (hauteur + 1)] = (double)((int)(byte)padImage[largeur + 28 * hauteur]) / 128.0 - 1.0;
                            //VecteurEntree[1 + largeur + 29 * (hauteur + 1)] = (double)((int)(byte)padImage[hauteur][largeur]) / 128.0 - 1.0;
                        }
                    }

                    for (int i = 0; i < 10; i++)
                    {
                        VecteurCible[i] = -1.0;
                    }

                    VecteurCible[Label] = 1.0;

                    VecteurCourant = BackpropagateNeuralNet(VecteurEntree, VecteurCible, _memorizedNeuronOutputs, _form, this.Distorsion);

                    dMSE = 0.0;
                    for (int i = 0; i < 10; ++i)
                    {
                        dMSE += (VecteurCourant[i] - VecteurCible[i]) * (VecteurCourant[i] - VecteurCible[i]);
                    }
                    dMSE     /= 2.0;
                    _dMSE    += dMSE;
                    _dMSE200 += dMSE;

                    // Calcul seuil d'erreur
                    if (dMSE <= (0.10 * Convert.ToDouble(OCR.Properties.Settings.Default.EstimatedCurrentMSE)))
                    {
                        Backpropagate = false;
                    }
                    else
                    {
                        Backpropagate = true;
                    }

                    _nn++;
                    int    iBestIndex = 0;
                    double maxValue   = -99.0;

                    for (int i = 0; i < 10; ++i)
                    {
                        if (VecteurCourant[i] > maxValue)
                        {
                            iBestIndex = i;
                            maxValue   = VecteurCourant[i];
                        }
                    }

                    if (iBestIndex != Label)
                    {
                        erreurReconnaissances++;
                    }
                    else
                    {
                        bonneReconnaissances++;
                    }

                    // make step
                    string s = "";
                    if (_nn >= 200)
                    {
                        _dMSE200 /= 200;
                        erreur    = _dMSE200;
                        s         = "MSE:" + _dMSE200.ToString();

                        double partielpourcentage = ((float)erreurReconnaissances * 100) / (float)Definitions.mnistApprentissageDatabase.indexNextPattern;

                        _form.Invoke(_form._DelegateAddObject, new Object[] { 2, partielpourcentage });

                        _dMSE200 = 0;
                        _nn      = 0;
                    }

                    s = String.Format("{0} Miss Number:{1} Bonne reo {2}", Convert.ToString(Definitions.mnistApprentissageDatabase.indexNextPattern), erreurReconnaissances, bonneReconnaissances);
                    if (_form != null)
                    {
                        _form.Invoke(_form._DelegateAddObject, new Object[] { 5, s });
                    }

                    if (Definitions.mnistApprentissageDatabase.indexNextPattern >= nombre - 1)
                    {
                        m_HiPerfTime.Stop();
                        _dMSE /= Definitions.mnistApprentissageDatabase.indexNextPattern;
                        Definitions.mnistApprentissageDatabase.indexNextPattern = 0;

                        double pourcentage = ((float)erreurReconnaissances * 100) / (float)Definitions.mnistApprentissageDatabase.imageCount;

                        s = String.Format("Epochs:{0}, Pourcentage:{1}, Temps {2}, erreurs:{3} ",
                                          Convert.ToString(_iEpochsCompleted + 1), Convert.ToString(pourcentage), m_HiPerfTime.Duration, erreurReconnaissances.ToString());

                        if (_form != null)
                        {
                            _form.Invoke(_form._DelegateAddObject, new Object[] { 3, s });
                        }

                        erreurReconnaissances = 0;
                        bonneReconnaissances  = 0;
                        _iEpochsCompleted++;
                        Definitions.mnistApprentissageDatabase.indexNextPattern = 0;
                        _dMSE = 0;
                    }

                    compteur++;

                    if (Backpropagate == true)
                    {
                        Definitions.rnn.Backpropagate(VecteurCourant, VecteurCible, 0, _memorizedNeuronOutputs, nombreBackPropagation);
                    }
                }

                archive.Serialisation(Definitions.rnn);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Exemple #2
0
        public void TestPattern(int pattern)
        {
            // int patternCourant = pattern;
            byte Label;
            int  matrice = Entree * Entree;

            NeuroneSortieListe _memorizedNeuronOutputs = new NeuroneSortieListe();

            double[] VecteurEntree  = new double[matrice];
            double[] VecteurCible   = new double[Sortie];
            double[] VecteurCourant = new double[Sortie];

            //double[] VecteurEntree = new double[841];
            //double[] VecteurCible = new double[10];
            //double[] VecteurCourant = new double[10];

            m_HiPerfTime.Start();

            //for (int i = 0; i < 841; i++)
            for (int i = 0; i < matrice; i++)
            {
                VecteurEntree[i] = 0.0;
            }
            for (int i = 0; i < Sortie; i++)
            {
                VecteurCible[i]   = 0.0;
                VecteurCourant[i] = 0.0;
            }
            byte[] padImage = new byte[29 * 29];

            //uint iPattern = BaseApprentissage.GetNextPattern();

            // patternCourant = (int)iPattern;
            BaseApprentissage.result[pattern].pixels.CopyTo(padImage, 0);
            Label = BaseApprentissage.result[pattern].label;

            for (int i = 0; i < matrice; i++)
            {
                VecteurEntree[i] = 1.0;      // 1 Blanc et 0 noir
            }
            for (int hauteur = 0; hauteur < Entree; ++hauteur)
            {
                for (int largeur = 0; largeur < Entree; ++largeur)
                {
                    VecteurEntree[1 + largeur + 29 * (hauteur + 1)] = (double)((int)(byte)padImage[largeur + 28 * hauteur]) / 128.0 - 1.0;
                    //VecteurEntree[1 + largeur + 29 * (hauteur + 1)] = (double)((int)(byte)padImage[hauteur][largeur]) / 128.0 - 1.0;
                }
            }

            for (int i = 0; i < Sortie; i++)
            {
                VecteurCible[i] = -1.0;
            }

            VecteurCible[Label] = 1.0;

            VecteurCourant = BackpropagateNeuralNet(VecteurEntree, VecteurCible, _memorizedNeuronOutputs, Label, _form);

            int    iBestIndex = 0;
            double maxValue   = -99.0;

            for (int i = 0; i < Sortie; ++i)
            {
                if (VecteurCourant[i] > maxValue)
                {
                    iBestIndex = i;
                    maxValue   = VecteurCourant[i];
                }
            }


            string s = "";

            s = "lblChiffreReconnu : " + iBestIndex.ToString();

            if (_form != null)
            {
                _form.Invoke(_form._DelegateAddObject, new Object[] { 8, s });
            }

            if (_form != null)
            {
                _form.Invoke(_form._DelegateAddObject, new Object[] { 9, VecteurCourant });
            }
        }