Java Code Examples for org.deeplearning4j.nn.conf.NeuralNetConfiguration#Builder

The following examples show how to use org.deeplearning4j.nn.conf.NeuralNetConfiguration#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSameDiffLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public void applyGlobalConfig(NeuralNetConfiguration.Builder b) {
    if (regularization == null || regularization.isEmpty()) {
        regularization = b.getRegularization();
    }
    if (regularizationBias == null || regularizationBias.isEmpty()) {
        regularizationBias = b.getRegularizationBias();
    }
    if (updater == null) {
        updater = b.getIUpdater();
    }
    if (biasUpdater == null) {
        biasUpdater = b.getBiasUpdater();
    }
    if (gradientNormalization == null) {
        gradientNormalization = b.getGradientNormalization();
    }
    if (Double.isNaN(gradientNormalizationThreshold)) {
        gradientNormalizationThreshold = b.getGradientNormalizationThreshold();
    }

    applyGlobalConfigToLayer(b);
}
 
Example 2
Source File: SameDiffConv.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void applyGlobalConfigToLayer(NeuralNetConfiguration.Builder globalConfig) {
    if (activation == null) {
        activation = SameDiffLayerUtils.fromIActivation(globalConfig.getActivationFn());
    }
    if (cm == null) {
        cm = globalConfig.getConvolutionMode();
    }
}
 
Example 3
Source File: TransferLearningComplex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testAddOutput() {
        NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9))
                        .activation(Activation.IDENTITY);

        ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
                        .addLayer("denseCentre0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inCentre")
                        .addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inRight")
                        .addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
                        .addLayer("outRight",
                                        new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4).nOut(2).build(),
                                        "mergeRight")
                        .setOutputs("outRight").build();
        ComputationGraph modelToTune = new ComputationGraph(conf);
        modelToTune.init();

        ComputationGraph modelNow =
                        new TransferLearning.GraphBuilder(modelToTune)
                                        .addLayer("outCentre",
                                                        new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(2)
                                                                        .nOut(3).build(),
                                                        "denseCentre0")
                                        .setOutputs("outRight", "outCentre").build();

        assertEquals(2, modelNow.getNumOutputArrays());
        MultiDataSet rand = new MultiDataSet(new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 2)},
                        new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 3)});
        modelNow.fit(rand);
//        log.info(modelNow.summary());
//        log.info(modelNow.summary(InputType.feedForward(2),InputType.feedForward(2)));
        modelNow.summary();
        modelNow.summary(InputType.feedForward(2),InputType.feedForward(2));
    }
 
Example 4
Source File: LocallyConnected1D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void applyGlobalConfigToLayer(NeuralNetConfiguration.Builder globalConfig) {
    if (activation == null) {
        activation = SameDiffLayerUtils.fromIActivation(globalConfig.getActivationFn());
    }
    if (cm == null) {
        cm = globalConfig.getConvolutionMode();
    }
}
 
Example 5
Source File: LocallyConnected2D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void applyGlobalConfigToLayer(NeuralNetConfiguration.Builder globalConfig) {
    if (activation == null) {
        activation = SameDiffLayerUtils.fromIActivation(globalConfig.getActivationFn());
    }
    if (cm == null) {
        cm = globalConfig.getConvolutionMode();
    }
}
 
Example 6
Source File: TransferLearningMLNTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllWithCNNNew() {
    Nd4j.getRandom().setSeed(12345);

    DataSet randomData = new DataSet(Nd4j.rand(DataType.FLOAT,10, 28 * 28 * 3).reshape(10, 3, 28, 28), TestUtils.randomOneHot(10, 10));
    MultiLayerNetwork modelToFineTune =
            new MultiLayerNetwork(
                    new NeuralNetConfiguration.Builder().seed(123)
                            .weightInit(WeightInit.XAVIER)
                            .updater(new Nesterovs(0.01, 0.9))
                            .list()
                            .layer(0, new ConvolutionLayer.Builder(5, 5).nIn(3).stride(1, 1)
                                    .nOut(20).activation(Activation.IDENTITY).build())
                            .layer(1, new SubsamplingLayer.Builder(PoolingType.MAX)
                                    .kernelSize(2, 2).stride(2, 2).build())
                            .layer(2, new ConvolutionLayer.Builder(5, 5).stride(1, 1)
                                    .nOut(50).activation(Activation.IDENTITY).build())
                            .layer(3, new SubsamplingLayer.Builder(PoolingType.MAX)
                                    .kernelSize(2, 2).stride(2, 2).build())
                            .layer(4, new DenseLayer.Builder().activation(Activation.RELU).nOut(500).build())
                            .layer(5, new DenseLayer.Builder().activation(Activation.RELU).nOut(250).build())
                            .layer(6, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                                    .nOut(100).activation(Activation.SOFTMAX).build())
                            .setInputType(InputType.convolutionalFlat(28, 28, 3)) //See note below
                            .build());
    modelToFineTune.init();
    INDArray asFrozenFeatures = modelToFineTune.feedForwardToLayer(2, randomData.getFeatures(), false).get(2); //10x20x12x12

    NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.2));
    FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.2)).build();

    MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
                    .setFeatureExtractor(1).removeLayersFromOutput(5)
                    .addLayer(new DenseLayer.Builder().activation(Activation.RELU).nIn(12 * 12 * 20).nOut(300)
                                    .build())
                    .addLayer(new DenseLayer.Builder().activation(Activation.RELU).nIn(300).nOut(150).build())
                    .addLayer(new DenseLayer.Builder().activation(Activation.RELU).nIn(150).nOut(50).build())
                    .addLayer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                                    .activation(Activation.SOFTMAX).nIn(50).nOut(10).build())
                    .setInputPreProcessor(2, new CnnToFeedForwardPreProcessor(12, 12, 20)).build();


    MultiLayerNetwork notFrozen = new MultiLayerNetwork(equivalentConf.list()
                    .layer(0, new DenseLayer.Builder().activation(Activation.RELU).nIn(12 * 12 * 20).nOut(300)
                                    .build())
                    .layer(1, new DenseLayer.Builder().activation(Activation.RELU).nIn(300).nOut(150).build())
                    .layer(2, new DenseLayer.Builder().activation(Activation.RELU).nIn(150).nOut(50).build())
                    .layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nIn(50)
                                    .nOut(10).activation(Activation.SOFTMAX).build())
                    .inputPreProcessor(0, new CnnToFeedForwardPreProcessor(12, 12, 20))
                    .build());
    notFrozen.init();

    assertArrayEquals(modelToFineTune.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
    //subsampling has no params
    //assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
    assertArrayEquals(notFrozen.getLayer(0).params().shape(), modelNow.getLayer(2).params().shape());
    modelNow.getLayer(2).setParams(notFrozen.getLayer(0).params());
    assertArrayEquals(notFrozen.getLayer(1).params().shape(), modelNow.getLayer(3).params().shape());
    modelNow.getLayer(3).setParams(notFrozen.getLayer(1).params());
    assertArrayEquals(notFrozen.getLayer(2).params().shape(), modelNow.getLayer(4).params().shape());
    modelNow.getLayer(4).setParams(notFrozen.getLayer(2).params());
    assertArrayEquals(notFrozen.getLayer(3).params().shape(), modelNow.getLayer(5).params().shape());
    modelNow.getLayer(5).setParams(notFrozen.getLayer(3).params());

    int i = 0;
    while (i < 3) {
        notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
        modelNow.fit(randomData);
        i++;
    }

    INDArray expectedParams = Nd4j.hstack(modelToFineTune.getLayer(0).params(), notFrozen.params());
    assertEquals(expectedParams, modelNow.params());
}
 
