PerceptronTrainer extends LinearTrainer { @Override protected void update(double label, Vector dataPoint, LinearModel model) { double factor = 1.0; if (label == 0.0) { factor = -1.0; } Vector updateVector = dataPoint.times(factor).times(this.learningRate); log.debug("Updatevec: {}", updateVector); model.addDelta(updateVector); model.shiftBias(factor * this.learningRate); log.debug("{}", model); } PerceptronTrainer(int dimension, double threshold,
                           double learningRate, double init, double initBias);   }
@Test public void testUpdate() throws Exception { double[] labels = { 1.0, 1.0, 1.0, 0.0 }; Vector labelset = new DenseVector(labels); double[][] values = new double[3][4]; for (int i = 0; i < 3; i++) { values[i][0] = 1.0; values[i][1] = 1.0; values[i][2] = 1.0; values[i][3] = 1.0; } values[1][0] = 0.0; values[2][0] = 0.0; values[1][1] = 0.0; values[2][2] = 0.0; Matrix dataset = new DenseMatrix(values); this.trainer.train(labelset, dataset); assertFalse(this.trainer.getModel().classify(dataset.getColumn(3))); assertTrue(this.trainer.getModel().classify(dataset.getColumn(0))); }
@Test public void testUpdate() throws Exception { double[] labels = { 1.0, 1.0, 1.0, 0.0 }; Vector labelset = new DenseVector(labels); double[][] values = new double[3][4]; for (int i = 0; i < 3; i++) { values[i][0] = 1.0; values[i][1] = 1.0; values[i][2] = 1.0; values[i][3] = 1.0; } values[1][0] = 0.0; values[2][0] = 0.0; values[1][1] = 0.0; values[2][2] = 0.0; Matrix dataset = new DenseMatrix(values); this.trainer.train(labelset, dataset); assertFalse(this.trainer.getModel().classify(dataset.viewColumn(3))); assertTrue(this.trainer.getModel().classify(dataset.viewColumn(0))); }