Esempio n. 1
0
  @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);
  }
Esempio n. 2
0
  @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);
  }