Example 7
Source File: DeepFMModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
/**
 * 获取计算图配置
 * 
 * @param dimensionSizes
 * @return
 */
protected ComputationGraphConfiguration getComputationGraphConfiguration(int[] dimensionSizes) {
    NeuralNetConfiguration.Builder netBuilder = new NeuralNetConfiguration.Builder();
    // 设置随机种子
    netBuilder.seed(6);
    netBuilder.weightInit(WeightInit.XAVIER_UNIFORM);
    netBuilder.updater(new Sgd(learnRatio)).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);
    netBuilder.l1(weightRegularization);

    GraphBuilder graphBuilder = netBuilder.graphBuilder();

    // 构建离散域(SparseField)节点
    String[] inputVertexNames = new String[dimensionSizes.length];
    int[] inputVertexSizes = new int[dimensionSizes.length];
    for (int fieldIndex = 0; fieldIndex < dimensionSizes.length; fieldIndex++) {
        inputVertexNames[fieldIndex] = "SparseField" + fieldIndex;
        // 每个离散特征的输入数
        inputVertexSizes[fieldIndex] = dimensionSizes[fieldIndex];
    }
    graphBuilder.addInputs(inputVertexNames);

    // 构建Embed节点
    // TODO 应该调整为配置项.
    int numberOfFactors = 10;
    // TODO Embed只支持输入的column为1.
    String[] embedVertexNames = new String[dimensionSizes.length];
    for (int fieldIndex = 0; fieldIndex < dimensionSizes.length; fieldIndex++) {
        embedVertexNames[fieldIndex] = "Embed" + fieldIndex;
        graphBuilder.addLayer(embedVertexNames[fieldIndex], new EmbeddingLayer.Builder().nIn(inputVertexSizes[fieldIndex]).nOut(numberOfFactors).activation(Activation.IDENTITY).build(), inputVertexNames[fieldIndex]);
    }

    // 构建因子分解机部分
    // 构建FM Plus节点(实际就是FM的输入)
    numberOfFeatures = 0;
    for (int fieldIndex = 0; fieldIndex < dimensionSizes.length; fieldIndex++) {
        numberOfFeatures += inputVertexSizes[fieldIndex];
    }
    // TODO 注意,由于EmbedLayer不支持与其它Layer共享输入,所以FM Plus节点构建自己的One Hot输入.
    graphBuilder.addInputs("FMInputs");
    graphBuilder.addLayer("FMPlus", new DeepFMInputConfiguration.Builder(dimensionSizes).nOut(1).activation(Activation.IDENTITY).build(), "FMInputs");

    // 构建FM Product节点
    // 注意:节点数量是(n*(n-1)/2)),n为Embed节点数量
    String[] productVertexNames = new String[dimensionSizes.length * (dimensionSizes.length - 1) / 2];
    int productIndex = 0;
    for (int outterFieldIndex = 0; outterFieldIndex < dimensionSizes.length; outterFieldIndex++) {
        for (int innerFieldIndex = outterFieldIndex + 1; innerFieldIndex < dimensionSizes.length; innerFieldIndex++) {
            productVertexNames[productIndex] = "FMProduct" + outterFieldIndex + ":" + innerFieldIndex;
            String left = embedVertexNames[outterFieldIndex];
            String right = embedVertexNames[innerFieldIndex];
            graphBuilder.addVertex(productVertexNames[productIndex], new DeepFMProductConfiguration(), left, right);
            productIndex++;
        }
    }

    // 构建FM Sum节点(实际就是FM的输出)
    String[] names = new String[productVertexNames.length + 1];
    System.arraycopy(productVertexNames, 0, names, 0, productVertexNames.length);
    names[names.length - 1] = "FMPlus";
    graphBuilder.addVertex("FMOutput", new DeepFMSumConfiguration(), names);

    // 构建多层网络部分
    // 构建Net Input节点
    // TODO 调整为支持输入(连续域)Dense Field.
    // TODO 应该调整为配置项.
    int numberOfHiddens = 100;
    graphBuilder.addLayer("NetInput", new DenseLayer.Builder().nIn(dimensionSizes.length * numberOfFactors).nOut(numberOfHiddens).activation(Activation.LEAKYRELU).build(), embedVertexNames);

    // TODO 应该调整为配置项.
    int numberOfLayers = 5;
    String currentLayer = "NetInput";
    for (int layerIndex = 0; layerIndex < numberOfLayers; layerIndex++) {
        graphBuilder.addLayer("NetHidden" + layerIndex, new DenseLayer.Builder().nIn(numberOfHiddens).nOut(numberOfHiddens).activation(Activation.LEAKYRELU).build(), currentLayer);
        currentLayer = "NetHidden" + layerIndex;
    }

    // 构建Net Output节点
    graphBuilder.addVertex("NetOutput", new DeepFMSumConfiguration(), currentLayer);

    // 构建Deep Output节点
    graphBuilder.addLayer("DeepOutput", new DeepFMOutputConfiguration.Builder(LossFunctions.LossFunction.XENT).activation(Activation.SIGMOID).nIn(2).nOut(1).build(), "FMOutput", "NetOutput");

    graphBuilder.setOutputs("DeepOutput");
    ComputationGraphConfiguration configuration = graphBuilder.build();
    return configuration;
}
 
