Пример #1
0
        public void EndLearn()
        {
            //рассчитываю сигмы и, конечно, обучаю выходные нейроны
            //очень важно согласовать здесь все размерности массивов

            //сигмы
            foreach (RBFCell cell in this.HiddenCells)
            {
                double MinDist = double.MaxValue;
                for (int i = 0; i < this.HiddenCells.Count; i++)
                {
                    double dist = cell.Dist(HiddenCells[i].Expectations);
                    if ((dist < MinDist) && (dist != 0))
                    {
                        MinDist = dist;
                    }
                }
                cell.Sigma = MinDist / 2;
                if (cell.Sigma == 0)
                {
                    cell.Sigma = 1;
                }
            }

            //а теперь градиентный спуск
            Random R = new Random();

            for (int i = 0; i < this.HiddenCells.Count(); i++)
            {
                this.OutputCells[i].InputWidth = this.HiddenCells.Count();
                this.OutputCells[i].InitWeights(() => 2 * R.NextDouble() - 1);
                this.OutputCells[i].Iteration = 0;
            }
            for (int i = this.HiddenCells.Count(); i < this.OutputCellCount; i++)
            {
                this.OutputCells[i].InputWidth = 0;
                this.OutputCells[i].InitWeights(() => 0);
            }

            double error;

            //Обучаем

            /*
             * foreach (var pair in this.TrainingSet)
             * {
             *  do
             *  {
             *      foreach (double[] img in pair.Value)
             *      {
             *
             *          double[] ResVect = new double[this.OutputCellCount];
             *          ResVect[pair.Key] = 1;
             *          this.Input = img;
             *          this.DoWork();
             *
             *          for (int i = 0; i < this.OutputCellCount; i++)
             *          {
             *              double err = ResVect[i] - this.OutputCells[i].Output;
             *              for (int w = 0; w < this.OutputCells[i].InputWidth; w++)
             *              {
             *                  this.OutputCells[i].Weights[w] += err * this.OutputCells[i].Input[w] * this.LearnSpeed;
             *                  this.OutputCells[i].Threshoold += err * this.LearnSpeed;
             *                  this.OutputCells[i].Iteration++;
             *              }
             *          }
             *      }
             *      //Проверяем, насколько хорошо усвоена выборка
             *      error = 0;
             *      foreach (double[] img in pair.Value)
             *      {
             *          double[] ResVect = new double[this.OutputCellCount];
             *          ResVect[pair.Key] = 1;
             *          this.Input = img;
             *          this.DoWork();
             *          double err = RBFGrid.Dist(ResVect, this.Output) / 2;
             *          if (err > error) error = err;
             *      }
             *  } while (error > this.MaxErrorValue);
             * }
             */

            //Обучаем
            do
            {
                foreach (var pair in this.TrainingSet)
                {
                    foreach (double[] img in pair.Value)
                    {
                        double[] ResVect = new double[this.OutputCellCount];
                        ResVect[pair.Key] = 1;
                        this.Input        = img;
                        this.DoWork();

                        for (int i = 0; i < this.OutputCellCount; i++)
                        {
                            double err = ResVect[i] - this.OutputCells[i].Output;
                            for (int w = 0; w < this.OutputCells[i].InputWidth; w++)
                            {
                                this.OutputCells[i].Weights[w] += err * this.OutputCells[i].Input[w] * this.LearnSpeed;
                                this.OutputCells[i].Threshoold += err * this.LearnSpeed;
                                this.OutputCells[i].Iteration++;
                            }
                        }
                    }
                }
                //Проверяем, насколько хорошо усвоена выборка
                error = 0;
                foreach (var pair in this.TrainingSet)
                {
                    foreach (double[] img in pair.Value)
                    {
                        double[] ResVect = new double[this.OutputCellCount];
                        ResVect[pair.Key] = 1;
                        this.Input        = img;
                        this.DoWork();
                        double err = RBFGrid.Dist(ResVect, this.Output) / 2;
                        if (err > error)
                        {
                            error = err;
                        }
                    }
                }
            } while (error > this.MaxErrorValue);

            this.Input = null;
        }
Пример #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.recognizer == null)
            {
                this.recognizer = new RBFGrid();
                this.recognizer.InputCellCount = height * width;
                this.recognizer.OutputCellCount = 3;
            }
            if (this.size == 0)
            {
                MessageBox.Show("No image to teach ");
                return;
            }

            int group = 0;
            if (radioButton2.Checked)
            {
                group = 1;
            }
            if (radioButton3.Checked)
            {
                group = 2;
            }

            recognizer.Input = BMPTransform.BitmapToDouble(GetBitmap(draws));
            recognizer.LearnImage(group);
            MessageBox.Show("Success");
        }