Package com.github.neuralnetworks.util

Examples of com.github.neuralnetworks.util.Matrix


  if (activation.getElements().length != target.getElements().length || activation.getColumns() != target.getColumns()) {
      throw new IllegalArgumentException("Matrices are not the same");
  }

  if (result == null || result.getElements().length != activation.getElements().length) {
      result = new Matrix(activation);
  }

  for (int i = 0; i < activation.getElements().length; i++) {
      result.getElements()[i] = (target.getElements()[i] - activation.getElements()[i]) * activation.getElements()[i] * (1 - activation.getElements()[i]);
  }
View Full Code Here


    public void propagateBackward(Matrix target) {
  NeuralNetwork nn = getNeuralNetwork();

  OutputErrorDerivative d = getProperties().getParameter(Constants.OUTPUT_ERROR_DERIVATIVE);
  Matrix outputErrorDerivative = d.getOutputErrorDerivative(activations.getValues(nn.getOutputLayer()), target);
  backpropagation.addValues(nn.getOutputLayer(), outputErrorDerivative);
  Set<Layer> calculatedLayers = new UniqueList<Layer>();
  calculatedLayers.add(nn.getOutputLayer());
  BackPropagationLayerCalculator blc = getBPLayerCalculator();
  blc.backpropagate(nn, calculatedLayers, activations, backpropagation);
View Full Code Here

  c.getWeights()[4] = 1;
  c.getWeights()[5] = 2;
  c.getWeights()[6] = 3;
  c.getWeights()[7] = 4;

  Matrix i1 = new Matrix(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }, 1);

  Matrix o = new Matrix(4, 1);

  AparapiConv2D conv = new AparapiConv2DFF(c, 1);

  conv.calculate(c, i1, o);

  // most simple case
  assertEquals(164, o.get(0, 0), 0);
  assertEquals(184, o.get(0, 1), 0);
  assertEquals(224, o.get(0, 2), 0);
  assertEquals(244, o.get(0, 3), 0);
  Util.fillArray(o.getElements(), 0);
    }
View Full Code Here

  c.getWeights()[12] = 1;
  c.getWeights()[13] = 2;
  c.getWeights()[14] = 3;
  c.getWeights()[15] = 4;

  Matrix i1 = new Matrix(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }, 1);

  Matrix o = new Matrix(8, 1);

  AparapiConv2D conv = new AparapiConv2DFF(c, 1);

  conv.calculate(c, i1, o);

  assertEquals(164, o.get(0, 0), 0);
  assertEquals(184, o.get(0, 1), 0);
  assertEquals(224, o.get(0, 2), 0);
  assertEquals(244, o.get(0, 3), 0);
  assertEquals(164, o.get(0, 4), 0);
  assertEquals(184, o.get(0, 5), 0);
  assertEquals(224, o.get(0, 6), 0);
  assertEquals(244, o.get(0, 7), 0);
  Util.fillArray(o.getElements(), 0);
    }
View Full Code Here

  c.getWeights()[12] = 1;
  c.getWeights()[13] = 2;
  c.getWeights()[14] = 3;
  c.getWeights()[15] = 4;

  Matrix i1 = new Matrix(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }, 1);

  ValuesProvider vp = new ValuesProvider();
  vp.addValues(nn.getInputLayer(), i1);

  Set<Layer> calculatedLayers = new HashSet<>();
  calculatedLayers.add(nn.getInputLayer());
  nn.getLayerCalculator().calculate(nn, nn.getOutputLayer(), calculatedLayers, vp);

  Matrix o = vp.getValues(nn.getOutputLayer());

  assertEquals(244, o.get(0, 0), 0);
  assertEquals(244, o.get(1, 0), 0);
    }
View Full Code Here

    }

    @Test
    public void testMaxPooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);
  Matrix i1 = new Matrix(new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 }, 2);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  ConnectionCalculator calc = new AparapiMaxPooling2D();
  Matrix o = new Matrix(8, 2);

  ValuesProvider vp = new ValuesProvider();
  vp.addValues(c.getInputLayer(), i1);
  vp.addValues(c.getOutputLayer(), o);

  calc.calculate(connections, vp, c.getOutputLayer());

  assertEquals(3, o.get(0, 0), 0);
  assertEquals(4, o.get(1, 0), 0);
  assertEquals(7, o.get(2, 0), 0);
  assertEquals(8, o.get(3, 0), 0);
  assertEquals(11, o.get(4, 0), 0);
  assertEquals(12, o.get(5, 0), 0);
  assertEquals(15, o.get(6, 0), 0);
  assertEquals(16, o.get(7, 0), 0);

  assertEquals(6, o.get(0, 1), 0);
  assertEquals(8, o.get(1, 1), 0);
  assertEquals(14, o.get(2, 1), 0);
  assertEquals(16, o.get(3, 1), 0);
  assertEquals(22, o.get(4, 1), 0);
  assertEquals(24, o.get(5, 1), 0);
  assertEquals(30, o.get(6, 1), 0);
  assertEquals(32, o.get(7, 1), 0);
    }