Example 8
Source File: TransferLearningHelperTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMLN() {
    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));

    NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .activation(Activation.IDENTITY);

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(overallConf.clone().list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new DenseLayer.Builder().nIn(3).nOut(2).build())
                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build());

    modelToFineTune.init();
    MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).setFeatureExtractor(1).build();
    List<INDArray> ff = modelToFineTune.feedForwardToLayer(2, randomData.getFeatures(), false);
    INDArray asFrozenFeatures = ff.get(2);

    TransferLearningHelper helper = new TransferLearningHelper(modelToFineTune, 1);

    INDArray paramsLastTwoLayers =
                    Nd4j.hstack(modelToFineTune.getLayer(2).params(), modelToFineTune.getLayer(3).params());
    MultiLayerNetwork notFrozen = new MultiLayerNetwork(overallConf.clone().list()
                    .layer(0, new DenseLayer.Builder().nIn(2).nOut(3).build())
                    .layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build(), paramsLastTwoLayers);

    assertEquals(asFrozenFeatures, helper.featurize(randomData).getFeatures());
    assertEquals(randomData.getLabels(), helper.featurize(randomData).getLabels());

    for (int i = 0; i < 5; i++) {
        notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
        helper.fitFeaturized(helper.featurize(randomData));
        modelNow.fit(randomData);
    }

    INDArray expected = Nd4j.hstack(modelToFineTune.getLayer(0).params(), modelToFineTune.getLayer(1).params(),
                    notFrozen.params());
    INDArray act = modelNow.params();
    assertEquals(expected, act);
}
 
