rendered paste bodypublic class Perceptron
{
double[][] data;
double[] target;
double[] weights;
public static void main(String args[])
{
Perceptron start = new Perceptron();
}
public Perceptron()
{
data = new double[][] {
{1,1,0,1,0,0,0},
{1,1,0,1,1,0,0},
{1,1,0,1,0,1,0},
{1,1,1,0,0,1,1},
{1,1,1,1,1,0,0},
{1,1,0,0,0,1,1},
{1,1,0,0,0,1,0},
{1,0,1,1,1,0,1},
{1,0,1,1,0,1,1},
{1,0,0,0,1,1,0},
{1,0,1,0,1,0,1},
{1,0,0,0,1,0,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,0,0}};
target = new double[] { 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
weights = new double[7];
double learningfactor = 0.8;
double e = 0.00001;
double sumoferrors = 0;
int errors = 0;
for (int j = 0; j < weights.length; j++)
{
weights[j] = 0.0;
}
weights[0] = 1;
do {
sumoferrors = 0;
for (int i = 0; i < data.length; i++)
{
double in = 0;
for (int j = 0; j < data[0].length; j++)
{
in += weights[j] * data[i][j];
}
double g = activation(in);
double gprime = activationprime(in);
double err = target[i] - g;
sumoferrors = sumoferrors + (0.5 * Math.pow(err, 2));
for (int k = 0; k < data[0].length; k++)
{
weights[k] += learningfactor * err * gprime * data[i][k];
}
}
} while (sumoferrors > e); //WE WILL FINISH AS SOON AS THE SUM OF ERRORS BECOME SMALL ENOUGH!
printMatrix(weights);
printTest();
}
public double activation(double input) {
return (1.0 / (1.0 + Math.exp(-input)));
}
public double activationprime(double input)
{
return activation(input) * (1 - activation(input));
}
public void printMatrix(double[] mat)
{
System.out.println("The weights are:");
for (int i = 0; i < mat.length; i++)
{
if (i == 0)
{
System.out.printf("Bias: %2.2f\t", mat[i]);
}
else
{
System.out.printf("%2.2f\t", mat[i]);
}
System.out.println();
}
}
public void printTest()
{
for (int i = 0; i < target.length; i++)
{
System.out.println("Solving for example " + (i + 1) + " with weight and inputs");
System.out.println("Target: " + target[i]);
double sum = 0.0;
for (int j = 0; j < weights.length; j++)
{
sum += weights[j] * data[i][j];
}
sum = activation(sum);
System.out.printf("W and xi: %2.2f\n", sum);
}
}
}