using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zadanie1 { class Numbers { private int[] values; private int results; public Numbers(int[] values, int results) { this.values = values; this.results = results; } public double[] ToPerceptron(double perceptron) { // +1 żeby w ostatnie miejsce wrzucic wynik (-1 lub 1) double[] wynik = new double[values.Length + 1]; for (int i = 0; i < values.Length; i++) wynik[i] = values[i]; if (results == perceptron) wynik[wynik.Length - 1] = 1; else wynik[wynik.Length - 1] = -1; return wynik; } } } ------------------------------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zadanie1 { class NumbersSamples { private static Numbers[] numbers = new Numbers[] { new Numbers(new int[] { -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1 }, 0), new Numbers(new int[] { -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1 }, 1), new Numbers(new int[] { -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1 }, 2), new Numbers(new int[] { -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1 }, 3), new Numbers(new int[] { -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1 }, 4), new Numbers(new int[] { -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1 }, 5), new Numbers(new int[] { -1, 1, 1, 1,- 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1 }, 6), new Numbers(new int[] { -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1 }, 7), new Numbers(new int[] { -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1 }, 8), new Numbers(new int[] { -1, 1, 1, 1, -1 , -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1 }, 9) }; private static Random rand = new Random(); public static double[][] makeDataForPercepton(int perc) { double[][] wynik = new double[100][]; int N = numbers.Length; int index = 0; for(int i = 0; i < 100; i++) { // 26 bo rozmair 5x5 planszy, a 26 bo wrzuca wynik w ostatnie miejsce wynik[i] = new double[26]; // 100 x 26 - 100 próbek dla każego perceptronu żeby się lepiej uczył // losuję każda z tych liczb int randNumber = rand.Next(0, N); // zamieniam ta liczbe na perceptron // to perceptron sprawdza czy ostatnia liczba tej tablicy jest faktycznie tym numerem, ktory sprawdzam double[] toPerc = numbers[randNumber].ToPerceptron(perc); // jezeli doby to 1, jezeli nie to -1 double isCorrect = toPerc[toPerc.Length - 1]; // to jest tablica, ktora jest tym perceptronem, z wycietym wynikiem - czy jest dobry czy zly // skracam tablice - pod przygotowane tablicy pod DFT double[] toPercShort = new double[toPerc.Length - 1]; for (int j = 0; j < toPercShort.Length; j++) toPercShort[j] = toPerc[j]; toPercShort = DFT.DFT2D(toPercShort, 5); for (int j = 0; j < toPercShort.Length; j++) toPerc[j] = toPercShort[j]; toPerc[toPerc.Length - 1] = isCorrect; // dodaje do wyniku ten perceptron i dalej wynik[index++] = toPerc; } return wynik; } } } ------------------------------------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zadanie1 { class Perceptrons { private Perceptron[] perceptrons; private int N = 10; public Perceptrons() { perceptrons = new Perceptron[N]; for (int i = 0; i < N; i++) perceptrons[i] = new Perceptron(); } public void TrainPerceptrons() { for (int i = 0; i < N; i++) { Debug.WriteLine("Zaczynam uczyć: {0}", i); perceptrons[i].Train(NumbersSamples.makeDataForPercepton(i), 30000); } } public List getCorrectPerceptrons(double[] input) { List wynik = new List(); //zliczamy ilosc dobrych porownan, gdy perceptron rozpozna cyfre for(int i = 0; i < N; i++) { if (perceptrons[i].Output(input) > 0) wynik.Add(i); } return wynik; } } } ------------------------------------------------------------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zadanie1 { public class Perceptron { private const int size = 25; private double[] weights = new double[size]; private Random rand = new Random(); public Perceptron() { // losuję wagi weights[i] małe, bliskie 0 for (int i = 0; i < size; i++) weights[i] = rand.NextDouble(); } public double Output(double[] input) //do inputu dodac gdziekolwiek jedynkę { double suma = 0; for (int i = 0; i < size; i++) suma = suma + input[i] * weights[i]; return suma; } public void Train(double[][] data, int iter) { //prog bledu; error,zeby nie uczyl sie w nieskonczonosc double ERROR = 0.000000001; double error = 1; //stala uczenia double learnConst = 0.00001; double index = 0; for (int i = 0; i < iter; i++) { foreach (double[] x in data) { double y = x[x.Length - 1]; for (int l = 0; l < size; l++) weights[l] = weights[l] + learnConst * (y - Output(x)) * x[l]; } error = CheckError(data); if (error < ERROR) break; } } //checkerror z wykładu wzór public double CheckError(double[][] data) { double result = 0; double tmp; double C; foreach (double[] x in data) { tmp = Output(x); C = x[25]; result = result + Math.Pow(tmp - C, 2); } return result; } } }