Facebook
From Social Echidna, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Zadanie1;
  7.  
  8. namespace PerceptronNumbers
  9. {
  10.     class NumberSamples
  11.     {
  12.         private static Numbers[] numbers = new Numbers[]    
  13.         {
  14.             new Numbers(new int[]
  15.             {
  16.                 -1, 1, 1, 1, -1,
  17.                 -1, 1, -1, 1, -1,
  18.                 -1, 1, -1, 1, -1,
  19.                 -1, 1, -1, 1, -1,
  20.                 -1, 1, 1, 1, -1
  21.             }, 0),
  22.  
  23.             new Numbers(new int[]
  24.             {
  25.                 -1, -1, -1, 1, -1,
  26.                 -1, -1, 1, 1, -1,
  27.                 -1, 1, -1, 1, -1,
  28.                 -1, -1, -1, 1, -1,
  29.                 -1, -1, -1, 1, -1
  30.             }, 1),
  31.  
  32.             new Numbers(new int[]
  33.             {
  34.                 -1, 1, 1, 1, -1,
  35.                 -1, -1, -1, 1, -1,
  36.                 -1, 1, 1, 1, -1,
  37.                 -1, 1, -1, -1, -1,
  38.                 -1, 1, 1, 1, -1
  39.             }, 2),
  40.  
  41.             new Numbers(new int[]
  42.             {
  43.                 -1, 1, 1, 1, -1,
  44.                 -1, -1, -1, 1, -1,
  45.                 -1, 1, 1, 1, -1,
  46.                 -1, -1, -1, 1, -1,
  47.                 -1, 1, 1, 1, -1
  48.             }, 3),
  49.  
  50.             new Numbers(new int[]
  51.             {
  52.                 -1, 1, -1, 1, -1,
  53.                 -1, 1, -1, 1, -1,
  54.                 -1, 1, 1, 1, -1,
  55.                 -1, -1, -1, 1, -1,
  56.                 -1, -1, -1, 1, -1
  57.             }, 4),
  58.  
  59.             new Numbers(new int[]
  60.             {
  61.                 -1, 1, 1, 1, -1,
  62.                 -1, 1, -1, -1, -1,
  63.                 -1, 1, 1, 1, -1,
  64.                 -1, -1, -1, 1, -1,
  65.                 -1, 1, 1, 1, -1
  66.             }, 5),
  67.  
  68.             new Numbers(new int[]
  69.             {
  70.                 -1, 1, 1, 1,- 1,
  71.                 -1, 1, -1, -1, -1,
  72.                 -1, 1, 1, 1, -1,
  73.                 -1, 1, -1, 1, -1,
  74.                 -1, 1, 1, 1, -1
  75.             }, 6),
  76.  
  77.             new Numbers(new int[]
  78.             {
  79.                 -1, 1, 1, 1, -1,
  80.                 -1, -1, -1, 1, -1,
  81.                 -1, -1, -1, 1, -1,
  82.                 -1, -1, -1, 1, -1,
  83.                 -1, -1, -1, 1, -1
  84.             }, 7),
  85.  
  86.             new Numbers(new int[]
  87.             {
  88.                 -1, 1, 1, 1, -1,
  89.                 -1, 1, -1, 1, -1,
  90.                 -1, 1, 1, 1, -1,
  91.                 -1, 1, -1, 1, -1,
  92.                 -1, 1, 1, 1, -1
  93.             }, 8),
  94.  
  95.             new Numbers(new int[]
  96.             {
  97.                 -1, 1, 1, 1, -1 ,
  98.                 -1, 1, -1, 1, -1,
  99.                 -1, 1, 1, 1, -1,
  100.                 -1, -1, -1, 1, -1,
  101.                 -1, -1, -1, 1, -1
  102.             }, 9)
  103.         };
  104.  
  105.         private static Random rand = new Random();
  106.  
  107.         public static double[][] MakeDataForPercepton(int perc)
  108.         {
  109.             double[][] wynik = new double[100][];
  110.             int N = numbers.Length;
  111.             int index = 0;
  112.  
  113.             for (int i = 0; i < 100; i++)
  114.             {
  115.                 // 26 bo potrzebuej rozmair 5x5 planszy, a 26 bo wrzuca wynik w ostatnie miejsce
  116.                 wynik[i] = new double[26];
  117.  
  118.                 // 100 x 26 - 100 próbek dla każego percepronu żeby się lepiej uczył
  119.                 // losuję każda z tych liczb
  120.                 int randNumber = rand.Next(0, N);
  121.  
  122.                 // zamieniam ta liczbe na perceptron
  123.                 // to perceptron sprawdza czy ostatnia liczba tej tablicy jest faktycznie tym numerem, ktory sprawdzam
  124.                 double[] toPerc = numbers[randNumber].ToPerceptron(perc);
  125.  
  126.                 // jezeli doby to 1, jezeli nie to -1
  127.                 double isCorrect = toPerc[toPerc.Length - 1];
  128.  
  129.                 // to jest tablica, ktora jest tym perceptronem, z wycietym wynikiem - czy jest dobry czy zly
  130.                 // skracam tablice - pod przygotowane tablicy pod DFT
  131.                 // taki jakby temp
  132.                 double[] toPercShort = new double[toPerc.Length - 1];
  133.  
  134.                 for (int j = 0; j < toPercShort.Length; j++)
  135.                     toPercShort[j] = toPerc[j];
  136.  
  137.                 toPercShort = DFT.DFT2D(toPercShort, 5);
  138.  
  139.                 for (int j = 0; j < toPercShort.Length; j++)
  140.                     toPerc[j] = toPercShort[j];
  141.  
  142.                 toPerc[toPerc.Length - 1] = isCorrect;
  143.  
  144.                 // dodaje do wyniku ten perceptron i lece dalej
  145.                 wynik[index++] = toPerc;
  146.             }
  147.             return wynik;
  148.         }
  149.     }
  150. }
  151.  
  152. ---------------------------------------------------------------------------------------
  153.   using System;
  154. using System.Collections.Generic;
  155. using System.Linq;
  156. using System.Text;
  157. using System.Threading.Tasks;
  158.  
  159. namespace PerceptronNumbers
  160. {
  161.     class Numbers
  162.     {
  163.         private int[] values;
  164.         private int results;
  165.  
  166.         public Numbers(int[] values, int results)
  167.         {
  168.             this.values = values;
  169.             this.results = results;
  170.         }
  171.  
  172.         public double[] ToPerceptron(double perceptron)
  173.         {
  174.             // +1 żeby w ostatnie miejsce wrzucic wynik (-1 lub 1)
  175.             double[] wynik = new double[values.Length + 1];
  176.  
  177.             for (int i = 0; i < values.Length; i++)
  178.                 wynik[i] = values[i];
  179.  
  180.             if (results == perceptron)
  181.                 wynik[wynik.Length - 1] = 1;
  182.             else
  183.                 wynik[wynik.Length - 1] = -1;
  184.  
  185.             return wynik;
  186.         }
  187.     }
  188. }
  189.