Facebook
From Melodic Curlew, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Zadanie1
  8. {
  9.     class Numbers
  10.     {
  11.         private int[] values;
  12.         private int results;
  13.  
  14.         public Numbers(int[] values, int results)
  15.         {
  16.             this.values = values;
  17.             this.results = results;
  18.         }
  19.  
  20.         public double[] ToPerceptron(double perceptron)
  21.         {
  22.             // +1 żeby w ostatnie miejsce wrzucic wynik (-1 lub 1)
  23.             double[] wynik = new double[values.Length + 1];
  24.             for (int i = 0; i < values.Length; i++) wynik[i] = values[i];
  25.             if (results == perceptron) wynik[wynik.Length - 1] = 1;
  26.             else wynik[wynik.Length - 1] = -1;
  27.  
  28.             return wynik;
  29.         }
  30.  
  31.     }
  32. }
  33.  
  34.  
  35. -------------------------------------------------------------------------------------------------------------------------------
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Diagnostics;
  39. using System.Linq;
  40. using System.Text;
  41. using System.Threading.Tasks;
  42.  
  43. namespace Zadanie1
  44. {
  45.     class NumbersSamples
  46.     {
  47.         private static Numbers[] numbers = new Numbers[]    {
  48.             new Numbers(new int[]
  49.             {
  50.                 -1, 1, 1, 1, -1,
  51.                 -1, 1, -1, 1, -1,
  52.                 -1, 1, -1, 1, -1,
  53.                 -1, 1, -1, 1, -1,
  54.                 -1, 1, 1, 1, -1
  55.             }, 0),
  56.  
  57.             new Numbers(new int[]
  58.             {
  59.                 -1, -1, -1, 1, -1,
  60.                 -1, -1, 1, 1, -1,
  61.                 -1, 1, -1, 1, -1,
  62.                 -1, -1, -1, 1, -1,
  63.                 -1, -1, -1, 1, -1
  64.             }, 1),
  65.  
  66.             new Numbers(new int[]
  67.             {
  68.                 -1, 1, 1, 1, -1,  
  69.                 -1, -1, -1, 1, -1,
  70.                 -1, 1, 1, 1, -1,
  71.                 -1, 1, -1, -1, -1,
  72.                 -1, 1, 1, 1, -1
  73.             }, 2),
  74.  
  75.             new Numbers(new int[]
  76.             {
  77.                 -1, 1, 1, 1, -1,
  78.                 -1, -1, -1, 1, -1,
  79.                 -1, 1, 1, 1, -1,
  80.                 -1, -1, -1, 1, -1,
  81.                 -1, 1, 1, 1, -1
  82.             }, 3),
  83.  
  84.             new Numbers(new int[]
  85.             {
  86.                 -1, 1, -1, 1, -1,
  87.                 -1, 1, -1, 1, -1,
  88.                 -1, 1, 1, 1, -1,
  89.                 -1, -1, -1, 1, -1,
  90.                 -1, -1, -1, 1, -1
  91.             }, 4),
  92.  
  93.             new Numbers(new int[]
  94.             {
  95.                 -1, 1, 1, 1, -1,
  96.                 -1, 1, -1, -1, -1,
  97.                 -1, 1, 1, 1, -1,
  98.                 -1, -1, -1, 1, -1,
  99.                 -1, 1, 1, 1, -1
  100.             }, 5),
  101.  
  102.             new Numbers(new int[]
  103.             {
  104.                 -1, 1, 1, 1,- 1,
  105.                 -1, 1, -1, -1, -1,
  106.                 -1, 1, 1, 1, -1,
  107.                 -1, 1, -1, 1, -1,
  108.                 -1, 1, 1, 1, -1
  109.             }, 6),
  110.  
  111.             new Numbers(new int[]
  112.             {
  113.                 -1, 1, 1, 1, -1,
  114.                 -1, -1, -1, 1, -1,
  115.                 -1, -1, -1, 1, -1,
  116.                 -1, -1, -1, 1, -1,
  117.                 -1, -1, -1, 1, -1
  118.             }, 7),
  119.  
  120.             new Numbers(new int[]
  121.             {
  122.                 -1, 1, 1, 1, -1,
  123.                 -1, 1, -1, 1, -1,
  124.                 -1, 1, 1, 1, -1,
  125.                 -1, 1, -1, 1, -1,
  126.                 -1, 1, 1, 1, -1
  127.             }, 8),
  128.  
  129.             new Numbers(new int[]
  130.             {
  131.                 -1, 1, 1, 1, -1 ,
  132.                 -1, 1, -1, 1, -1,
  133.                 -1, 1, 1, 1, -1,
  134.                 -1, -1, -1, 1, -1,
  135.                 1, 1, 1, 1, -1
  136.             }, 9)
  137.         };
  138.  
  139.         private static Random rand = new Random();
  140.  
  141.         public static double[][] makeDataForPercepton(int perc)
  142.         {
  143.             double[][] wynik = new double[100][];
  144.             int N = numbers.Length;
  145.             int index = 0;
  146.  
  147.             for(int i = 0; i < 100; i++)
  148.             {
  149.                 // 26 bo rozmair 5x5 planszy, a 26 bo wrzuca wynik w ostatnie miejsce
  150.                 wynik[i] = new double[26];
  151.  
  152.                 // 100 x 26 - 100 próbek dla każego perceptronu żeby się lepiej uczył
  153.                 // losuję każda z tych liczb
  154.                 int randNumber = rand.Next(0, N);
  155.  
  156.                 // zamieniam ta liczbe na perceptron
  157.                 // to perceptron sprawdza czy ostatnia liczba tej tablicy jest faktycznie tym numerem, ktory sprawdzam
  158.                 double[] toPerc = numbers[randNumber].ToPerceptron(perc);
  159.  
  160.                 // jezeli doby to 1, jezeli nie to -1
  161.                 double isCorrect = toPerc[toPerc.Length - 1];
  162.  
  163.                 // to jest tablica, ktora jest tym perceptronem, z wycietym wynikiem - czy jest dobry czy zly
  164.                 // skracam tablice - pod przygotowane tablicy pod DFT
  165.                 double[] toPercShort = new double[toPerc.Length - 1];
  166.  
  167.                 for (int j = 0; j < toPercShort.Length; j++) toPercShort[j] = toPerc[j];
  168.                 toPercShort = DFT.DFT2D(toPercShort, 5);
  169.  
  170.                 for (int j = 0; j < toPercShort.Length; j++) toPerc[j] = toPercShort[j];
  171.                 toPerc[toPerc.Length - 1] = isCorrect;
  172.  
  173.              
  174.                 // dodaje do wyniku ten perceptron i dalej
  175.                 wynik[index++] = toPerc;
  176.             }
  177.  
  178.             return wynik;
  179.         }
  180.     }
  181. }
  182.  
  183. -------------------------------------------------------------------------------------------------------------------------------------
  184. using System;
  185. using System.Collections.Generic;
  186. using System.Diagnostics;
  187. using System.Linq;
  188. using System.Text;
  189. using System.Threading.Tasks;
  190.  
  191.  
  192. namespace Zadanie1
  193. {
  194.     class Perceptrons
  195.     {
  196.         private Perceptron[] perceptrons;
  197.         private int N = 10;
  198.  
  199.         public Perceptrons()
  200.         {
  201.             perceptrons = new Perceptron[N];
  202.             for (int i = 0; i < N; i++) perceptrons[i] = new Perceptron();
  203.         }
  204.  
  205.         public void TrainPerceptrons()
  206.         {
  207.             for (int i = 0; i < N; i++)
  208.             {
  209.                 Debug.WriteLine("Zaczynam uczyć: {0}", i);
  210.                 perceptrons[i].Train(NumbersSamples.makeDataForPercepton(i), 30000);
  211.             }
  212.         }
  213.  
  214.         public List<int> getCorrectPerceptrons(double[] input)
  215.         {
  216.             List<int> wynik = new List<int>();
  217.             //zliczamy ilosc dobrych porownan, gdy perceptron rozpozna cyfre
  218.             for(int i = 0; i < N; i++)
  219.             {
  220.                 if (perceptrons[i].Output(input) > 0) wynik.Add(i);
  221.             }
  222.  
  223.             return wynik;
  224.         }
  225.  
  226.     }
  227. }
  228.  
  229. ------------------------------------------------------------------------------------------------------------------------------------
  230. using System;
  231. using System.Collections.Generic;
  232. using System.Diagnostics;
  233. using System.Linq;
  234. using System.Text;
  235. using System.Threading.Tasks;
  236.  
  237. namespace Zadanie1
  238. {
  239.     public class Perceptron
  240.     {
  241.         private const int size = 25;
  242.         private double[] weights = new double[size];
  243.         private Random rand = new Random();
  244.  
  245.         public Perceptron()
  246.         {
  247.             // losuję wagi weights[i] małe, bliskie 0
  248.             for (int i = 0; i < size; i++) weights[i] = rand.NextDouble();
  249.         }
  250.  
  251.         public double Output(double[] input) //do inputu dodac gdziekolwiek jedynkę
  252.         {
  253.  
  254.             double suma = 0;
  255.  
  256.             for (int i = 0; i < size; i++) suma = suma + input[i] * weights[i];
  257.             return suma;
  258.         }
  259.  
  260.         public void Train(double[][] data, int iter)
  261.         {
  262.             //prog bledu; error,zeby nie uczyl sie w nieskonczonosc
  263.             double ERROR = 0.000000001;
  264.             double error = 1;
  265.  
  266.             //stala uczenia
  267.             double learnConst = 0.00001;
  268.             double index = 0;
  269.  
  270.             for (int i = 0; i < iter; i++)
  271.             {
  272.                 foreach (double[] x in data)
  273.                 {
  274.                     double y = x[x.Length - 1];
  275.  
  276.                     for (int l = 0; l < size; l++) weights[l] = weights[l] + learnConst * (y - Output(x)) * x[l];
  277.                 }
  278.              
  279.                 error = CheckError(data);
  280.  
  281.                 if (error < ERROR) break;
  282.             }
  283.         }
  284.         //checkerror z wykładu wzór
  285.         public double CheckError(double[][] data)
  286.         {
  287.             double result = 0;
  288.             double tmp;
  289.             double C;
  290.  
  291.             foreach (double[] x in data)
  292.             {
  293.                 tmp = Output(x);
  294.                 C = x[25];
  295.                 result = result + Math.Pow(tmp - C, 2);
  296.             }
  297.  
  298.             return result;
  299.         }
  300.     }
  301. }
  302.