Skip to content

Instantly share code, notes, and snippets.

@brpereyra
Created July 9, 2018 18:35
Show Gist options
  • Save brpereyra/39f599ef31ff6b73ebd1eedac822302b to your computer and use it in GitHub Desktop.
Save brpereyra/39f599ef31ff6b73ebd1eedac822302b to your computer and use it in GitHub Desktop.
using System;
using System.Text;
namespace App1
{
class Perceptron
{
//peceptron simple
static void Main(string[] args)
{
Random rand = new Random();
bool aprendiendo = true;
int salidaEntera=0;
int iteraciones = 0;
int[,] datos ={
// x1, x2, Y
{1,1,1},
{1,0,0},
{0,1,0},
{0,0,0}
};
//w1 w2 b
double[] pesos = { rand.NextDouble(), rand.NextDouble(), rand.NextDouble()};
while (aprendiendo)
{
iteraciones++;
aprendiendo = !aprendiendo;
for (int i = 0; i <= 3; i++)
{
double salidaReal = datos[i, 0] * pesos[0] + datos[i, 1] * pesos[1] + pesos[2];
//funcion escalon activacion ESCALONADA
salidaEntera = (salidaReal > 0) ? 1 : 0;
//Cambios en los pesos de la neurona
if (salidaEntera != datos[i, 2])
{
pesos[0] = rand.NextDouble() - rand.NextDouble();
pesos[1] = rand.NextDouble() - rand.NextDouble();
pesos[2] = rand.NextDouble() - rand.NextDouble();
aprendiendo = true;
}
}
}
Console.WriteLine("Peso 1:{0}",pesos[0]);
Console.WriteLine("Peso 2:{0}",pesos[1]);
Console.WriteLine("Peso b:{0}",pesos[2]);
Console.WriteLine("numero de iteraciones para el conocimiento:" + iteraciones);
for (int cont = 0; cont <= 3; cont++)
{ //Muestra el perceptron con la tabla AND aprendida
double salidaReal = datos[cont, 0] * pesos[0] + datos[cont, 1] * pesos[1] + pesos[2];
salidaEntera = (salidaReal > 0) ? 1 : 0;
Console.WriteLine("Entradas: " + datos[cont, 0].ToString() + " y " + datos[cont, 1].ToString() + " = " +
datos[cont, 2].ToString() + " perceptron: " + salidaEntera.ToString());
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment