You are on page 1of 2

import java.text.DecimalFormat; public class Perceptron { /** Vector de Pesos */ private double weights[] = {0.5,-0.1,0.

2}; /** * Patrones aumentados de nuestra Muestra de Entrenamiento: * {Caracterstica #1, Caracterstica #2, Bias} * * */ private double entries[][] = {{1.5,3,1}, {3.5,4,1}, {1,2,1}, {4,2,1} }; /** Clases a las que pertenecen los vectores de la variable entries*/ private double outputs[] = {0,1,0,1}; /** Razn de aprendizaje */ private double learningRate = 0.9; public double[] getWeights(){ return weights; } public double[][] getEntries(){ return entries; } public double[] getOutputs(){ return outputs; } /** Funcin para calcular el net */ public double computeNet(double[] entrie){ double net = 0; for(int i = 0; i < entrie.length; i++) net += entrie[i] * weights[i]; if(net > 0) return 1; else return 0; } /** Funcin para corregir pesos */ public void adjustWeights(double[] entrie,double net, double output){

double e = (output - net) * learningRate; for(int i=0; i < entrie.length; i++){ weights[i] = roundTwoDecimals(entrie[i] * e + weights[i]); } } /** Funcin para entrenar */ public void train(){ boolean mistake; do{ mistake = false; for(int i = 0; i < entries.length; i++){ double net = computeNet(entries[i]); if(net != outputs[i]){ mistake = true; adjustWeights(entries[i],net,outputs[i]); } } }while(mistake); } public void outputFinalWeights(){ for(int i = 0; i< weights.length; i++){ System.out.println("Peso " + (i+1) + weights[i]); } } public double roundTwoDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.##"); return Double.valueOf(twoDForm.format(d)); } public Perceptron(){ } public static void main(String args[]){ Perceptron p = new Perceptron(); p.train(); p.outputFinalWeights(); } }

"=" +

You might also like