Examples of ValuesProvider


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

  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

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

    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

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

  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

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

  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

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

  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

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

    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

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

  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

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

  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

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

  nn.setLayerCalculator(NNFactory.lcWeightedSum(nn, null));

  Conv2DConnection cc = (Conv2DConnection) nn.getInputLayer().getConnections().get(0);
  cc.getWeights().forEach(i -> cc.getWeights().getElements()[i] = 1);

  ValuesProvider vp = TensorFactory.tensorProvider(nn, 1, true);
  float[] src = new float[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
  System.arraycopy(src, 0, vp.get(nn.getInputLayer()).getElements(), vp.get(nn.getInputLayer()).getStartIndex(), src.length);

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

  Tensor o = vp.get(nn.getOutputLayer());
  assertEquals(16, o.get(0, 0, 0, 0), 0.00001);
  assertEquals(24, o.get(0, 0, 1, 0), 0.00001);
  assertEquals(56, o.get(0, 1, 0, 0), 0.00001);
  assertEquals(64, o.get(0, 1, 1, 0), 0.00001);
    }
View Full Code Here

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

  cnn.setLayerCalculator(NNFactory.lcSigmoid(cnn, null));
  NNFactory.lcMaxPooling(cnn);
  FullyConnected cnnfc = (FullyConnected) cnn.getOutputLayer().getConnections().get(0);
  cnnfc.getWeights().set(0.05f, 0, 0);
  cnnfc.getWeights().set(0.08f, 0, 1);
  ValuesProvider cnnvp = TensorFactory.tensorProvider(cnn, 1, Environment.getInstance().getUseDataSharedMemory());
  Tensor cnnin = cnnvp.get(cnn.getInputLayer());
  cnnin.set(0.2f, 0, 0, 0, 0);
  cnnin.set(0.6f, 0, 1, 0, 0);

  // MLP
  NeuralNetworkImpl mlp = NNFactory.mlpSigmoid(new int[] { 2, 1 }, false);
  FullyConnected mlpfc = (FullyConnected) mlp.getOutputLayer().getConnections().get(0);
  mlpfc.getWeights().set(0.05f, 0, 0);
  mlpfc.getWeights().set(0.08f, 0, 1);
  ValuesProvider mlpvp = TensorFactory.tensorProvider(mlp, 1, Environment.getInstance().getUseDataSharedMemory());
  Tensor mlpin = mlpvp.get(mlp.getInputLayer());
  mlpin.set(0.2f, 0, 0);
  mlpin.set(0.6f, 1, 0);

  // compare ff
  Set<Layer> calculated = new HashSet<>();
  calculated.add(cnn.getInputLayer());
  cnn.getLayerCalculator().calculate(cnn, cnn.getOutputLayer(), calculated, cnnvp);

  calculated = new HashSet<>();
  calculated.add(mlp.getInputLayer());
  mlp.getLayerCalculator().calculate(mlp, mlp.getOutputLayer(), calculated, mlpvp);

  assertTrue(Arrays.equals(cnnvp.get(cnn.getOutputLayer()).getElements(), mlpvp.get(mlp.getOutputLayer()).getElements()));
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.