Package com.github.neuralnetworks.calculation.memory

Examples of com.github.neuralnetworks.calculation.memory.ValuesProvider


  Environment.getInstance().setUseWeightsSharedMemory(true);
  ConnectionFactory cf = new ConnectionFactory();
  NNFactory.addFullyConnectedLayer(nn, h, cf, 2, 3, true);
  NNFactory.addFullyConnectedLayer(nn, o, cf, 4, 1, true);

  ValuesProvider tp = TensorFactory.tensorProvider(nn, 2, true);

  Matrix im = tp.get(nn.getInputLayer());
  Matrix hm1 = tp.get(h, 3, 2);
  Matrix hm2 = tp.get(h, 4, 2);

  Tensor om = tp.get(o);

  assertTrue(im == tp.get(i, 2, 2));
  assertTrue(im == tp.get(i));
  assertTrue(hm1 == tp.get(h, 3, 2));
  assertTrue(hm2 == tp.get(h, 4, 2));
  assertTrue(hm1 == TensorFactory.tensor(h, nn.getConnection(i, h), tp));
  assertTrue(hm2 == TensorFactory.tensor(h, nn.getConnection(h, o), tp));
  assertTrue(om == tp.get(o, 1, 2));
  assertTrue(om == tp.get(o));
    }
View Full Code Here


    @Test
    public void testTensorProvider3() {
  // simple mlp test
  Environment.getInstance().setUseWeightsSharedMemory(true);
  NeuralNetworkImpl nn = NNFactory.mlp(new int[] { 3, 4, 2 }, true);
  ValuesProvider tp = TensorFactory.tensorProvider(nn, 2, false);

  Matrix in = tp.get(nn.getInputLayer());
  Matrix hidden = tp.get(nn.getLayers().stream().filter(l -> l != nn.getInputLayer() && l != nn.getOutputLayer() && !Util.isBias(l)).findFirst().get());
  Matrix out = tp.get(nn.getOutputLayer());

  assertEquals(6, in.getElements().length, 0);
  assertEquals(3, in.getRows(), 0);
  assertEquals(2, in.getColumns(), 0);
  assertEquals(8, hidden.getElements().length, 0);
View Full Code Here

  Environment.getInstance().setUseWeightsSharedMemory(true);
  Conv2DConnection c = new ConnectionFactory().conv2d(new Layer(), new Layer(), 3, 3, 2, 2, 2, 1, 1);

  c.getWeights().setElements(new float[] {1, 2, 3, 4, 1, 2, 3, 4});

  ValuesProvider vp = TensorFactory.tensorProvider(c, 1, true);
  TensorIterator it = vp.get(c.getInputLayer()).iterator();
  for (int i = 0; i < vp.get(c.getInputLayer()).getSize(); i++) {
      vp.get(c.getInputLayer()).getElements()[it.next()] = i + 1;
  }

  AparapiConv2D conv = new AparapiConv2DFF(c, vp, c.getOutputLayer());
  conv.calculate(c, vp, c.getOutputLayer());

  // most simple case
  Tensor o = vp.get(c.getOutputLayer());

  assertEquals(164, o.get(0, 0, 0, 0), 0);
  assertEquals(184, o.get(0, 0, 1, 0), 0);
  assertEquals(224, o.get(0, 1, 0, 0), 0);
  assertEquals(244, o.get(0, 1, 1, 0), 0);
View Full Code Here

    public void testConvolutions2() {
  Environment.getInstance().setUseWeightsSharedMemory(true);
  Conv2DConnection c = new ConnectionFactory().conv2d(new Layer(), new Layer(), 3, 3, 2, 2, 2, 2, 1);
  c.getWeights().setElements(new float[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4});

  ValuesProvider vp = TensorFactory.tensorProvider(c, 1, true);
  TensorIterator it = vp.get(c.getInputLayer()).iterator();
  for (int i = 0; i < vp.get(c.getInputLayer()).getSize(); i++) {
      vp.get(c.getInputLayer()).getElements()[it.next()] = i + 1;
  }

  AparapiConv2D conv = new AparapiConv2DFF(c, vp, c.getOutputLayer());
  conv.calculate(c, vp, c.getOutputLayer());

  Tensor o = vp.get(c.getOutputLayer());

  assertEquals(164, o.get(0, 0, 0, 0), 0);
  assertEquals(184, o.get(0, 0, 1, 0), 0);
  assertEquals(224, o.get(0, 1, 0, 0), 0);
  assertEquals(244, o.get(0, 1, 1, 0), 0);
View Full Code Here

  NNFactory.lcMaxPooling(nn);

  Conv2DConnection c = (Conv2DConnection) nn.getInputLayer().getConnections().get(0);
  c.getWeights().setElements(new float[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4});

  ValuesProvider vp = TensorFactory.tensorProvider(nn, 1, true);
  TensorIterator it = vp.get(c.getInputLayer()).iterator();
  for (int i = 0; i < vp.get(c.getInputLayer()).getSize(); i++) {
      vp.get(c.getInputLayer()).getElements()[it.next()] = i + 1;
  }

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

  Tensor o = vp.get(nn.getOutputLayer());

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

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

  ConnectionCalculator calc = new AparapiMaxPooling2D();

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  float[] src = 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 };
  System.arraycopy(src, 0, vp.get(c.getInputLayer()).getElements(), vp.get(c.getInputLayer()).getStartIndex(), src.length);

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

  Tensor o = vp.get(c.getOutputLayer());

  assertEquals(3, o.get(0, 0, 0, 0), 0);
  assertEquals(4, o.get(0, 0, 1, 0), 0);
  assertEquals(7, o.get(0, 1, 0, 0), 0);
  assertEquals(8, o.get(0, 1, 1, 0), 0);
View Full Code Here

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

  AparapiAveragePooling2D calc = new AparapiAveragePooling2D();

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  float[] src = 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 };
  System.arraycopy(src, 0, vp.get(c.getInputLayer()).getElements(), vp.get(c.getInputLayer()).getStartIndex(), src.length);

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

  Tensor o = vp.get(c.getOutputLayer());

  assertEquals(1.75, o.get(0, 0, 0, 0), 0);
  assertEquals(2.75, o.get(0, 0, 1, 0), 0);
  assertEquals(5.75, o.get(0, 1, 0, 0), 0);
  assertEquals(6.75, o.get(0, 1, 1, 0), 0);
