Package com.github.neuralnetworks.architecture.types

Examples of com.github.neuralnetworks.architecture.types.DBN


     * DBN testing
     */
    @Test
    public void testDBN() {
  // deep belief network with two rbms - 4-16 and 16-3 with biases
  DBN dbn = NNFactory.dbn(new int[] {4, 4, 3}, true);
  assertEquals(2, dbn.getNeuralNetworks().size(), 0);

  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  TrainingInputProvider trainInputProvider = new IrisInputProvider(150, 150000, new IrisTargetMultiNeuronOutputConverter(), false, true, false);
  TrainingInputProvider testInputProvider = new IrisInputProvider(1, 150, new IrisTargetMultiNeuronOutputConverter(), false, true, false);
  // rbm trainers for each layer
  AparapiCDTrainer firstTrainer = TrainerFactory.cdSigmoidTrainer(dbn.getFirstNeuralNetwork(), null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f)), 0.01f, 0.5f, 0f, 0f, 1, true);
  AparapiCDTrainer lastTrainer = TrainerFactory.cdSigmoidTrainer(dbn.getLastNeuralNetwork(), null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f)), 0.01f, 0.5f, 0f, 0f, 1, true);

  Map<NeuralNetwork, OneStepTrainer<?>> map = new HashMap<>();
  map.put(dbn.getFirstNeuralNetwork(), firstTrainer);
  map.put(dbn.getLastNeuralNetwork(), lastTrainer);

  // deep trainer
  DBNTrainer deepTrainer = TrainerFactory.dbnTrainer(dbn, map, trainInputProvider, null, null);

  Environment.getInstance().setExecutionMode(EXECUTION_MODE.SEQ);
View Full Code Here


      // execution mode
  Environment.getInstance().setExecutionMode(EXECUTION_MODE.CPU);

  // deep belief network with two rbms - 4-4 and 4-3 with biases
  Environment.getInstance().setUseWeightsSharedMemory(true);
  DBN dbn = NNFactory.dbn(new int[] {4, 4, 3}, true);
  assertEquals(2, dbn.getNeuralNetworks().size(), 0);

  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  IrisInputProvider trainInputProvider = new IrisInputProvider(new IrisTargetMultiNeuronOutputConverter(), false);
  trainInputProvider.addInputModifier(new ScalingInputFunction(trainInputProvider));

  IrisInputProvider testInputProvider = new IrisInputProvider(new IrisTargetMultiNeuronOutputConverter(), false);
  testInputProvider.addInputModifier(new ScalingInputFunction(testInputProvider));

  // rbm trainers for each layer
  AparapiCDTrainer firstTrainer = TrainerFactory.cdSigmoidBinaryTrainer(dbn.getFirstNeuralNetwork(), null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f)), 0.01f, 0.5f, 0f, 0f, 1, 150, 1000, true);
  AparapiCDTrainer lastTrainer = TrainerFactory.cdSigmoidBinaryTrainer(dbn.getLastNeuralNetwork(), null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f)), 0.01f, 0.5f, 0f, 0f, 1, 150, 1000, true);

  Map<NeuralNetwork, OneStepTrainer<?>> map = new HashMap<>();
  map.put(dbn.getFirstNeuralNetwork(), firstTrainer);
  map.put(dbn.getLastNeuralNetwork(), lastTrainer);

  // deep trainer
  DBNTrainer deepTrainer = TrainerFactory.dbnTrainer(dbn, map, trainInputProvider, null, null);

  Environment.getInstance().setExecutionMode(EXECUTION_MODE.SEQ);
View Full Code Here

public class DNNTest {