Example 9
Source File: TransferLearningHelperTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testFitUnFrozen() {

        NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9)).seed(124)
                        .activation(Activation.IDENTITY)
                        .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);

        ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
                        .addLayer("denseCentre0", new DenseLayer.Builder().nIn(10).nOut(9).build(), "inCentre")
                        .addLayer("denseCentre1", new DenseLayer.Builder().nIn(9).nOut(8).build(), "denseCentre0")
                        .addLayer("denseCentre2", new DenseLayer.Builder().nIn(8).nOut(7).build(), "denseCentre1")
                        .addLayer("denseCentre3", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
                        .addLayer("outCentre",
                                        new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(7).nOut(4).build(),
                                        "denseCentre3")
                        .addVertex("subsetLeft", new SubsetVertex(0, 3), "denseCentre1")
                        .addLayer("denseLeft0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "subsetLeft")
                        .addLayer("outLeft",
                                        new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(6).build(),
                                        "denseLeft0")
                        .addLayer("denseRight", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
                        .addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(3).build(), "inRight")
                        .addVertex("mergeRight", new MergeVertex(), "denseRight", "denseRight0")
                        .addLayer("denseRight1", new DenseLayer.Builder().nIn(10).nOut(5).build(), "mergeRight")
                        .addLayer("outRight",
                                        new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(5).build(),
                                        "denseRight1")
                        .setOutputs("outLeft", "outCentre", "outRight").build();

        ComputationGraph modelToTune = new ComputationGraph(conf);
        modelToTune.init();

        INDArray inRight = Nd4j.rand(10, 2);
        INDArray inCentre = Nd4j.rand(10, 10);
        INDArray outLeft = Nd4j.rand(10, 6);
        INDArray outRight = Nd4j.rand(10, 5);
        INDArray outCentre = Nd4j.rand(10, 4);
        MultiDataSet origData = new MultiDataSet(new INDArray[] {inCentre, inRight},
                        new INDArray[] {outLeft, outCentre, outRight});
        ComputationGraph modelIdentical = modelToTune.clone();
        modelIdentical.getVertex("denseCentre0").setLayerAsFrozen();
        modelIdentical.getVertex("denseCentre1").setLayerAsFrozen();
        modelIdentical.getVertex("denseCentre2").setLayerAsFrozen();

        TransferLearningHelper helper = new TransferLearningHelper(modelToTune, "denseCentre2");
        MultiDataSet featurizedDataSet = helper.featurize(origData);

        assertEquals(modelIdentical.getLayer("denseRight0").params(), modelToTune.getLayer("denseRight0").params());
        modelIdentical.fit(origData);
        helper.fitFeaturized(featurizedDataSet);

        assertEquals(modelIdentical.getLayer("denseCentre0").params(), modelToTune.getLayer("denseCentre0").params());
        assertEquals(modelIdentical.getLayer("denseCentre1").params(), modelToTune.getLayer("denseCentre1").params());
        assertEquals(modelIdentical.getLayer("denseCentre2").params(), modelToTune.getLayer("denseCentre2").params());
        assertEquals(modelIdentical.getLayer("denseCentre3").params(), modelToTune.getLayer("denseCentre3").params());
        assertEquals(modelIdentical.getLayer("outCentre").params(), modelToTune.getLayer("outCentre").params());
        assertEquals(modelIdentical.getLayer("denseRight").conf().toJson(),
                        modelToTune.getLayer("denseRight").conf().toJson());
        assertEquals(modelIdentical.getLayer("denseRight").params(), modelToTune.getLayer("denseRight").params());
        assertEquals(modelIdentical.getLayer("denseRight0").conf().toJson(),
                        modelToTune.getLayer("denseRight0").conf().toJson());
        //assertEquals(modelIdentical.getLayer("denseRight0").params(),modelToTune.getLayer("denseRight0").params());
        assertEquals(modelIdentical.getLayer("denseRight1").params(), modelToTune.getLayer("denseRight1").params());
        assertEquals(modelIdentical.getLayer("outRight").params(), modelToTune.getLayer("outRight").params());
        assertEquals(modelIdentical.getLayer("denseLeft0").params(), modelToTune.getLayer("denseLeft0").params());
        assertEquals(modelIdentical.getLayer("outLeft").params(), modelToTune.getLayer("outLeft").params());

//        log.info(modelIdentical.summary());
//        log.info(helper.unfrozenGraph().summary());
        modelIdentical.summary();
        helper.unfrozenGraph().summary();
    }
 
Example 10
Source File: TransferLearningHelperTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void tesUnfrozenSubset() {

    NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().seed(124)
                    .activation(Activation.IDENTITY)
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).updater(new Sgd(0.1));
    /*
                         (inCentre)                        (inRight)
                            |                                |
                        denseCentre0                         |
                            |                                |
             ,--------  denseCentre1                       denseRight0
            /               |                                |
    subsetLeft(0-3)    denseCentre2 ---- denseRight ----  mergeRight
          |                 |                                |
     denseLeft0        denseCentre3                        denseRight1
          |                 |                                |
      (outLeft)         (outCentre)                        (outRight)
    
     */

    ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
                    .addLayer("denseCentre0", new DenseLayer.Builder().nIn(10).nOut(9).build(), "inCentre")
                    .addLayer("denseCentre1", new DenseLayer.Builder().nIn(9).nOut(8).build(), "denseCentre0")
                    .addLayer("denseCentre2", new DenseLayer.Builder().nIn(8).nOut(7).build(), "denseCentre1")
                    .addLayer("denseCentre3", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
                    .addLayer("outCentre",
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(7).nOut(4).build(),
                                    "denseCentre3")
                    .addVertex("subsetLeft", new SubsetVertex(0, 3), "denseCentre1")
                    .addLayer("denseLeft0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "subsetLeft")
                    .addLayer("outLeft",
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(6).build(),
                                    "denseLeft0")
                    .addLayer("denseRight", new DenseLayer.Builder().nIn(7).nOut(7).build(), "denseCentre2")
                    .addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(3).build(), "inRight")
                    .addVertex("mergeRight", new MergeVertex(), "denseRight", "denseRight0")
                    .addLayer("denseRight1", new DenseLayer.Builder().nIn(10).nOut(5).build(), "mergeRight")
                    .addLayer("outRight",
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5).nOut(5).build(),
                                    "denseRight1")
                    .setOutputs("outLeft", "outCentre", "outRight").build();

    ComputationGraph modelToTune = new ComputationGraph(conf);
    modelToTune.init();

    TransferLearningHelper helper = new TransferLearningHelper(modelToTune, "denseCentre2");

    ComputationGraph modelSubset = helper.unfrozenGraph();

    ComputationGraphConfiguration expectedConf =
                    overallConf.graphBuilder().addInputs("denseCentre1", "denseCentre2", "inRight") //inputs are in sorted order
                                    .addLayer("denseCentre3", new DenseLayer.Builder().nIn(7).nOut(7).build(),
                                                    "denseCentre2")
                                    .addLayer("outCentre",
                                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(7)
                                                                    .nOut(4).build(),
                                                    "denseCentre3")
                                    .addVertex("subsetLeft", new SubsetVertex(0, 3), "denseCentre1")
                                    .addLayer("denseLeft0", new DenseLayer.Builder().nIn(4).nOut(5).build(),
                                                    "subsetLeft")
                                    .addLayer("outLeft",
                                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5)
                                                                    .nOut(6).build(),
                                                    "denseLeft0")
                                    .addLayer("denseRight", new DenseLayer.Builder().nIn(7).nOut(7).build(),
                                                    "denseCentre2")
                                    .addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(3).build(),
                                                    "inRight")
                                    .addVertex("mergeRight", new MergeVertex(), "denseRight", "denseRight0")
                                    .addLayer("denseRight1", new DenseLayer.Builder().nIn(10).nOut(5).build(),
                                                    "mergeRight")
                                    .addLayer("outRight",
                                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(5)
                                                                    .nOut(5).build(),
                                                    "denseRight1")
                                    .setOutputs("outLeft", "outCentre", "outRight").build();
    ComputationGraph expectedModel = new ComputationGraph(expectedConf);
    expectedModel.init();
    assertEquals(expectedConf.toJson(), modelSubset.getConfiguration().toJson());
}
 
