Facebook
From Mustard Giraffe, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7.  
  8. namespace PerceptronNumbers
  9. {
  10.     class Perceptron
  11.     {  
  12.         Random rnd;
  13.         private double[] weights;
  14.         private double error;
  15.         // ilość dobrych porównań - implikowana gdy perceptron rozpozna cyfrę
  16.         private int countOfCorrectComparisions;
  17.  
  18.         public Perceptron(double[] learnData, double[] learnAnswer, double[] weights)
  19.         {
  20.             rnd = new Random();
  21.  
  22.             // losuję wagi weights[i] małe, bliskie 0
  23.             for (int i = 0; i < weights.Length; i++)
  24.                 weights[i] = rnd.NextDouble();
  25.  
  26.             // losowanie progu błędu
  27.             error = rnd.NextDouble();
  28.  
  29.             // początkowe zerowanie ilości dobrych rozpoznań cyfry przez dany perceptron
  30.             countOfCorrectComparisions = 0;
  31.  
  32.             for (int j = 0; j < 5000; j++)
  33.             {
  34.                 // suma - może być dodatnia lub ujemna
  35.                 double sum = 0;
  36.                 int wynik;
  37.                
  38.                 // output
  39.                 sum = Output(learnData, countOfCorrectComparisions, weights);
  40.  
  41.                 if (sum >= error)
  42.                     wynik = 1;
  43.                 else
  44.                     wynik = -1;
  45.  
  46.                 // póki aktualnie wyliczony wynik pokrywa się z wartościami wiersza tablicy learnAnswer (zbiór uczący)
  47.                 // przekazaną w konstruktorze
  48.                 // zwiększana jest ilość poprawnych rozwiązań
  49.                 // poniższy if-else umożliwia ich zliczanie - gdy ilość wynosi 10 to cyfra jest rozpoznana
  50.                 if (wynik == learnAnswer[countOfCorrectComparisions])
  51.                     countOfCorrectComparisions++;  
  52.                 else
  53.                 {
  54.                     // ponowne wyliczneie, korekta wag
  55.                     for (int i = 0; i < weights.Length; i++)
  56.                         weights[i] = weights[i] + learnAnswer[countOfCorrectComparisions] * learnData[i + 25 * countOfCorrectComparisions];
  57.  
  58.                     countOfCorrectComparisions = 0;
  59.  
  60.                     setWeights(weights);
  61.                     setError(error);
  62.                 }
  63.  
  64.                 // wszystkie perceptrony potrafią rozpoznać te 10 cyfr
  65.                 if (countOfCorrectComparisions >= 10)
  66.                     return;
  67.             }
  68.         }
  69.  
  70.         public double Output(double[] input, int size, double[] weights)
  71.         {
  72.             double suma = 0;
  73.  
  74.             // w  pętli poniżej zliczana jest suma wag dla jednej cyfry w miejscach gdzie jest 1 w modelu
  75.             for (int i = 0 + 25 * size; i < 25 + 25 * size; i++)
  76.                 // weights[i%25] - mnożenie każdej wagi po kolei - przedział 0 - 25 tablicy weights
  77.                 suma = suma + input[i] * weights[i%25];
  78.  
  79.             /*
  80.             MessageBox.Show("i = " + i.ToString());
  81.             MessageBox.Show("warunek pętli = " + (weights.Length + 25 * countOfCorrectComparisions).ToString());
  82.             MessageBox.Show("i%25 = " + (i % 25).ToString());
  83.             MessageBox.Show("learnData[i] = " + (learnData[i]).ToString());
  84.             MessageBox.Show("weights[i % 25] = " + (weights[i % 25]).ToString());
  85.             MessageBox.Show("suma = " + sum.ToString());
  86.             */
  87.  
  88.             return suma;
  89.         }
  90.  
  91.         public double[] getWeights()
  92.         {
  93.             return weights;
  94.         }
  95.  
  96.         public void setWeights(double[] w)
  97.         {
  98.             this.weights = w;
  99.         }
  100.  
  101.         public double getError()
  102.         {
  103.             return error;
  104.         }
  105.  
  106.         public void setError(double err)
  107.         {
  108.             this.error = err;
  109.         }
  110.     }
  111. }
  112.  
  113. ---------------------------------------------------------------------------------------
  114. using System;
  115. using System.Collections.Generic;
  116. using System.Linq;
  117. using System.Text;
  118. using System.Threading.Tasks;
  119.  
  120. namespace PerceptronNumbers
  121. {
  122.     class PerceptronClass
  123.     {
  124.         private const int size = 25;
  125.         private double[] weights = new double[size];
  126.         private Random rand = new Random();
  127.  
  128.         public PerceptronClass()
  129.         {
  130.             for (int i = 0; i < size; i++)
  131.                 weights[i] = rand.NextDouble();
  132.         }
  133.  
  134.         public double Output(double[] input) //do inputu dodac gdziekolwiek jedynkę
  135.         {
  136.             double suma = 0;
  137.  
  138.             for (int i = 0; i < size; i++)
  139.                 suma = suma + input[i] * weights[i];
  140.  
  141.             return suma;
  142.         }
  143.  
  144.         public void Train(double[][] data, int iter)
  145.         {
  146.             // error zeby nie uczyl w nieskonczonosc
  147.             double ERROR = 0.000000001;
  148.             double error = 1;
  149.             // stala uczenia
  150.             double learnConst = 0.00001;
  151.  
  152.             for (int i = 0; i < iter; i++)
  153.             {
  154.                 foreach (double[] x in data)
  155.                 {
  156.                     double y = x[x.Length - 1];
  157.  
  158.                     for (int l = 0; l < size; l++)
  159.                         weights[l] = weights[l] + learnConst * (y - Output(x)) * x[l];
  160.                 }
  161.  
  162.                 error = CheckError(data);
  163.                 if (error < ERROR)
  164.                     break;
  165.             }
  166.         }
  167.  
  168.         // checkerror - wzór z wykładu
  169.         public double CheckError(double[][] data)
  170.         {
  171.             double result = 0;
  172.             double tmp;
  173.             double C;
  174.  
  175.             foreach (double[] x in data)
  176.             {
  177.                 tmp = Output(x);
  178.                 C = x[25];
  179.                 result = result + Math.Pow(tmp - C, 2);
  180.             }
  181.  
  182.             return result;
  183.         }
  184.     }
  185. }
  186.  
  187.