Example #1
0
        public Matrice Feed(double[] entree)
        {
            Matrice val = new Matrice(entree.Length, 1).FromArrayVector(entree);

            input = val.Copy();
            Matrice buffer = couches[0].Feed(input); // Première couche

            if (DEBUG)
            {
                Console.WriteLine("Couche 1 ");
                Console.WriteLine("-----------------------------------------------------");
                couches[0].inputs.Debug("ENTREE");
                couches[0].weights.Debug("POIDS");
            }
            for (int i = 1; i < couches.Length; i++)
            {
                buffer = couches[i].Feed(buffer);
                if (DEBUG)
                {
                    Console.WriteLine("Couche " + Convert.ToString(i + 1)); // On donne la sortie des couches aux couches suivantes
                    Console.WriteLine("-----------------------------------------------------");
                    couches[i].output.Debug("");
                }
            }
            output = buffer.Copy();
            return(buffer);
        }
Example #2
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);
        }
Example #3
0
        public Matrice Vector(Matrice val)
        {
            Matrice sortie = this.Copy();
            Matrice autre  = val.Copy().Transpose(); // On transpose le vecteur (Plus simple)

            for (int i = 0; i < lignes; i++)
            {
                for (int n = 0; n < colonnes; n++)
                {
                    sortie.valeurs[i, n] *= autre.valeurs[0, n]; // On multiplie par le vecteur
                }
            }
            return(sortie);
        }
Example #4
0
        public Matrice MultiplyHorizontalVector(Matrice val)
        {
            // Multiplie de droite à gauche plutot que de haut en bas
            Matrice sortie = this.Copy();
            Matrice autre  = val.Copy(); // On ne transpose pas le vecteur (Plus simple)

            for (int i = 0; i < lignes; i++)
            {
                for (int n = 0; n < colonnes; n++)
                {
                    sortie.valeurs[i, n] *= autre.valeurs[i, 0]; // On multiplie par le vecteur
                }
            }
            return(sortie);
        }
Example #5
0
        public Matrice MultiplyVectors(Matrice val)
        {
            // Nouvelle matrice carré
            Matrice sortie = new Matrice(this.lignes, val.colonnes);
            Matrice ceci   = this.Copy();
            Matrice autre  = val.Copy();

            for (int i = 0; i < sortie.lignes; i++)
            {
                for (int n = 0; n < sortie.colonnes; n++)
                {
                    sortie.valeurs[i, n] = ceci.valeurs[i, 0] * autre.valeurs[0, n]; // On utilise le vecteur pour vaincre le vecteur
                }
            }
            return(sortie);
        }
Example #6
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();
        }