Example 11
Source File: TransferLearningComplex.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testLessSimpleMergeBackProp() {

    NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9))
                    .activation(Activation.IDENTITY);

    /*
            inCentre                inRight
               |                        |
         denseCentre0               denseRight0
               |                        |
               |------ mergeRight ------|
               |            |
             outCentre     outRight
    
    */

    ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
                    .addLayer("denseCentre0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inCentre")
                    .addLayer("outCentre", new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(2).nOut(2).build(),"denseCentre0")
                    .addLayer("denseRight0", new DenseLayer.Builder().nIn(3).nOut(2).build(), "inRight")
                    .addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
                    .addLayer("outRight", new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4).nOut(2).build(),"mergeRight")
                    .setOutputs("outCentre", "outRight").build();
    ComputationGraph modelToTune = new ComputationGraph(conf);
    modelToTune.init();
    modelToTune.getVertex("denseCentre0").setLayerAsFrozen();

    MultiDataSet randData = new MultiDataSet(new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 3)},
                    new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 2)});
    INDArray denseCentre0 = modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0");
    MultiDataSet otherRandData =
                    new MultiDataSet(new INDArray[] {denseCentre0, randData.getFeatures(1)}, randData.getLabels());

    ComputationGraph modelNow =
                    new TransferLearning.GraphBuilder(modelToTune).setFeatureExtractor("denseCentre0").build();
    assertTrue(modelNow.getLayer("denseCentre0") instanceof FrozenLayer);
    int n = 0;
    while (n < 5) {
        //confirm activations out of frozen vertex is the same as the input to the other model
        modelToTune.fit(randData);
        modelNow.fit(randData);

        assertEquals(otherRandData.getFeatures(0),
                        modelNow.feedForward(randData.getFeatures(), false).get("denseCentre0"));
        assertEquals(otherRandData.getFeatures(0),
                        modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0"));

        assertEquals(modelToTune.getLayer("denseRight0").params(), modelNow.getLayer("denseRight0").params());

        assertEquals(modelToTune.getLayer("outRight").params(), modelNow.getLayer("outRight").params());

        assertEquals(modelToTune.getLayer("outCentre").params(), modelNow.getLayer("outCentre").params());
        n++;
    }

}
 
Example 12
Source File: TransferLearningComplex.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimplerMergeBackProp() {

    NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.9))
                    .activation(Activation.IDENTITY)
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);

    /*
            inCentre                inRight
               |                        |
         denseCentre0               denseRight0
               |                        |
               |------ mergeRight ------|
                            |
                          outRight
    
    */

    ComputationGraphConfiguration conf = overallConf.graphBuilder().addInputs("inCentre", "inRight")
                    .addLayer("denseCentre0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inCentre")
                    .addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "inRight")
                    .addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
                    .addLayer("outRight",
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4).nOut(2).build(),
                                    "mergeRight")
                    .setOutputs("outRight").build();
    ComputationGraph modelToTune = new ComputationGraph(conf);
    modelToTune.init();

    MultiDataSet randData = new MultiDataSet(new INDArray[] {Nd4j.rand(2, 2), Nd4j.rand(2, 2)},
                    new INDArray[] {Nd4j.rand(2, 2)});
    INDArray denseCentre0 = modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0");
    MultiDataSet otherRandData =
                    new MultiDataSet(new INDArray[] {denseCentre0, randData.getFeatures(1)}, randData.getLabels());

    ComputationGraphConfiguration otherConf =
                    overallConf.graphBuilder().addInputs("denseCentre0", "inRight")
                                    .addLayer("denseRight0", new DenseLayer.Builder().nIn(2).nOut(2).build(),
                                                    "inRight")
                                    .addVertex("mergeRight", new MergeVertex(), "denseCentre0", "denseRight0")
                                    .addLayer("outRight",
                                                    new OutputLayer.Builder(LossFunctions.LossFunction.MSE).nIn(4)
                                                                    .nOut(2).build(),
                                                    "mergeRight")
                                    .setOutputs("outRight").build();
    ComputationGraph modelOther = new ComputationGraph(otherConf);
    modelOther.init();
    modelOther.getLayer("denseRight0").setParams(modelToTune.getLayer("denseRight0").params());
    modelOther.getLayer("outRight").setParams(modelToTune.getLayer("outRight").params());

    modelToTune.getVertex("denseCentre0").setLayerAsFrozen();
    ComputationGraph modelNow =
                    new TransferLearning.GraphBuilder(modelToTune).setFeatureExtractor("denseCentre0").build();
    int n = 0;
    while (n < 5) {
        if (n == 0) {
            //confirm activations out of the merge are equivalent
            assertEquals(modelToTune.feedForward(randData.getFeatures(), false).get("mergeRight"),
                            modelOther.feedForward(otherRandData.getFeatures(), false).get("mergeRight"));
            assertEquals(modelNow.feedForward(randData.getFeatures(), false).get("mergeRight"),
                            modelOther.feedForward(otherRandData.getFeatures(), false).get("mergeRight"));
        }
        //confirm activations out of frozen vertex is the same as the input to the other model
        modelOther.fit(otherRandData);
        modelToTune.fit(randData);
        modelNow.fit(randData);

        assertEquals(otherRandData.getFeatures(0),
                        modelNow.feedForward(randData.getFeatures(), false).get("denseCentre0"));
        assertEquals(otherRandData.getFeatures(0),
                        modelToTune.feedForward(randData.getFeatures(), false).get("denseCentre0"));

        assertEquals(modelOther.getLayer("denseRight0").params(), modelNow.getLayer("denseRight0").params());
        assertEquals(modelOther.getLayer("denseRight0").params(), modelToTune.getLayer("denseRight0").params());

        assertEquals(modelOther.getLayer("outRight").params(), modelNow.getLayer("outRight").params());
        assertEquals(modelOther.getLayer("outRight").params(), modelToTune.getLayer("outRight").params());
        n++;
    }

}
 
