Facebook
From Morose Bison, 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.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Zadanie1
  9. {
  10.     public class Perceptron
  11.     {
  12.         private const int size = 25;
  13.         private double[] weights = new double[size];
  14.         private Random rand = new Random();
  15.  
  16.         public Perceptron()
  17.         {
  18.             // losuję wagi weights[i] małe, bliskie 0
  19.             for (int i = 0; i < size; i++) weights[i] = rand.NextDouble();
  20.         }
  21.  
  22.         public double Output(double[] input) //do inputu dodac gdziekolwiek jedynkę
  23.         {
  24.  
  25.             double suma = 0;
  26.  
  27.             for (int i = 0; i < size; i++) suma = suma + input[i] * weights[i];
  28.             return suma;
  29.         }
  30.  
  31.         public void Train(double[][] data, int iter)
  32.         {
  33.             //prog bledu; error,zeby nie uczyl sie w nieskonczonosc
  34.             double ERROR = 0.000000001;
  35.             double error = 1;
  36.  
  37.             //stala uczenia
  38.             double learnConst = 0.00001;
  39.             double index = 0;
  40.  
  41.             for (int i = 0; i < iter; i++)
  42.             {
  43.                 foreach (double[] x in data)
  44.                 {
  45.                     double y = x[x.Length - 1];
  46.  
  47.                     for (int l = 0; l < size; l++) weights[l] = weights[l] + learnConst * (y - Output(x)) * x[l];
  48.                 }
  49.              
  50.                 error = CheckError(data);
  51.  
  52.                 if (error < ERROR) break;
  53.             }
  54.         }
  55.         //checkerror z wykładu wzór
  56.         public double CheckError(double[][] data)
  57.         {
  58.             double result = 0;
  59.             double tmp;
  60.             double C;
  61.  
  62.             foreach (double[] x in data)
  63.             {
  64.                 tmp = Output(x);
  65.                 C = x[25];
  66.                 result = result + Math.Pow(tmp - C, 2);
  67.             }
  68.  
  69.             return result;
  70.         }
  71.     }
  72. }
  73.  
  74. -----------------------------------------------------------------------------------------------------------------------------
  75. using System;
  76. using System.Collections.Generic;
  77. using System.Diagnostics;
  78. using System.Linq;
  79. using System.Text;
  80. using System.Threading.Tasks;
  81.  
  82.  
  83. namespace Zadanie1
  84. {
  85.     class Perceptrons
  86.     {
  87.         private Perceptron[] perceptrons;
  88.         private int N = 10;
  89.  
  90.         public Perceptrons()
  91.         {
  92.             perceptrons = new Perceptron[N];
  93.             for (int i = 0; i < N; i++) perceptrons[i] = new Perceptron();
  94.         }
  95.  
  96.         public void TrainPerceptrons()
  97.         {
  98.             for (int i = 0; i < N; i++)
  99.             {
  100.                 Debug.WriteLine("Zaczynam uczyć: {0}", i);
  101.                 perceptrons[i].Train(NumbersSamples.makeDataForPercepton(i), 30000);
  102.             }
  103.         }
  104.  
  105.         public List<int> getCorrectPerceptrons(double[] input)
  106.         {
  107.             List<int> wynik = new List<int>();
  108.             //zliczamy ilosc dobrych porownan, gdy perceptron rozpozna cyfre
  109.             for(int i = 0; i < N; i++)
  110.             {
  111.                 if (perceptrons[i].Output(input) > 0) wynik.Add(i);
  112.             }
  113.  
  114.             return wynik;
  115.         }
  116.  
  117.     }
  118. }
  119.