    @Test
    public void testDBNConstruction() {
  DBN dbn = NNFactory.dbn(new int[] { 4, 4, 4, 4}, false);
  assertEquals(4, dbn.getLayers().size(), 0);
  assertEquals(3, dbn.getNeuralNetworks().size(), 0);
  assertEquals(2, dbn.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(2, dbn.getNeuralNetwork(1).getLayers().size(), 0);
  assertEquals(2, dbn.getLastNeuralNetwork().getLayers().size(), 0);

  dbn = NNFactory.dbn(new int[] { 4, 4, 4, 4 }, true);
  assertEquals(7, dbn.getLayers().size(), 0);
  assertEquals(3, dbn.getNeuralNetworks().size(), 0);
  assertEquals(4, dbn.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(4, dbn.getNeuralNetwork(0).getLayers().size(), 0);
  assertEquals(4, dbn.getLastNeuralNetwork().getLayers().size(), 0);

  assertEquals(true, dbn.getFirstNeuralNetwork().getHiddenBiasConnections() != null);
  assertEquals(true, dbn.getFirstNeuralNetwork().getVisibleBiasConnections() != null);
  assertEquals(true, dbn.getNeuralNetwork(1).getHiddenBiasConnections() != null);
  assertEquals(true, dbn.getNeuralNetwork(1).getVisibleBiasConnections() != null);
  assertEquals(true, dbn.getLastNeuralNetwork().getHiddenBiasConnections() != null);
  assertEquals(true, dbn.getLastNeuralNetwork().getVisibleBiasConnections() != null);

  assertEquals(false, dbn.getLayers().contains(dbn.getFirstNeuralNetwork().getVisibleBiasConnections().getInputLayer()));
  assertEquals(false, dbn.getLayers().contains(dbn.getNeuralNetwork(1).getVisibleBiasConnections().getInputLayer()));
  assertEquals(false, dbn.getLayers().contains(dbn.getLastNeuralNetwork().getVisibleBiasConnections().getInputLayer()));

  assertEquals(true, dbn.getFirstNeuralNetwork().getHiddenLayer() == dbn.getNeuralNetwork(1).getVisibleLayer());
  assertEquals(true, dbn.getNeuralNetwork(1).getHiddenLayer() == dbn.getLastNeuralNetwork().getVisibleLayer());

  assertEquals(true, dbn.getOutputLayer().equals(dbn.getLastNeuralNetwork().getHiddenLayer()));
    }
View Full Code Here

  assertEquals(true, sae.getFirstNeuralNetwork().getHiddenLayer() == sae.getLastNeuralNetwork().getInputLayer());
    }

    @Test
    public void testDBNCalculation() {
  DBN dbn = NNFactory.dbn(new int [] {3, 2, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcWeightedSum(dbn, null));

  RBM firstRBM = dbn.getFirstNeuralNetwork();
  Util.fillArray(firstRBM.getMainConnections().getConnectionGraph().getElements(), 0.2f);
  Util.fillArray(firstRBM.getVisibleBiasConnections().getConnectionGraph().getElements(), 0.1f);
  Util.fillArray(firstRBM.getHiddenBiasConnections().getConnectionGraph().getElements(), 0.3f);

  RBM secondRBM = dbn.getLastNeuralNetwork();
  Util.fillArray(secondRBM.getMainConnections().getConnectionGraph().getElements(), 0.4f);
  Util.fillArray(secondRBM.getVisibleBiasConnections().getConnectionGraph().getElements(), 0.8f);
  Util.fillArray(secondRBM.getHiddenBiasConnections().getConnectionGraph().getElements(), 0.5f);

  Set<Layer> calculatedLayers = new HashSet<>();
  calculatedLayers.add(dbn.getInputLayer());

  ValuesProvider results = new ValuesProvider();

  results.addValues(dbn.getInputLayer(), new Matrix(new float[] {1, 0, 1}, 1));
  dbn.getLayerCalculator().calculate(dbn, dbn.getOutputLayer(), calculatedLayers, results);

  assertEquals(1.06, results.getValues(dbn.getOutputLayer()).get(0, 0), 0.00001);
  assertEquals(1.06, results.getValues(dbn.getOutputLayer()).get(1, 0), 0.00001);
    }
View Full Code Here

  assertEquals(1.06, results.getValues(sae.getOutputLayer()).get(1, 0), 0.00001);
    }

    @Test
    public void testDNNLayerTrainer() {
  DBN dbn = NNFactory.dbn(new int [] {3, 2, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  RBM firstRBM = dbn.getFirstNeuralNetwork();

  Matrix cg1 = firstRBM.getMainConnections().getConnectionGraph();
  cg1.set(0.2f, 0, 0);
  cg1.set(0.4f, 0, 1);
  cg1.set(-0.5f, 0, 2);
  cg1.set(-0.3f, 1, 0);
  cg1.set(0.1f, 1, 1);
  cg1.set(0.2f, 1, 2);

  Matrix cgb1 = firstRBM.getVisibleBiasConnections().getConnectionGraph();
  cgb1.set(0f, 0, 0);
  cgb1.set(0f, 1, 0);
  cgb1.set(0f, 2, 0);

  Matrix cgb2 = firstRBM.getHiddenBiasConnections().getConnectionGraph();
  cgb2.set(-0.4f, 0, 0);
  cgb2.set(0.2f, 1, 0);

  SimpleInputProvider inputProvider = new SimpleInputProvider(new float[][] { { 1, 0, 1 } }, null, 1, 1);

  AparapiCDTrainer firstTrainer = TrainerFactory.cdSigmoidTrainer(firstRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, true);
  firstTrainer.setLayerCalculator(NNFactory.rbmSigmoidSigmoid(firstRBM));

  RBM secondRBM = dbn.getLastNeuralNetwork();

  AparapiCDTrainer secondTrainer = TrainerFactory.cdSigmoidTrainer(secondRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, true);
  secondTrainer.setLayerCalculator(NNFactory.rbmSigmoidSigmoid(secondRBM));

  Map<NeuralNetwork, OneStepTrainer<?>> layerTrainers = new HashMap<>();
View Full Code Here

  assertEquals(0.2 - 0.02693379, cgb2.get(1, 0), 0.00001);
    }

    @Test
    public void testDNNLayerTrainer2() {
  DBN dbn = NNFactory.dbn(new int [] {3, 3, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  LayerCalculatorImpl lc = (LayerCalculatorImpl) dbn.getLayerCalculator();
  RBM firstRBM = dbn.getFirstNeuralNetwork();
  lc.addConnectionCalculator(firstRBM.getHiddenLayer(), new AparapiWeightedSumConnectionCalculator());

  Matrix m1 = firstRBM.getMainConnections().getConnectionGraph();
  m1.set(1, 0, 0);
  m1.set(0, 0, 1);
  m1.set(0, 0, 2);
  m1.set(0, 1, 0);
  m1.set(1, 1, 1);
  m1.set(0, 1, 2);
  m1.set(0, 2, 0);
  m1.set(0, 2, 1);
  m1.set(1, 2, 2);

  RBM secondRBM = dbn.getLastNeuralNetwork();
 
  Matrix cg1 = secondRBM.getMainConnections().getConnectionGraph();
  cg1.set(0.2f, 0, 0);
  cg1.set(0.4f, 0, 1);
  cg1.set(-0.5f, 0, 2);
View Full Code Here

    @Test
    public void testDBNConstruction() {
  Environment.getInstance().setUseWeightsSharedMemory(true);

  DBN dbn = NNFactory.dbn(new int[] { 4, 4, 4, 4}, false);
  assertEquals(4, dbn.getLayers().size(), 0);
  assertEquals(3, dbn.getNeuralNetworks().size(), 0);
  assertEquals(2, dbn.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(2, dbn.getNeuralNetwork(1).getLayers().size(), 0);
  assertEquals(2, dbn.getLastNeuralNetwork().getLayers().size(), 0);

  dbn = NNFactory.dbn(new int[] { 4, 4, 4, 4 }, true);
  assertEquals(7, dbn.getLayers().size(), 0);
  assertEquals(3, dbn.getNeuralNetworks().size(), 0);
  assertEquals(4, dbn.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(4, dbn.getNeuralNetwork(0).getLayers().size(), 0);
  assertEquals(4, dbn.getLastNeuralNetwork().getLayers().size(), 0);

  assertEquals(true, dbn.getFirstNeuralNetwork().getHiddenBiasConnections() != null);
  assertEquals(true, dbn.getFirstNeuralNetwork().getVisibleBiasConnections() != null);
  assertEquals(true, dbn.getNeuralNetwork(1).getHiddenBiasConnections() != null);
  assertEquals(true, dbn.getNeuralNetwork(1).getVisibleBiasConnections() != null);
  assertEquals(true, dbn.getLastNeuralNetwork().getHiddenBiasConnections() != null);
  assertEquals(true, dbn.getLastNeuralNetwork().getVisibleBiasConnections() != null);

  assertEquals(false, dbn.getLayers().contains(dbn.getFirstNeuralNetwork().getVisibleBiasConnections().getInputLayer()));
  assertEquals(false, dbn.getLayers().contains(dbn.getNeuralNetwork(1).getVisibleBiasConnections().getInputLayer()));
  assertEquals(false, dbn.getLayers().contains(dbn.getLastNeuralNetwork().getVisibleBiasConnections().getInputLayer()));

  assertEquals(true, dbn.getFirstNeuralNetwork().getHiddenLayer() == dbn.getNeuralNetwork(1).getVisibleLayer());
  assertEquals(true, dbn.getNeuralNetwork(1).getHiddenLayer() == dbn.getLastNeuralNetwork().getVisibleLayer());

  assertEquals(true, dbn.getOutputLayer().equals(dbn.getLastNeuralNetwork().getHiddenLayer()));
    }
View Full Code Here

    }

    @Test
    public void testDBNCalculation() {
  Environment.getInstance().setUseWeightsSharedMemory(true);
  DBN dbn = NNFactory.dbn(new int [] {3, 2, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcWeightedSum(dbn, null));

  RBM firstRBM = dbn.getFirstNeuralNetwork();

  Tensor t = firstRBM.getMainConnections().getWeights();
  float[] e1 = t.getElements();
  t.forEach(i -> e1[i] = 0.2f);
 
  t = firstRBM.getVisibleBiasConnections().getWeights();
  float[] e2 = t.getElements();
  t.forEach(i -> e2[i] = 0.1f);

  t = firstRBM.getHiddenBiasConnections().getWeights();
  float[] e3 = t.getElements();
  t.forEach(i -> e3[i] = 0.3f);

  RBM secondRBM = dbn.getLastNeuralNetwork();

  t = secondRBM.getMainConnections().getWeights();
  float[] e4 = t.getElements();
  t.forEach(i -> e4[i] = 0.4f);

  t = secondRBM.getVisibleBiasConnections().getWeights();
  float[] e5 = t.getElements();
  t.forEach(i -> e5[i] = 0.8f);

  t = secondRBM.getHiddenBiasConnections().getWeights();
  float[] e6 = t.getElements();
  t.forEach(i -> e6[i] = 0.5f);

  Set<Layer> calculatedLayers = new HashSet<>();
  calculatedLayers.add(dbn.getInputLayer());

  ValuesProvider results = TensorFactory.tensorProvider(dbn, 1, true);
  results.get(dbn.getInputLayer()).set(1, 0, 0);
  results.get(dbn.getInputLayer()).set(0, 1, 0);
  results.get(dbn.getInputLayer()).set(1, 2, 0);
  dbn.getLayerCalculator().calculate(dbn, dbn.getOutputLayer(), calculatedLayers, results);

  assertEquals(1.06, results.get(dbn.getOutputLayer()).get(0, 0), 0.00001);
  assertEquals(1.06, results.get(dbn.getOutputLayer()).get(1, 0), 0.00001);
    }
View Full Code Here

    }

    @Test
    public void testDNNLayerTrainer() {
  Environment.getInstance().setUseWeightsSharedMemory(true);
  DBN dbn = NNFactory.dbn(new int [] {3, 2, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  RBM firstRBM = dbn.getFirstNeuralNetwork();

  Matrix cg1 = firstRBM.getMainConnections().getWeights();
  cg1.set(0.2f, 0, 0);
  cg1.set(0.4f, 0, 1);
  cg1.set(-0.5f, 0, 2);
  cg1.set(-0.3f, 1, 0);
  cg1.set(0.1f, 1, 1);
  cg1.set(0.2f, 1, 2);

  Matrix cgb1 = firstRBM.getVisibleBiasConnections().getWeights();
  cgb1.set(0f, 0, 0);
  cgb1.set(0f, 1, 0);
  cgb1.set(0f, 2, 0);

  Matrix cgb2 = firstRBM.getHiddenBiasConnections().getWeights();
  cgb2.set(-0.4f, 0, 0);
  cgb2.set(0.2f, 1, 0);

  SimpleInputProvider inputProvider = new SimpleInputProvider(new float[][] { { 1, 0, 1 } }, null);

  AparapiCDTrainer firstTrainer = TrainerFactory.cdSigmoidTrainer(firstRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, 1, 1, true);

  RBM secondRBM = dbn.getLastNeuralNetwork();

  AparapiCDTrainer secondTrainer = TrainerFactory.cdSigmoidTrainer(secondRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, 1, 1, true);

  Map<NeuralNetwork, OneStepTrainer<?>> layerTrainers = new HashMap<>();
  layerTrainers.put(firstRBM, firstTrainer);
View Full Code Here

    }

    @Test
    public void testDNNLayerTrainer2() {
  Environment.getInstance().setUseWeightsSharedMemory(true);
  DBN dbn = NNFactory.dbn(new int [] {3, 3, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  RBM firstRBM = dbn.getFirstNeuralNetwork();

  LayerCalculatorImpl lc = (LayerCalculatorImpl) dbn.getLayerCalculator();
  lc.addConnectionCalculator(firstRBM.getHiddenLayer(), new AparapiWeightedSumConnectionCalculator());

  Matrix m1 = firstRBM.getMainConnections().getWeights();
  m1.set(1, 0, 0);
  m1.set(0, 0, 1);
  m1.set(0, 0, 2);
  m1.set(0, 1, 0);
  m1.set(1, 1, 1);
  m1.set(0, 1, 2);
  m1.set(0, 2, 0);
  m1.set(0, 2, 1);
  m1.set(1, 2, 2);

  RBM secondRBM = dbn.getLastNeuralNetwork();
 
  Matrix cg1 = secondRBM.getMainConnections().getWeights();
  cg1.set(0.2f, 0, 0);
  cg1.set(0.4f, 0, 1);
  cg1.set(-0.5f, 0, 2);
View Full Code Here

TOP

Related Classes of com.github.neuralnetworks.architecture.types.DBN

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.