Example 13
Source File: FrozenLayerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void cloneCompGraphFrozen() {

    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));

    NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
                    .activation(Activation.IDENTITY);

    ComputationGraph modelToFineTune = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
                    .addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(3).build(), "layer0In")
                    .addLayer("layer1", new DenseLayer.Builder().nIn(3).nOut(2).build(), "layer0")
                    .addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(3).build(), "layer1")
                    .addLayer("layer3",
                                    new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                                    LossFunctions.LossFunction.MCXENT)
                                                                    .activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                                    .build(),
                                    "layer2")
                    .setOutputs("layer3").build());

    modelToFineTune.init();
    INDArray asFrozenFeatures = modelToFineTune.feedForward(randomData.getFeatures(), false).get("layer1");
    ComputationGraph modelNow =
                    new TransferLearning.GraphBuilder(modelToFineTune).setFeatureExtractor("layer1").build();

    ComputationGraph clonedModel = modelNow.clone();

    //Check json
    assertEquals(clonedModel.getConfiguration().toJson(), modelNow.getConfiguration().toJson());

    //Check params
    assertEquals(modelNow.params(), clonedModel.params());

    ComputationGraph notFrozen = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
                    .addLayer("layer0", new DenseLayer.Builder().nIn(2).nOut(3).build(), "layer0In")
                    .addLayer("layer1",
                                    new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                                    LossFunctions.LossFunction.MCXENT)
                                                                    .activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                                    .build(),
                                    "layer0")
                    .setOutputs("layer1").build());
    notFrozen.init();
    notFrozen.setParams(Nd4j.hstack(modelToFineTune.getLayer("layer2").params(),
                    modelToFineTune.getLayer("layer3").params()));


    int i = 0;
    while (i < 5) {
        notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
        modelNow.fit(randomData);
        clonedModel.fit(randomData);
        i++;
    }

    INDArray expectedParams = Nd4j.hstack(modelToFineTune.getLayer("layer0").params(),
                    modelToFineTune.getLayer("layer1").params(), notFrozen.params());
    assertEquals(expectedParams, modelNow.params());
    assertEquals(expectedParams, clonedModel.params());
}
 
Example 14
Source File: TransferLearningCompGraphTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveAndAdd() {
    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));

    NeuralNetConfiguration.Builder overallConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1))
                    .activation(Activation.IDENTITY);
    FineTuneConfiguration fineTuneConfiguration = new FineTuneConfiguration.Builder().updater(new Sgd(0.1))
                    .activation(Activation.IDENTITY).build();

    ComputationGraph modelToFineTune = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
                    .addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(5).build(), "layer0In")
                    .addLayer("layer1", new DenseLayer.Builder().nIn(5).nOut(2).build(), "layer0")
                    .addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(3).build(), "layer1")
                    .addLayer("layer3",
                                    new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                                    LossFunctions.LossFunction.MCXENT)
                                                                    .activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                                    .build(),
                                    "layer2")
                    .setOutputs("layer3").build());
    modelToFineTune.init();

    ComputationGraph modelNow = new TransferLearning.GraphBuilder(modelToFineTune)
                    .fineTuneConfiguration(fineTuneConfiguration)
                    .nOutReplace("layer0", 7, WeightInit.XAVIER, WeightInit.XAVIER)
                    .nOutReplace("layer2", 5, WeightInit.XAVIER).removeVertexKeepConnections("layer3")
                    .addLayer("layer3",
                                    new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(3)
                                                    .activation(Activation.SOFTMAX).build(),
                                    "layer2")
                    //.setOutputs("layer3")
                    .build();

    ComputationGraph modelExpectedArch = new ComputationGraph(overallConf.graphBuilder().addInputs("layer0In")
                    .addLayer("layer0", new DenseLayer.Builder().nIn(4).nOut(7).build(), "layer0In")
                    .addLayer("layer1", new DenseLayer.Builder().nIn(7).nOut(2).build(), "layer0")
                    .addLayer("layer2", new DenseLayer.Builder().nIn(2).nOut(5).build(), "layer1")
                    .addLayer("layer3",
                                    new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                                    LossFunctions.LossFunction.MCXENT)
                                                                    .activation(Activation.SOFTMAX).nIn(5).nOut(3)
                                                                    .build(),
                                    "layer2")
                    .setOutputs("layer3").build());

    modelExpectedArch.init();

    //modelNow should have the same architecture as modelExpectedArch
    assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
    assertArrayEquals(modelExpectedArch.getLayer("layer0").params().shape(),
                    modelNow.getLayer("layer0").params().shape());
    assertArrayEquals(modelExpectedArch.getLayer("layer1").params().shape(),
                    modelNow.getLayer("layer1").params().shape());
    assertArrayEquals(modelExpectedArch.getLayer("layer2").params().shape(),
                    modelNow.getLayer("layer2").params().shape());
    assertArrayEquals(modelExpectedArch.getLayer("layer3").params().shape(),
                    modelNow.getLayer("layer3").params().shape());

    modelNow.setParams(modelExpectedArch.params());
    //fit should give the same results
    modelExpectedArch.fit(randomData);
    modelNow.fit(randomData);
    assertEquals(modelExpectedArch.score(), modelNow.score(), 1e-8);
    assertEquals(modelExpectedArch.params(), modelNow.params());
}
 
Example 15
Source File: TransferLearningMLNTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveAndAdd() {
    Nd4j.getRandom().setSeed(12345);
    DataSet randomData = new DataSet(Nd4j.rand(DataType.FLOAT,10, 4), TestUtils.randomOneHot(DataType.FLOAT, 10, 3));

    NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.1));
    FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.1)).build();

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(//overallConf.list()
                    equivalentConf.list().layer(0, new DenseLayer.Builder().nIn(4).nOut(5).build())
                                    .layer(1, new DenseLayer.Builder().nIn(5).nOut(2).build())
                                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(3).build())
                                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                                    LossFunctions.LossFunction.MCXENT)
                                                                    .activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                                    .build())
                                    .build());
    modelToFineTune.init();

    MultiLayerNetwork modelNow =
                    new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
                                    .nOutReplace(0, 7, WeightInit.XAVIER, WeightInit.XAVIER)
                                    .nOutReplace(2, 5, WeightInit.XAVIER).removeOutputLayer()
                                    .addLayer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5)
                                                    .nOut(3).updater(new Sgd(0.5)).activation(Activation.SOFTMAX)
                                                    .build())
                                    .build();

    MultiLayerNetwork modelExpectedArch = new MultiLayerNetwork(equivalentConf.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(7).build())
                    .layer(1, new DenseLayer.Builder().nIn(7).nOut(2).build())
                    .layer(2, new DenseLayer.Builder().nIn(2).nOut(5).build())
                    .layer(3, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX)
                                                    .updater(new Sgd(0.5)).nIn(5).nOut(3).build())
                    .build());
    modelExpectedArch.init();

    //modelNow should have the same architecture as modelExpectedArch
    assertArrayEquals(modelExpectedArch.params().shape(), modelNow.params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(2).params().shape(), modelNow.getLayer(2).params().shape());
    assertArrayEquals(modelExpectedArch.getLayer(3).params().shape(), modelNow.getLayer(3).params().shape());

    modelNow.setParams(modelExpectedArch.params());
    //fit should give the same results
    modelExpectedArch.fit(randomData);
    modelNow.fit(randomData);
    double scoreExpected = modelExpectedArch.score();
    double scoreActual = modelNow.score();
    assertEquals(scoreExpected, scoreActual, 1e-4);
    assertEquals(modelExpectedArch.params(), modelNow.params());
}
 
