Exemple #1
0
        public Matrice Feed(Matrice i) // i est le vecteur d'entrées
        {
            inputs = i.Copy();
            if (DEBUG)
            {
                inputs.Debug("Entrées");
            }
            Matrice buffer = weights.Multiply(inputs); // On multiplie les entrées par les poids

            if (DEBUG)
            {
                buffer.Debug("Sortie Nette");
            }
            buffer = buffer.Sum(); // On en fait la somme
            if (DEBUG)
            {
                buffer.Debug("Sortie sommée");
            }
            buffer = buffer.Function(Sigmoid); // On fait passer le buffer dans la fonction d'activation (sigmoid)
            if (DEBUG)
            {
                buffer.Debug("Sortie sigmoid");
            }

            output = buffer.Copy();
            return(buffer);
        }
Exemple #2
0
        public void Train(double[] entree, double[] attendu)
        {
            Matrice val = new Matrice(entree.Length, 1).FromArrayVector(entree);

            Matrice Yattendu = new Matrice(attendu.Length, 1).FromArrayVector(attendu);

            this.input = val;
            Matrice Ysortie = Feed(entree);

            int nombre_h = this.couches.Length - 1; // Nombre de couches cachées (donc, sans compter la couche de sortie)

            // Selon la formule E = Yattendu - Ysortie :
            // Calculons l'erreur

            Matrice erreur_sortie = Yattendu.Substract(Ysortie);

            double[] buffer = erreur_sortie.ToArrayVector();
            this.mean_error = buffer.Average();

            // Calcul du gradient

            Matrice gradient = Ysortie.Copy();

            gradient = gradient.Function(DeSigmoid);
            gradient = gradient.MultiplyExact(erreur_sortie);
            gradient = gradient.Scalar(this.learning_rate);

            // Sortie de la couche précédente :
            // this.couches[this.couches.Length - 2].output

            // Calcul du Delta

            Matrice buff = this.couches[this.couches.Length - 2].output.Copy().Transpose();

            Matrice deltas = gradient.MultiplyVectors(buff);

            this.couches[this.couches.Length - 1].buffer = deltas.Copy();
            this.couches[this.couches.Length - 1].UpdateWeights();

            // ------------------------------ COUCHE MILIEU ---------------------
            // ------------------------------ LA BOUCLE COMMENCE ----------------

            Matrice erreur_prec = erreur_sortie.Copy();

            for (int i = 1; i <= nombre_h - 1; i++)
            {
                int index = this.couches.Length - 1 - i;

                // Poids de la couche suivante :
                // this.couches[index + 1].weights (espérons)

                Matrice poids_T = this.couches[index + 1].weights.Transpose();
                erreur_prec = poids_T.Vector(erreur_prec); // Erreur de cette couche

                gradient = this.couches[index].output.Copy();
                gradient = gradient.Function(DeSigmoid);
                gradient = gradient.MultiplyExact(erreur_prec);
                gradient = gradient.Scalar(this.learning_rate);

                buff = this.couches[index - 1].output.Copy().Transpose();

                deltas = gradient.MultiplyVectors(buff);

                this.couches[index].buffer = deltas.Copy();
                this.couches[index].UpdateWeights();
            }

            // ------------------------------ PREMIERE COUCHE ----------------

            // Poids de la couche suivante :
            // this.couches[index + 1].weights (espérons)

            Matrice poids_T_ = this.couches[1].weights.Transpose();

            erreur_prec = poids_T_.Vector(erreur_prec); // Erreur de cette couche

            gradient = this.couches[0].output.Copy();
            gradient = gradient.Function(DeSigmoid);
            gradient = gradient.MultiplyExact(erreur_prec);
            gradient = gradient.Scalar(this.learning_rate);

            buff = this.input.Copy().Transpose();

            deltas = gradient.MultiplyVectors(buff);

            this.couches[0].buffer = deltas.Copy();
            this.couches[0].UpdateWeights();
        }