Package com.github.neuralnetworks.architecture.types

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


    }

    @Test
    public void testSAE() {
  // create stacked autoencoder with input layer of size 4, hidden layer of the first AE with size 4 and hidden layer of the second AE with size 3
  StackedAutoencoder sae = NNFactory.saeSigmoid(new int[] { 4, 4, 3 }, true);

  // stacked networks
  Autoencoder firstNN = sae.getFirstNeuralNetwork();
  firstNN.setLayerCalculator(NNFactory.lcSigmoid(firstNN, null));

  Autoencoder lastNN = sae.getLastNeuralNetwork();
  lastNN.setLayerCalculator(NNFactory.lcSigmoid(lastNN, null));

  // trainers for each of the stacked networks
  BackPropagationAutoencoder firstTrainer = TrainerFactory.backPropagationAutoencoder(firstNN, null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f)), 0.001f, 0.5f, 0f, 0f, 0f);
  BackPropagationAutoencoder secondTrainer = TrainerFactory.backPropagationAutoencoder(lastNN, null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f)), 0.001f, 0.5f, 0f, 0f, 0f);
View Full Code Here


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

  // create stacked autoencoder with input layer of size 4, hidden layer of the first AE with size 4 and hidden layer of the second AE with size 3
  Environment.getInstance().setUseWeightsSharedMemory(true);
  StackedAutoencoder sae = NNFactory.saeSigmoid(new int[] { 4, 4, 3 }, true);

  // data and error providers
  IrisInputProvider trainInputProvider = new IrisInputProvider(new IrisTargetMultiNeuronOutputConverter(), false);
  trainInputProvider.addInputModifier(new ScalingInputFunction(trainInputProvider));

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

  // stacked networks
  Autoencoder firstNN = sae.getFirstNeuralNetwork();
  firstNN.setLayerCalculator(NNFactory.lcSigmoid(firstNN, null));

  Autoencoder lastNN = sae.getLastNeuralNetwork();
  lastNN.setLayerCalculator(NNFactory.lcSigmoid(lastNN, null));

  // trainers for each of the stacked networks
  BackPropagationAutoencoder firstTrainer = TrainerFactory.backPropagationAutoencoder(firstNN, null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f), 0.5f), 0.02f, 0.7f, 0f, 0f, 0f, 150, 1, 2000);
  BackPropagationAutoencoder secondTrainer = TrainerFactory.backPropagationAutoencoder(lastNN, null, null, null, new NNRandomInitializer(new MersenneTwisterRandomInitializer(-0.01f, 0.01f), 0.5f), 0.02f, 0.7f, 0f, 0f, 0f, 150, 1, 2000);