Example 16
Source File: SameDiffVertex.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public void applyGlobalConfigToLayer(NeuralNetConfiguration.Builder globalConfig) {
    //Default implementation: no op
}
 
Example 17
Source File: RecurrentAttentionLayer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void applyGlobalConfigToLayer(NeuralNetConfiguration.Builder globalConfig) {
    if (activation == null) {
        activation = SameDiffLayerUtils.fromIActivation(globalConfig.getActivationFn());
    }
}
 
Example 18
Source File: TransferLearningMLNTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllWithCNN() {
    Nd4j.getRandom().setSeed(12345);

    DataSet randomData = new DataSet(Nd4j.rand(DataType.FLOAT, 10, 28 * 28 * 3).reshape(10, 3, 28, 28), TestUtils.randomOneHot(DataType.FLOAT,10, 10));
    MultiLayerNetwork modelToFineTune =
                    new MultiLayerNetwork(
                                    new NeuralNetConfiguration.Builder().seed(123)
                                                    .weightInit(WeightInit.XAVIER)
                                                    .updater(new Nesterovs(0.01, 0.9))
                                                    .list()
                                                    .layer(0, new ConvolutionLayer.Builder(5, 5).nIn(3).stride(1, 1)
                                                                    .nOut(20).activation(Activation.IDENTITY)
                                                                    .build())
                                                    .layer(1, new SubsamplingLayer.Builder(
                                                                    SubsamplingLayer.PoolingType.MAX)
                                                                                    .kernelSize(2, 2).stride(2, 2)
                                                                                    .build())
                                                    .layer(2, new ConvolutionLayer.Builder(5, 5).stride(1, 1)
                                                                    .nOut(50).activation(Activation.IDENTITY)
                                                                    .build())
                                                    .layer(3, new SubsamplingLayer.Builder(
                                                                    SubsamplingLayer.PoolingType.MAX)
                                                                                    .kernelSize(2, 2).stride(2, 2)
                                                                                    .build())
                                                    .layer(4, new DenseLayer.Builder().activation(Activation.RELU)
                                                                    .nOut(500).build())
                                                    .layer(5, new DenseLayer.Builder().activation(Activation.RELU)
                                                                    .nOut(250).build())
                                                    .layer(6, new OutputLayer.Builder(
                                                                    LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                                                                                    .nOut(100)
                                                                                    .activation(Activation.SOFTMAX)
                                                                                    .build())
                                                    .setInputType(InputType.convolutionalFlat(28, 28, 3))
                                                    .build());
    modelToFineTune.init();
    INDArray asFrozenFeatures = modelToFineTune.feedForwardToLayer(2, randomData.getFeatures(), false).get(2); //10x20x12x12

    NeuralNetConfiguration.Builder equivalentConf = new NeuralNetConfiguration.Builder().updater(new Sgd(0.2))
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT);

    FineTuneConfiguration overallConf = new FineTuneConfiguration.Builder().updater(new Sgd(0.2))
                    .build();

    MultiLayerNetwork modelNow = new TransferLearning.Builder(modelToFineTune).fineTuneConfiguration(overallConf)
                    .setFeatureExtractor(1).nOutReplace(4, 600, WeightInit.XAVIER).removeLayersFromOutput(2)
                    .addLayer(new DenseLayer.Builder().activation(Activation.RELU).nIn(600).nOut(300).build())
                    .addLayer(new DenseLayer.Builder().activation(Activation.RELU).nIn(300).nOut(150).build())
                    .addLayer(new DenseLayer.Builder().activation(Activation.RELU).nIn(150).nOut(50).build())
                    .addLayer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                                    .activation(Activation.SOFTMAX).nIn(50).nOut(10).build())
                    .build();

    MultiLayerNetwork notFrozen = new MultiLayerNetwork(equivalentConf.list()
                    .layer(0, new ConvolutionLayer.Builder(5, 5).stride(1, 1).nOut(50)
                                    .activation(Activation.IDENTITY).build())
                    .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2)
                                    .stride(2, 2).build())
                    .layer(2, new DenseLayer.Builder().activation(Activation.RELU).nOut(600).build())
                    .layer(3, new DenseLayer.Builder().activation(Activation.RELU).nOut(300).build())
                    .layer(4, new DenseLayer.Builder().activation(Activation.RELU).nOut(150).build())
                    .layer(5, new DenseLayer.Builder().activation(Activation.RELU).nOut(50).build())
                    .layer(6, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(10)
                                    .activation(Activation.SOFTMAX).build())
                    .setInputType(InputType.convolutionalFlat(12, 12, 20)).build());
    notFrozen.init();

    assertArrayEquals(modelToFineTune.getLayer(0).params().shape(), modelNow.getLayer(0).params().shape());
    //subsampling has no params
    //assertArrayEquals(modelExpectedArch.getLayer(1).params().shape(), modelNow.getLayer(1).params().shape());
    assertArrayEquals(notFrozen.getLayer(0).params().shape(), modelNow.getLayer(2).params().shape());
    modelNow.getLayer(2).setParams(notFrozen.getLayer(0).params());
    //subsampling has no params
    //assertArrayEquals(notFrozen.getLayer(1).params().shape(), modelNow.getLayer(3).params().shape());
    assertArrayEquals(notFrozen.getLayer(2).params().shape(), modelNow.getLayer(4).params().shape());
    modelNow.getLayer(4).setParams(notFrozen.getLayer(2).params());
    assertArrayEquals(notFrozen.getLayer(3).params().shape(), modelNow.getLayer(5).params().shape());
    modelNow.getLayer(5).setParams(notFrozen.getLayer(3).params());
    assertArrayEquals(notFrozen.getLayer(4).params().shape(), modelNow.getLayer(6).params().shape());
    modelNow.getLayer(6).setParams(notFrozen.getLayer(4).params());
    assertArrayEquals(notFrozen.getLayer(5).params().shape(), modelNow.getLayer(7).params().shape());
    modelNow.getLayer(7).setParams(notFrozen.getLayer(5).params());
    assertArrayEquals(notFrozen.getLayer(6).params().shape(), modelNow.getLayer(8).params().shape());
    modelNow.getLayer(8).setParams(notFrozen.getLayer(6).params());

    int i = 0;
    while (i < 3) {
        notFrozen.fit(new DataSet(asFrozenFeatures, randomData.getLabels()));
        modelNow.fit(randomData);
        i++;
    }

    INDArray expectedParams = Nd4j.hstack(modelToFineTune.getLayer(0).params(), notFrozen.params());
    assertEquals(expectedParams, modelNow.params());
}
 
