Ejemplo n.º 1
0
        public override int Recognize(string filename)
        {
            List <double> x = AlgsWithFiles.binarImage(filename, baseColor);

            for (int i = 0; i < K; ++i)
            {
                double s = 0;

                for (int j = 0; j < M; ++j)
                {
                    s += weightMatrix[i, j] * x[j];
                }

                s += M / 2.0;

                // здесь s - состояние нейрона первого слоя

                outputs[i] = s;
            }

            Array.Copy(outputs, tmpoutputs, outputs.Length);

            double hammingDist;
            int    indx = -1;

            while (true)
            {
                outputs = Matrix.MultiplyOnVector(feedbackSynapse, tmpoutputs);

                hammingDist = 0;

                for (int i = 0; i < K; ++i)
                {
                    outputs[i] = TresholdFun(outputs[i]);

                    hammingDist += Math.Abs(outputs[i] - tmpoutputs[i]);
                }


                if (hammingDist <= emax)
                {
                    double max = outputs[0];
                    indx = 0;

                    for (int i = 1; i < K; ++i)
                    {
                        if (outputs[i] > max)
                        {
                            max  = outputs[i];
                            indx = i;
                        }
                    }
                    break;
                }

                Array.Copy(outputs, tmpoutputs, outputs.Length);
            }
            return(indx);
        }
Ejemplo n.º 2
0
        public override int Recognize(string filename)
        {
            List <double> x = AlgsWithFiles.bipolImage(filename, baseColor);

            double[] fstSignals = Matrix.getDoubleArr(x);
            double[] sndSignals = fstSignals;

            double eps = 0.001;

            for (int i = 0; i < 10000; ++i)
            {
                double err = 0.0;
                sndSignals = Matrix.MultiplyOnVector(weightMatrix, fstSignals);

                for (int j = 0; j < sndSignals.Length; ++j)
                {
                    sndSignals[j] = TresholdFunc(sndSignals[j]);
                }

                for (int j = 0; j < sndSignals.Length; ++j)
                {
                    err += fstSignals[j] * sndSignals[j];
                }

                err        = fstSignals.Length - err;
                fstSignals = sndSignals;

                if (err < eps)
                {
                    break;
                }
            }

            // find result
            double minDist = double.MaxValue;
            int    index   = 0;

            for (int k = 0; k < samples.Count; ++k)
            {
                double dist = 0.0;
                for (int j = 0; j < sndSignals.Length; ++j)
                {
                    dist += samples[k][j] * sndSignals[j];
                }
                dist = fstSignals.Length - dist;
                if (dist < minDist)
                {
                    minDist = dist;
                    index   = k;
                }
            }

            return(index);
        }
Ejemplo n.º 3
0
        // создание сети по названию папки с эталонными изображениями, обучение сети
        public HammingNN(String dirname, Color baseColor)
        {
            List <List <double> > input = AlgsWithFiles.processingFilesInDirectory(dirname, AlgsWithFiles.binarImage, baseColor);

            K = input.Count;
            M = input[0].Count;

            this.baseColor = baseColor;

            outputs    = new double[K];
            tmpoutputs = new double[K];

            weightMatrix    = new double[K, M];
            feedbackSynapse = new double[K][];


            double eps = 1 / (1.2 * K);

            // инициализация матриц весовой и синапсов обр связи
            for (int i = 0; i < K; ++i)
            {
                for (int j = 0; j < M; ++j)
                {
                    weightMatrix[i, j] = input[i][j] / 2.0;
                }

                ////
                feedbackSynapse[i] = new double[K];
                for (int k = 0; k < K; ++k)
                {
                    if (i == k)
                    {
                        feedbackSynapse[i][k] = 1;
                    }
                    else
                    {
                        feedbackSynapse[i][k] = -eps;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public HopfieldNN(String dirname, Color baseColor)
        {
            samples = AlgsWithFiles.processingFilesInDirectory(dirname, AlgsWithFiles.bipolImage, baseColor);

            this.baseColor = baseColor;

            int n = samples[0].Count;

            weightMatrix = new double[n][];

            for (int i = 0; i < n; ++i)
            {
                weightMatrix[i] = new double[n];
                for (int j = 0; j < n; ++j)
                {
                    double sum = 0;
                    for (int k = 0; k < samples.Count; ++k)
                    {
                        sum += samples[k][i] * samples[k][j];
                    }
                    weightMatrix[i][j] = sum / n;
                }
            }
        }