View Full Code Here

    public void testStochasticPooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 3, 3, 3, 3, 1);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  float[] src = new float[] { 1.6f, 1.6f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.4f, 2.4f };
  System.arraycopy(src, 0, vp.get(c.getInputLayer()).getElements(), vp.get(c.getInputLayer()).getStartIndex(), src.length);

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

  Tensor t = vp.get(c.getOutputLayer());

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

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

  // max pooling
  ValuesProvider activations = TensorFactory.tensorProvider(c, 2, true);
  float[] src = 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 };
  System.arraycopy(src, 0, activations.get(c.getInputLayer()).getElements(), activations.get(c.getInputLayer()).getStartIndex(), src.length);

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

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  TensorFactory.copy(activations.get(c.getOutputLayer()), vp.get(c.getOutputLayer()));

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

  Tensor a = activations.get(c.getInputLayer());
  Tensor bpo = vp.get(c.getInputLayer());

  assertEquals(true, bpo.get(0, 1, 1, 0) == a.get(0, 1, 1, 0));
  assertEquals(true, bpo.get(0, 1, 3, 0) == a.get(0, 1, 3, 0));
  assertEquals(true, bpo.get(0, 3, 1, 0) == a.get(0, 3, 1, 0));
  assertEquals(true, bpo.get(0, 3, 2, 0) == a.get(0, 3, 2, 0));
View Full Code Here

  ConnectionCalculator calc = new AparapiAveragePooling2D();

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

  ValuesProvider activations = TensorFactory.tensorProvider(c, 2, true);
  float[] src = 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 };
  System.arraycopy(src, 0, activations.get(c.getInputLayer()).getElements(), activations.get(c.getInputLayer()).getStartIndex(), src.length);

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

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

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  TensorFactory.copy(activations.get(c.getOutputLayer()), vp.get(c.getOutputLayer()));

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

  Tensor o = activations.get(c.getOutputLayer());
  Tensor bpo = vp.get(c.getInputLayer());
  assertEquals(true, bpo.get(0, 0, 0, 0) == o.get(0, 0, 0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 0, 2, 0) == o.get(0, 0, 1, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 2, 0, 0) == o.get(0, 1, 0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 2, 2, 0) == o.get(0, 1, 1, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 0, 0, 0) == o.get(1, 0, 0, 0) / c.getSubsamplingRegionLength());
View Full Code Here

TOP

Related Classes of com.github.neuralnetworks.calculation.memory.ValuesProvider

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.