From e09b6fb6e4ebc326361fbcce5c734b828deec636 Mon Sep 17 00:00:00 2001 From: Niklas Birk Date: Tue, 25 Jun 2019 12:01:37 +0200 Subject: [PATCH] Adjusted to be more verbose while error --- src/machine_learning/perceptron/Perceptron.java | 11 +++++++---- src/machine_learning/perceptron/Vector.java | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/machine_learning/perceptron/Perceptron.java b/src/machine_learning/perceptron/Perceptron.java index ec27c11..cb5cb23 100644 --- a/src/machine_learning/perceptron/Perceptron.java +++ b/src/machine_learning/perceptron/Perceptron.java @@ -7,6 +7,7 @@ public class Perceptron { public void learn(List positives, List negatives) { + var iterationCounter = 0; var weight = this.getInitializationVector(positives, negatives); do @@ -16,6 +17,7 @@ public class Perceptron if (weight.scalar(x) <= 0) { weight = weight.add(x); + System.out.println(weight); } } @@ -24,16 +26,17 @@ public class Perceptron if (weight.scalar(x) > 0) { weight = weight.subtract(x); + System.out.println(weight); } } - System.out.println(weight); + iterationCounter++; } while (!elementsAreCorrectClassified(positives, negatives, weight)); - System.out.println("----------------------------------------------"); - System.out.println("-- All datapoints are classified correctly. --"); - System.out.println("----------------------------------------------"); + System.out.println("-----------------------------------------------------------------"); + System.out.println("-- All datapoints are classified correctly in " + iterationCounter + " iterations. --"); + System.out.println("-----------------------------------------------------------------"); } private Vector getInitializationVector(List positives, List negatives) diff --git a/src/machine_learning/perceptron/Vector.java b/src/machine_learning/perceptron/Vector.java index dbbcabb..d032fc9 100644 --- a/src/machine_learning/perceptron/Vector.java +++ b/src/machine_learning/perceptron/Vector.java @@ -30,6 +30,7 @@ public class Vector public Vector add(Vector b) { + if (this.dimension() != b.dimension()) throw new IllegalArgumentException("Dimensions must be equals."); return new Vector(IntStream.range(0, this.dimension()) .mapToObj(i -> this.get(i) + b.get(i)) @@ -40,6 +41,7 @@ public class Vector public Vector subtract(Vector b) { + if (this.dimension() != b.dimension()) throw new IllegalArgumentException("Dimensions must be equals."); return new Vector(IntStream.range(0, this.dimension()) .mapToObj(i -> this.get(i) - b.get(i)) @@ -49,6 +51,7 @@ public class Vector public double scalar(Vector b) { + if (this.dimension() != b.dimension()) throw new IllegalArgumentException("Dimensions must be equals."); return IntStream.range(0, this.dimension()) .mapToDouble(i -> this.get(i) * b.get(i))