Example 19
Source File: KerasModel.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Configure a ComputationGraph from this Keras Model configuration.
 *
 * @return ComputationGraph
 */
public ComputationGraphConfiguration getComputationGraphConfiguration()
        throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
    if (!this.className.equals(config.getFieldClassNameModel()) && !this.className.equals(config.getFieldClassNameSequential()))

        throw new InvalidKerasConfigurationException(
                "Keras model class name " + this.className + " incompatible with ComputationGraph");
    NeuralNetConfiguration.Builder modelBuilder = new NeuralNetConfiguration.Builder();

    if (optimizer != null) {
        modelBuilder.updater(optimizer);
    }

    ComputationGraphConfiguration.GraphBuilder graphBuilder = modelBuilder.graphBuilder();
    // NOTE: normally this is disallowed in DL4J. However, in Keras you can create disconnected graph vertices.
    // The responsibility for doing this correctly is that of the Keras user.
    graphBuilder.allowDisconnected(true);


    /* Build String array of input layer names, add to ComputationGraph. */
    String[] inputLayerNameArray = new String[this.inputLayerNames.size()];
    this.inputLayerNames.toArray(inputLayerNameArray);
    graphBuilder.addInputs(inputLayerNameArray);

    /* Build InputType array of input layer types, add to ComputationGraph. */
    List<InputType> inputTypeList = new ArrayList<>();
    for (String inputLayerName : this.inputLayerNames)
        inputTypeList.add(this.layers.get(inputLayerName).getOutputType());
    InputType[] inputTypes = new InputType[inputTypeList.size()];
    inputTypeList.toArray(inputTypes);
    graphBuilder.setInputTypes(inputTypes);

    /* Build String array of output layer names, add to ComputationGraph. */
    String[] outputLayerNameArray = new String[this.outputLayerNames.size()];
    this.outputLayerNames.toArray(outputLayerNameArray);
    graphBuilder.setOutputs(outputLayerNameArray);

    Map<String, InputPreProcessor> preprocessors = new HashMap<>();

    /* Add layersOrdered one at a time. */
    for (KerasLayer layer : this.layersOrdered) {
        /* Get inbound layer names. */
        List<String> inboundLayerNames = layer.getInboundLayerNames();
        String[] inboundLayerNamesArray = new String[inboundLayerNames.size()];
        inboundLayerNames.toArray(inboundLayerNamesArray);

        /* Get inbound InputTypes and InputPreProcessor, if necessary. */
        List<InputType> inboundTypeList = new ArrayList<>();
        for (String layerName : inboundLayerNames)
            inboundTypeList.add(this.outputTypes.get(layerName));
        InputType[] inboundTypeArray = new InputType[inboundTypeList.size()];
        inboundTypeList.toArray(inboundTypeArray);
        InputPreProcessor preprocessor = layer.getInputPreprocessor(inboundTypeArray);

        if (layer.isLayer()) {
            if (preprocessor != null)
                preprocessors.put(layer.getLayerName(), preprocessor);
            graphBuilder.addLayer(layer.getLayerName(), layer.getLayer(), inboundLayerNamesArray);
        } else if (layer.isVertex()) { // Ignore "preprocessor" layers for now
            if (preprocessor != null)
                preprocessors.put(layer.getLayerName(), preprocessor);
            graphBuilder.addVertex(layer.getLayerName(), layer.getVertex(), inboundLayerNamesArray);
        } else if (layer.isInputPreProcessor()) {
            if (preprocessor == null)
                throw new UnsupportedKerasConfigurationException("Layer " + layer.getLayerName()
                        + " could not be mapped to Layer, Vertex, or InputPreProcessor");
            graphBuilder.addVertex(layer.getLayerName(), new PreprocessorVertex(preprocessor),
                    inboundLayerNamesArray);
        }
    }
    graphBuilder.setInputPreProcessors(preprocessors);

    /* Whether to use standard backprop (or BPTT) or truncated BPTT. */
    if (this.useTruncatedBPTT && this.truncatedBPTT > 0)
        graphBuilder.backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(truncatedBPTT)
                .tBPTTBackwardLength(truncatedBPTT);
    else
        graphBuilder.backpropType(BackpropType.Standard);

    return graphBuilder.build();
}
 
Example 20
Source File: SameDiffMSEOutputLayer.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
@Override
public void applyGlobalConfigToLayer(NeuralNetConfiguration.Builder globalConfig){

}