Adjusted to be more verbose while error

This commit is contained in:
Niklas Birk 2019-06-25 12:01:37 +02:00
parent 248593af6c
commit e09b6fb6e4
2 changed files with 10 additions and 4 deletions

View File

@ -7,6 +7,7 @@ public class Perceptron
{ {
public void learn(List<Vector> positives, List<Vector> negatives) public void learn(List<Vector> positives, List<Vector> negatives)
{ {
var iterationCounter = 0;
var weight = this.getInitializationVector(positives, negatives); var weight = this.getInitializationVector(positives, negatives);
do do
@ -16,6 +17,7 @@ public class Perceptron
if (weight.scalar(x) <= 0) if (weight.scalar(x) <= 0)
{ {
weight = weight.add(x); weight = weight.add(x);
System.out.println(weight);
} }
} }
@ -24,16 +26,17 @@ public class Perceptron
if (weight.scalar(x) > 0) if (weight.scalar(x) > 0)
{ {
weight = weight.subtract(x); weight = weight.subtract(x);
System.out.println(weight);
} }
} }
System.out.println(weight); iterationCounter++;
} }
while (!elementsAreCorrectClassified(positives, negatives, weight)); while (!elementsAreCorrectClassified(positives, negatives, weight));
System.out.println("----------------------------------------------"); System.out.println("-----------------------------------------------------------------");
System.out.println("-- All datapoints are classified correctly. --"); System.out.println("-- All datapoints are classified correctly in " + iterationCounter + " iterations. --");
System.out.println("----------------------------------------------"); System.out.println("-----------------------------------------------------------------");
} }
private Vector getInitializationVector(List<Vector> positives, List<Vector> negatives) private Vector getInitializationVector(List<Vector> positives, List<Vector> negatives)

View File

@ -30,6 +30,7 @@ public class Vector
public Vector add(Vector b) public Vector add(Vector b)
{ {
if (this.dimension() != b.dimension()) throw new IllegalArgumentException("Dimensions must be equals.");
return new Vector(IntStream.range(0, return new Vector(IntStream.range(0,
this.dimension()) this.dimension())
.mapToObj(i -> this.get(i) + b.get(i)) .mapToObj(i -> this.get(i) + b.get(i))
@ -40,6 +41,7 @@ public class Vector
public Vector subtract(Vector b) public Vector subtract(Vector b)
{ {
if (this.dimension() != b.dimension()) throw new IllegalArgumentException("Dimensions must be equals.");
return new Vector(IntStream.range(0, return new Vector(IntStream.range(0,
this.dimension()) this.dimension())
.mapToObj(i -> this.get(i) - b.get(i)) .mapToObj(i -> this.get(i) - b.get(i))
@ -49,6 +51,7 @@ public class Vector
public double scalar(Vector b) public double scalar(Vector b)
{ {
if (this.dimension() != b.dimension()) throw new IllegalArgumentException("Dimensions must be equals.");
return IntStream.range(0, return IntStream.range(0,
this.dimension()) this.dimension())
.mapToDouble(i -> this.get(i) * b.get(i)) .mapToDouble(i -> this.get(i) * b.get(i))