View Full Code Here

    }

    @Test
    public void testAveragePooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);
  Matrix i1 = new Matrix(new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 }, 2);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  AparapiAveragePooling2D calc = new AparapiAveragePooling2D();
  Matrix o = new Matrix(8, 2);

  ValuesProvider vp = new ValuesProvider();
  vp.addValues(c.getInputLayer(), i1);
  vp.addValues(c.getOutputLayer(), o);

  calc.calculate(connections, vp, c.getOutputLayer());

  assertEquals(1.75, o.get(0, 0), 0);
  assertEquals(2.75, o.get(1, 0), 0);
  assertEquals(5.75, o.get(2, 0), 0);
  assertEquals(6.75, o.get(3, 0), 0);
  assertEquals(9.75, o.get(4, 0), 0);
  assertEquals(10.75, o.get(5, 0), 0);
  assertEquals(13.75, o.get(6, 0), 0);
  assertEquals(14.75, o.get(7, 0), 0);

  assertEquals(3.5, o.get(0, 1), 0);
  assertEquals(5.5, o.get(1, 1), 0);
  assertEquals(11.5, o.get(2, 1), 0);
  assertEquals(13.5, o.get(3, 1), 0);
  assertEquals(19.5, o.get(4, 1), 0);
  assertEquals(21.5, o.get(5, 1), 0);
  assertEquals(27.5, o.get(6, 1), 0);
  assertEquals(29.5, o.get(7, 1), 0);
    }
View Full Code Here

    }

    @Test
    public void testStochasticPooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 3, 3, 3, 3, 1);
  Matrix i1 = new Matrix(new float[] { 1.6f, 1.6f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.4f, 2.4f }, 2);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  Matrix o = new Matrix(1, 2);

  ValuesProvider vp = new ValuesProvider();
  vp.addValues(c.getInputLayer(), i1);
  vp.addValues(c.getOutputLayer(), o);

  AparapiStochasticPooling2D calc = new AparapiStochasticPooling2D();
  calc.calculate(connections, vp, c.getOutputLayer());

  assertEquals(2.08, o.get(0, 0), 0.01);
  assertEquals(2.08, o.get(0, 1), 0.01);
    }
View Full Code Here

    }

    @Test
    public void testMaxPoolingBackpropagation() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);
  Matrix a1 = new Matrix(new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 }, 2);
  Matrix o = new Matrix(8, 2);

  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  // max pooling
  ValuesProvider vp = new ValuesProvider();
  vp.addValues(c.getInputLayer(), a1);
  vp.addValues(c.getOutputLayer(), o);

  ConnectionCalculator calc = new AparapiMaxPooling2D();
  calc.calculate(connections, vp, c.getOutputLayer());

  ValuesProvider activations = new ValuesProvider();
  activations.addValues(c.getInputLayer(), a1);

  Matrix bpo = new Matrix(32, 2);

  vp = new ValuesProvider();
  vp.addValues(c.getOutputLayer(), o);
  vp.addValues(c.getInputLayer(), bpo);

  BackpropagationMaxPooling2D bp = new BackpropagationMaxPooling2D();
  bp.setActivations(activations);
  bp.calculate(connections, vp, c.getInputLayer());

  assertEquals(true, bpo.get(5, 0) == a1.get(5, 0));
  assertEquals(true, bpo.get(7, 0) == a1.get(7, 0));
  assertEquals(true, bpo.get(13, 0) == a1.get(13, 0));
  assertEquals(true, bpo.get(14, 0) == a1.get(14, 0));
  assertEquals(true, bpo.get(5, 1) == a1.get(5, 1));
  assertEquals(true, bpo.get(7, 1) == a1.get(7, 1));
  assertEquals(true, bpo.get(13, 1) == a1.get(13, 1));
  assertEquals(true, bpo.get(14, 1) == a1.get(14, 1));
    }
View Full Code Here

    @Test
    public void testAveragePoolingBackpropagation() {
  Environment.getInstance().setExecutionMode(EXECUTION_MODE.SEQ);
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);
  Matrix a1 = new Matrix(new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 }, 2);

  // max pooling
  ConnectionCalculator calc = new AparapiAveragePooling2D();

  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  ValuesProvider vp = new ValuesProvider();
  vp.addValues(c.getInputLayer(), a1);
  Matrix o = new Matrix(8, 2);
  vp.addValues(c.getOutputLayer(), o);

  calc.calculate(connections, vp, c.getOutputLayer());

  ValuesProvider activations = new ValuesProvider();
  activations.addValues(c.getInputLayer(), a1);

  BackpropagationAveragePooling2D bp = new BackpropagationAveragePooling2D();
  bp.setActivations(activations);

  vp = new ValuesProvider();
  vp.addValues(c.getOutputLayer(), o);
  Matrix bpo = new Matrix(32, 2);
  vp.addValues(c.getInputLayer(), bpo);

  bp.calculate(connections, vp, c.getInputLayer());

  assertEquals(true, bpo.get(0, 0) == o.get(0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 0) == o.get(0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(4, 0) == o.get(0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(5, 0) == o.get(0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(10, 0) == o.get(3, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(11, 0) == o.get(3, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(14, 0) == o.get(3, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(15, 0) == o.get(3, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 1) == o.get(0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 1) == o.get(0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(4, 1) == o.get(0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(5, 1) == o.get(0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(10, 1) == o.get(3, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(11, 1) == o.get(3, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(14, 1) == o.get(3, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(15, 1) == o.get(3, 1) / c.getSubsamplingRegionLength());
    }
View Full Code Here

TOP

Related Classes of com.github.neuralnetworks.util.Matrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.