View Full Code Here

  assertEquals(true, dbn.getOutputLayer().equals(dbn.getLastNeuralNetwork().getHiddenLayer()));
    }

    @Test
    public void testStackedAutoencoderConstruction() {
  StackedAutoencoder sae = NNFactory.sae(new int[] { 5, 4, 3 }, false);
  assertEquals(3, sae.getLayers().size(), 0);
  assertEquals(2, sae.getNeuralNetworks().size(), 0);
  assertEquals(3, sae.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(3, sae.getLastNeuralNetwork().getLayers().size(), 0);

  sae = NNFactory.sae(new int[] { 5, 4, 3 }, true);
  assertEquals(5, sae.getLayers().size(), 0);
  assertEquals(2, sae.getNeuralNetworks().size(), 0);
  assertEquals(5, sae.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(5, sae.getLastNeuralNetwork().getLayers().size(), 0);

  assertEquals(false, sae.getLayers().contains(sae.getFirstNeuralNetwork().getOutputLayer()));
  assertEquals(false, sae.getLayers().contains(sae.getLastNeuralNetwork().getOutputLayer()));
  assertEquals(true, sae.getOutputLayer().equals(sae.getLastNeuralNetwork().getHiddenLayer()));

  assertEquals(true, sae.getFirstNeuralNetwork().getHiddenLayer() == sae.getLastNeuralNetwork().getInputLayer());
    }
View Full Code Here

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

    @Test
    public void testSAECalculation() {
  StackedAutoencoder sae = NNFactory.sae(new int [] {3, 2, 2}, true);
  sae.setLayerCalculator(NNFactory.lcWeightedSum(sae, null));

  Autoencoder firstAE = sae.getFirstNeuralNetwork();
  Util.fillArray(((GraphConnections) firstAE.getConnection(firstAE.getInputLayer(), firstAE.getHiddenLayer())).getConnectionGraph().getElements(), 0.2f);
  Util.fillArray(((GraphConnections) firstAE.getConnection(firstAE.getHiddenBiasLayer(), firstAE.getHiddenLayer())).getConnectionGraph().getElements(), 0.3f);
  Util.fillArray(((GraphConnections) firstAE.getConnection(firstAE.getHiddenLayer(), firstAE.getOutputLayer())).getConnectionGraph().getElements(), 0.8f);
  Util.fillArray(((GraphConnections) firstAE.getConnection(firstAE.getOutputBiasLayer(), firstAE.getOutputLayer())).getConnectionGraph().getElements(), 0.9f);

  Autoencoder secondAE = sae.getLastNeuralNetwork();
  Util.fillArray(((GraphConnections) secondAE.getConnection(secondAE.getInputLayer(), secondAE.getHiddenLayer())).getConnectionGraph().getElements(), 0.4f);
  Util.fillArray(((GraphConnections) secondAE.getConnection(secondAE.getHiddenBiasLayer(), secondAE.getHiddenLayer())).getConnectionGraph().getElements(), 0.5f);
  Util.fillArray(((GraphConnections) secondAE.getConnection(secondAE.getHiddenLayer(), secondAE.getOutputLayer())).getConnectionGraph().getElements(), 0.7f);
  Util.fillArray(((GraphConnections) secondAE.getConnection(secondAE.getOutputBiasLayer(), secondAE.getOutputLayer())).getConnectionGraph().getElements(), 0.9f);

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

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

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

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

  StackedAutoencoder sae = NNFactory.sae(new int[] { 5, 4, 3 }, false);
  assertEquals(3, sae.getLayers().size(), 0);
  assertEquals(2, sae.getNeuralNetworks().size(), 0);
  assertEquals(3, sae.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(3, sae.getLastNeuralNetwork().getLayers().size(), 0);

  sae = NNFactory.sae(new int[] { 5, 4, 3 }, true);
  assertEquals(5, sae.getLayers().size(), 0);
  assertEquals(2, sae.getNeuralNetworks().size(), 0);
  assertEquals(5, sae.getFirstNeuralNetwork().getLayers().size(), 0);
  assertEquals(5, sae.getLastNeuralNetwork().getLayers().size(), 0);

  assertEquals(false, sae.getLayers().contains(sae.getFirstNeuralNetwork().getOutputLayer()));
  assertEquals(false, sae.getLayers().contains(sae.getLastNeuralNetwork().getOutputLayer()));
  assertEquals(true, sae.getOutputLayer().equals(sae.getLastNeuralNetwork().getHiddenLayer()));

  assertEquals(true, sae.getFirstNeuralNetwork().getHiddenLayer() == sae.getLastNeuralNetwork().getInputLayer());
    }
View Full Code Here

    }

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

  Autoencoder firstAE = sae.getFirstNeuralNetwork();
  Tensor t = ((FullyConnected) firstAE.getConnection(firstAE.getInputLayer(), firstAE.getHiddenLayer())).getWeights();
  float[] e1 = t.getElements();
  t.forEach(i -> e1[i] = 0.2f);

  t = ((FullyConnected) firstAE.getConnection(firstAE.getHiddenBiasLayer(), firstAE.getHiddenLayer())).getWeights();
  float[] e2 = t.getElements();
  t.forEach(i -> e2[i] = 0.3f);

  t = ((FullyConnected) firstAE.getConnection(firstAE.getHiddenLayer(), firstAE.getOutputLayer())).getWeights();
  float[] e3 = t.getElements();
  t.forEach(i -> e3[i] = 0.8f);

  t = ((FullyConnected) firstAE.getConnection(firstAE.getOutputBiasLayer(), firstAE.getOutputLayer())).getWeights();
  float[] e4 = t.getElements();
  t.forEach(i -> e4[i] = 0.9f);

  Autoencoder secondAE = sae.getLastNeuralNetwork();

  t = ((FullyConnected) secondAE.getConnection(secondAE.getInputLayer(), secondAE.getHiddenLayer())).getWeights();
  float[] e5 = t.getElements();
  t.forEach(i -> e5[i] = 0.4f);
 
  t = ((FullyConnected) secondAE.getConnection(secondAE.getHiddenBiasLayer(), secondAE.getHiddenLayer())).getWeights();
  float[] e6 = t.getElements();
  t.forEach(i -> e6[i] = 0.5f);

  t = ((FullyConnected) secondAE.getConnection(secondAE.getHiddenLayer(), secondAE.getOutputLayer())).getWeights();
  float[] e7 = t.getElements();
  t.forEach(i -> e7[i] = 0.7f);

  t = ((FullyConnected) secondAE.getConnection(secondAE.getOutputBiasLayer(), secondAE.getOutputLayer())).getWeights();
  float[] e8 = t.getElements();
  t.forEach(i -> e8[i] = 0.9f);

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

  ValuesProvider results = TensorFactory.tensorProvider(sae, 1, true);
  results.get(sae.getInputLayer()).set(1, 0, 0);
  results.get(sae.getInputLayer()).set(0, 1, 0);
  results.get(sae.getInputLayer()).set(1, 2, 0);

  sae.getLayerCalculator().calculate(sae, sae.getOutputLayer(), calculatedLayers, results);

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

TOP

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

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.