using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace PerceptronNumbers
{
class Perceptron
{
Random rnd;
private double[] weights;
private double error;
// ilość dobrych porównań - implikowana gdy perceptron rozpozna cyfrę
private int countOfCorrectComparisions;
public Perceptron(double[] learnData, double[] learnAnswer, double[] weights)
{
// losuję wagi weights[i] małe, bliskie 0
for (int i = 0; i < weights.Length; i++)
weights[i] = rnd.NextDouble();
// losowanie progu błędu
error = rnd.NextDouble();
// początkowe zerowanie ilości dobrych rozpoznań cyfry przez dany perceptron
countOfCorrectComparisions = 0;
for (int j = 0; j < 5000; j++)
{
// suma - może być dodatnia lub ujemna
double sum = 0;
int wynik;
// output
sum = Output(learnData, countOfCorrectComparisions, weights);
if (sum >= error)
wynik = 1;
else
wynik = -1;
// póki aktualnie wyliczony wynik pokrywa się z wartościami wiersza tablicy learnAnswer (zbiór uczący)
// przekazaną w konstruktorze
// zwiększana jest ilość poprawnych rozwiązań
// poniższy if-else umożliwia ich zliczanie - gdy ilość wynosi 10 to cyfra jest rozpoznana
if (wynik == learnAnswer[countOfCorrectComparisions])
countOfCorrectComparisions++;
else
{
// ponowne wyliczneie, korekta wag
for (int i = 0; i < weights.Length; i++)
weights[i] = weights[i] + learnAnswer[countOfCorrectComparisions] * learnData[i + 25 * countOfCorrectComparisions];
countOfCorrectComparisions = 0;
setWeights(weights);
setError(error);
}
// wszystkie perceptrony potrafią rozpoznać te 10 cyfr
if (countOfCorrectComparisions >= 10)
return;
}
}
public double Output(double[] input, int size, double[] weights)
{
double suma = 0;
// w pętli poniżej zliczana jest suma wag dla jednej cyfry w miejscach gdzie jest 1 w modelu
for (int i = 0 + 25 * size; i < 25 + 25 * size; i++)
// weights[i%25] - mnożenie każdej wagi po kolei - przedział 0 - 25 tablicy weights
suma = suma + input[i] * weights[i%25];
/*
MessageBox.Show("i = " + i.ToString());
MessageBox.Show("warunek pętli = " + (weights.Length + 25 * countOfCorrectComparisions).ToString());
MessageBox.Show("i%25 = " + (i % 25).ToString());
MessageBox.Show("learnData[i] = " + (learnData[i]).ToString());
MessageBox.Show("weights[i % 25] = " + (weights[i % 25]).ToString());
MessageBox.Show("suma = " + sum.ToString());
*/
return suma;
}
public double[] getWeights()
{
return weights;
}
public void setWeights(double[] w)
{
this.weights = w;
}
public double getError()
{
return error;
}
public void setError(double err)
{
this.error = err;
}
}
}
---------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PerceptronNumbers
{
class PerceptronClass
{
private const int size = 25;
private double[] weights
= new double[size
];
private Random rand
= new Random
();
public PerceptronClass()
{
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)
{
// error zeby nie uczyl w nieskonczonosc
double ERROR = 0.000000001;
double error = 1;
// stala uczenia
double learnConst = 0.00001;
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 - wzór z wykładu
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;
}
}
}