Java Code Examples for org.deeplearning4j.nn.multilayer.MultiLayerNetwork#getLayers()

The following examples show how to use org.deeplearning4j.nn.multilayer.MultiLayerNetwork#getLayers() . 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: MultiLayerNeuralNetConfigurationTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testTrainingListener() {
    MultiLayerNetwork model1 = new MultiLayerNetwork(getConf());
    model1.init();
    model1.addListeners( new ScoreIterationListener(1));

    MultiLayerNetwork model2 = new MultiLayerNetwork(getConf());
    model2.addListeners( new ScoreIterationListener(1));
    model2.init();

    Layer[] l1 = model1.getLayers();
    for (int i = 0; i < l1.length; i++)
        assertTrue(l1[i].getListeners() != null && l1[i].getListeners().size() == 1);

    Layer[] l2 = model2.getLayers();
    for (int i = 0; i < l2.length; i++)
        assertTrue(l2[i].getListeners() != null && l2[i].getListeners().size() == 1);
}
 
Example 2
Source File: TransferLearning.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a model with the fine tune configuration and specified architecture changes.
 * .init() need not be called. Can be directly fit.
 *
 * @return MultiLayerNetwork
 */
public MultiLayerNetwork build() {

    if (!prepDone) {
        doPrep();
    }

    editedModel = new MultiLayerNetwork(constructConf(), constructParams());
    if (frozenTill != -1) {
        org.deeplearning4j.nn.api.Layer[] layers = editedModel.getLayers();
        for (int i = frozenTill; i >= 0; i--) {
            //Complication here: inner Layer (implementation) NeuralNetConfiguration.layer (config) should keep
            // the original layer config. While network NNC should have the frozen layer, for to/from JSON etc
            NeuralNetConfiguration origNNC = editedModel.getLayerWiseConfigurations().getConf(i);
            NeuralNetConfiguration layerNNC = origNNC.clone();
            layers[i].setConf(layerNNC);
            layers[i] = new FrozenLayer(layers[i]);

            if (origNNC.getVariables() != null) {
                List<String> vars = origNNC.variables(true);
                origNNC.clearVariables();
                layerNNC.clearVariables();
                for (String s : vars) {
                    origNNC.variables(false).add(s);
                    layerNNC.variables(false).add(s);
                }
            }

            Layer origLayerConf = editedModel.getLayerWiseConfigurations().getConf(i).getLayer();
            Layer newLayerConf = new org.deeplearning4j.nn.conf.layers.misc.FrozenLayer(origLayerConf);
            newLayerConf.setLayerName(origLayerConf.getLayerName());
            editedModel.getLayerWiseConfigurations().getConf(i).setLayer(newLayerConf);
        }
        editedModel.setLayers(layers);
    }

    return editedModel;
}
 
Example 3
Source File: MultiLayerUpdater.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public MultiLayerUpdater(MultiLayerNetwork network, INDArray updaterState) {
    super(network, updaterState);

    layersByName = new HashMap<>();
    Layer[] l = network.getLayers();
    for (int i = 0; i < l.length; i++) {
        layersByName.put(String.valueOf(i), l[i]);
    }
}
 
Example 4
Source File: BaseLayerTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetExistingParamsDenseMultiLayer() {
    MultiLayerNetwork net = configureMultiLayer();

    for (Layer layer : net.getLayers()) {
        assertNotEquals(paramTable, layer.paramTable());
        layer.setParamTable(paramTable);
        assertEquals(paramTable, layer.paramTable());
    }
}
 
Example 5
Source File: CNNGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCnnSamePaddingMode() {
    int nOut = 2;

    int[] minibatchSizes = {1, 3, 3, 2, 1, 2};
    int[] heights = new int[]{4, 5, 6, 5, 4, 4}; //Same padding mode: insensitive to exact input size...
    int[] kernelSizes = new int[]{2, 3, 2, 3, 2, 3};
    int[] inputDepths = {1, 2, 4, 3, 2, 3};

    int width = 5;

    Nd4j.getRandom().setSeed(12345);

    boolean nchw = format == CNN2DFormat.NCHW;

    for( int i=0; i<minibatchSizes.length; i++ ){
        int inputDepth = inputDepths[i];
        int minibatchSize = minibatchSizes[i];
        int height = heights[i];
        int k = kernelSizes[i];

        long[] inShape = nchw ? new long[]{minibatchSize, inputDepth, height, width} : new long[]{minibatchSize, height, width, inputDepth};
        INDArray input = Nd4j.rand(DataType.DOUBLE, inShape);

        INDArray labels = TestUtils.randomOneHot(minibatchSize, nOut);

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345)
                .dataType(DataType.DOUBLE)
                .updater(new NoOp())
                .activation(Activation.TANH).convolutionMode(Same).list()
                .layer(0, new ConvolutionLayer.Builder().name("layer 0").kernelSize(k, k)
                        .stride(1, 1).padding(0, 0).nIn(inputDepth).nOut(2).build())
                .layer(1, new SubsamplingLayer.Builder()
                        .poolingType(SubsamplingLayer.PoolingType.MAX).kernelSize(k, k)
                        .stride(1, 1).padding(0, 0).build())
                .layer(2, new ConvolutionLayer.Builder().nIn(2).nOut(2).kernelSize(k, k)
                        .stride(1, 1).padding(0, 0).build())
                .layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                        .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutional(height, width, inputDepth, format)).build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        for (int j = 0; j < net.getLayers().length; j++) {
            System.out.println("nParams, layer " + j + ": " + net.getLayer(j).numParams());
        }

        String msg = "Minibatch=" + minibatchSize + ", inDepth=" + inputDepth + ", height=" + height
                + ", kernelSize=" + k;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);

        TestUtils.testModelSerialization(net);
    }
}
 
Example 6
Source File: CNNGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCnnSamePaddingModeStrided() {
    int nOut = 2;

    int[] minibatchSizes = {1, 3};
    int width = 16;
    int height = 16;
    int[] kernelSizes = new int[]{2, 3};
    int[] strides = {1, 2, 3};
    int[] inputDepths = {1, 3};

    Nd4j.getRandom().setSeed(12345);

    boolean nchw = format == CNN2DFormat.NCHW;

    for (int inputDepth : inputDepths) {
        for (int minibatchSize : minibatchSizes) {
            for (int stride : strides) {
                for (int k : kernelSizes) {
                    for (boolean convFirst : new boolean[]{true, false}) {
                        long[] inShape = nchw ? new long[]{minibatchSize, inputDepth, height, width} : new long[]{minibatchSize, height, width, inputDepth};
                        INDArray input = Nd4j.rand(DataType.DOUBLE, inShape);

                        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
                        for (int i = 0; i < minibatchSize; i++) {
                            labels.putScalar(new int[]{i, i % nOut}, 1.0);
                        }

                        Layer convLayer = new ConvolutionLayer.Builder().name("layer 0").kernelSize(k, k)
                                .stride(stride, stride).padding(0, 0).nIn(inputDepth).nOut(2).build();

                        Layer poolLayer = new SubsamplingLayer.Builder()
                                .poolingType(SubsamplingLayer.PoolingType.MAX).kernelSize(k, k)
                                .stride(stride, stride).padding(0, 0).build();

                        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345)
                                .dataType(DataType.DOUBLE)
                                .updater(new NoOp())
                                .activation(Activation.TANH).convolutionMode(Same).list()
                                .layer(0, convFirst ? convLayer : poolLayer)
                                .layer(1, convFirst ? poolLayer : convLayer)
                                .layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                        .activation(Activation.SOFTMAX).nOut(nOut).build())
                                .setInputType(InputType.convolutional(height, width, inputDepth, format))
                                .build();

                        MultiLayerNetwork net = new MultiLayerNetwork(conf);
                        net.init();

                        for (int i = 0; i < net.getLayers().length; i++) {
                            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
                        }

                        String msg = "Minibatch=" + minibatchSize + ", inDepth=" + inputDepth + ", height=" + height
                                + ", kernelSize=" + k + ", stride = " + stride + ", convLayer first = "
                                + convFirst;
                        System.out.println(msg);

                        boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(input)
                                .labels(labels).subset(true).maxPerParam(128));

                        assertTrue(msg, gradOK);

                        TestUtils.testModelSerialization(net);
                    }
                }
            }
        }
    }
}
 
Example 7
Source File: CNNGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeconvolution2D() {
    int nOut = 2;

    int[] minibatchSizes = new int[]{1, 3, 3, 1, 3};
    int[] kernelSizes = new int[]{1, 1, 1, 3, 3};
    int[] strides = {1, 1, 2, 2, 2};
    int[] dilation = {1, 2, 1, 2, 2};
    Activation[] activations = new Activation[]{Activation.SIGMOID, Activation.TANH, Activation.SIGMOID, Activation.SIGMOID, Activation.SIGMOID};
    ConvolutionMode[] cModes = new ConvolutionMode[]{Same, Same, Truncate, Truncate, Truncate};
    int width = 7;
    int height = 7;
    int inputDepth = 3;

    Nd4j.getRandom().setSeed(12345);

    boolean nchw = format == CNN2DFormat.NCHW;

    for (int i = 0; i < minibatchSizes.length; i++) {
        int minibatchSize = minibatchSizes[i];
        int k = kernelSizes[i];
        int s = strides[i];
        int d = dilation[i];
        ConvolutionMode cm = cModes[i];
        Activation act = activations[i];


        int w = d * width;
        int h = d * height;

        long[] inShape = nchw ? new long[]{minibatchSize, inputDepth, h, w} : new long[]{minibatchSize, h, w, inputDepth};
        INDArray input = Nd4j.rand(DataType.DOUBLE, inShape);


        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int j = 0; j < minibatchSize; j++) {
            labels.putScalar(new int[]{j, j % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .dataType(DataType.DOUBLE)
                .updater(new NoOp())
                .activation(act)
                .list()
                .layer(new Deconvolution2D.Builder().name("deconvolution_2D_layer")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .convolutionMode(cm)
                        .nIn(inputDepth).nOut(nOut).build());

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutional(h, w, inputDepth, format)).build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        for (int j = 0; j < net.getLayers().length; j++) {
            System.out.println("nParams, layer " + j + ": " + net.getLayer(j).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(input)
                .labels(labels).subset(true).maxPerParam(100));

        assertTrue(msg, gradOK);

        TestUtils.testModelSerialization(net);
    }
}
 
Example 8
Source File: CNNGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSeparableConv2D() {
    int nOut = 2;
    int width = 6;
    int height = 6;
    int inputDepth = 3;

    Nd4j.getRandom().setSeed(12345);

    int[] ks = new int[]{1, 3, 3, 1, 3};
    int[] ss = new int[]{1, 1, 1, 2, 2};
    int[] ds = new int[]{1, 1, 2, 2, 2};
    ConvolutionMode[] cms = new ConvolutionMode[]{Truncate, Truncate, Truncate, Truncate, Truncate};
    int[] mb = new int[]{1, 1, 1, 3, 3};

    boolean nchw = format == CNN2DFormat.NCHW;

    for (int t = 0; t < ks.length; t++) {

        int k = ks[t];
        int s = ss[t];
        int d = ds[t];
        ConvolutionMode cm = cms[t];
        int minibatchSize = mb[t];

        //Use larger input with larger dilation values (to avoid invalid config)
        int w = d * width;
        int h = d * height;

        long[] inShape = nchw ? new long[]{minibatchSize, inputDepth, h, w} : new long[]{minibatchSize, h, w, inputDepth};
        INDArray input = Nd4j.rand(DataType.DOUBLE, inShape);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .dataType(DataType.DOUBLE)
                .updater(new NoOp())
                .activation(Activation.TANH)
                .convolutionMode(cm)
                .list()
                .layer(new SeparableConvolution2D.Builder().name("Separable conv 2D layer")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .depthMultiplier(3)
                        .nIn(inputDepth).nOut(2).build());

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutional(h, w, inputDepth, format)).build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(input)
                .labels(labels).subset(true).maxPerParam(50));  //Most params are in output layer

        assertTrue(msg, gradOK);

        TestUtils.testModelSerialization(net);
    }
}
 
Example 9
Source File: CNNGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCnnDilated() {
    int nOut = 2;
    int minibatchSize = 2;
    int width = 8;
    int height = 8;
    int inputDepth = 2;

    Nd4j.getRandom().setSeed(12345);

    boolean[] sub = new boolean[]{true, true, false, true, false};
    int[] stride = new int[]{1, 1, 1, 2, 2};
    int[] kernel = new int[]{2, 3, 3, 3, 3};
    int[] ds = new int[]{2, 2, 3, 3, 2};
    ConvolutionMode[] cms = new ConvolutionMode[]{Same, Truncate, Truncate, Same, Truncate};

    boolean nchw = format == CNN2DFormat.NCHW;

    for (int t = 0; t < sub.length; t++) {
        boolean subsampling = sub[t];
        int s = stride[t];
        int k = kernel[t];
        int d = ds[t];
        ConvolutionMode cm = cms[t];

        //Use larger input with larger dilation values (to avoid invalid config)
        int w = d * width;
        int h = d * height;

        long[] inShape = nchw ? new long[]{minibatchSize, inputDepth, h, w} : new long[]{minibatchSize, h, w, inputDepth};
        INDArray input = Nd4j.rand(DataType.DOUBLE, inShape);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .dataType(DataType.DOUBLE)
                .updater(new NoOp())
                .activation(Activation.TANH).convolutionMode(cm).list()
                .layer(new ConvolutionLayer.Builder().name("layer 0")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .nIn(inputDepth).nOut(2).build());
        if (subsampling) {
            b.layer(new SubsamplingLayer.Builder()
                    .poolingType(SubsamplingLayer.PoolingType.MAX)
                    .kernelSize(k, k)
                    .stride(s, s)
                    .dilation(d, d)
                    .build());
        } else {
            b.layer(new ConvolutionLayer.Builder().nIn(2).nOut(2)
                    .kernelSize(k, k)
                    .stride(s, s)
                    .dilation(d, d)
                    .build());
        }

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutional(h, w, inputDepth, format)).build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = (subsampling ? "subsampling" : "conv") + " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);

        TestUtils.testModelSerialization(net);
    }
}
 
Example 10
Source File: CNNGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDepthwiseConv2D() {
    int nIn = 3;
    int depthMultiplier = 2;
    int nOut = nIn * depthMultiplier;

    int width = 5;
    int height = 5;

    Nd4j.getRandom().setSeed(12345);

    int[] ks = new int[]{1,3,3,1,3};
    int[] ss = new int[]{1,1,1,2,2};
    ConvolutionMode[] cms = new ConvolutionMode[]{
            Truncate, Truncate, Truncate, Truncate, Truncate};
    int[] mb = new int[]{1,1,1,3,3};

    boolean nchw = format == CNN2DFormat.NCHW;

    for( int t=0; t<ks.length; t++ ){

        int k = ks[t];
        int s = ss[t];
        ConvolutionMode cm = cms[t];
        int minibatchSize = mb[t];

        long[] inShape = nchw ? new long[]{minibatchSize, nIn, height, width} : new long[]{minibatchSize, height, width, nIn};
        INDArray input = Nd4j.rand(DataType.DOUBLE, inShape);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .dataType(DataType.DOUBLE)
                .updater(new NoOp())
                .activation(Activation.TANH)
                .convolutionMode(cm)
                .list()
                .layer(new Convolution2D.Builder().kernelSize(1, 1).stride(1, 1).nIn(nIn).nOut(nIn).build())
                .layer(new DepthwiseConvolution2D.Builder().name("depth-wise conv 2D layer")
                        .cudnnAllowFallback(false)
                        .kernelSize(k, k)
                        .stride(s, s)
                        .depthMultiplier(depthMultiplier)
                        .nIn(nIn).build()); // nOut = nIn * depthMultiplier

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutional(height, width, nIn, format)).build();

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", nIn=" + nIn + ", depthMul=" + depthMultiplier + ", s=" + s + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(input)
                .labels(labels).subset(true).maxPerParam(256));

        assertTrue(msg, gradOK);

        TestUtils.testModelSerialization(net);
    }
}
 
Example 11
Source File: TransferLearningMLNTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleFineTune() {

    long rng = 12345L;
    Nd4j.getRandom().setSeed(rng);
    DataSet randomData = new DataSet(Nd4j.rand(DataType.FLOAT, 10, 4), TestUtils.randomOneHot(DataType.FLOAT, 10, 3));
    //original conf
    NeuralNetConfiguration.Builder confToChange =
                    new NeuralNetConfiguration.Builder().seed(rng).optimizationAlgo(OptimizationAlgorithm.LBFGS)
                                    .updater(new Nesterovs(0.01, 0.99));

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(confToChange.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).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());
    modelToFineTune.init();

    //model after applying changes with transfer learning
    MultiLayerNetwork modelNow =
            new TransferLearning.Builder(modelToFineTune)
                    .fineTuneConfiguration(new FineTuneConfiguration.Builder().seed(rng)
                            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                            .updater(new RmsProp(0.5)) //Intent: override both weight and bias LR, unless bias LR is manually set also
                            .l2(0.4).build())
                    .build();

    for (org.deeplearning4j.nn.api.Layer l : modelNow.getLayers()) {
        BaseLayer bl = ((BaseLayer) l.conf().getLayer());
        assertEquals(new RmsProp(0.5), bl.getIUpdater());
    }


    NeuralNetConfiguration.Builder confSet = new NeuralNetConfiguration.Builder().seed(rng)
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .updater(new RmsProp(0.5)).l2(0.4);

    MultiLayerNetwork expectedModel = new MultiLayerNetwork(confSet.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).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());
    expectedModel.init();
    expectedModel.setParams(modelToFineTune.params().dup());

    assertEquals(expectedModel.params(), modelNow.params());

    //Check json
    MultiLayerConfiguration expectedConf = expectedModel.getLayerWiseConfigurations();
    assertEquals(expectedConf.toJson(), modelNow.getLayerWiseConfigurations().toJson());

    //Check params after fit
    modelNow.fit(randomData);
    expectedModel.fit(randomData);

    assertEquals(modelNow.score(), expectedModel.score(), 1e-6);
    INDArray pExp = expectedModel.params();
    INDArray pNow = modelNow.params();
    assertEquals(pExp, pNow);
}
 
Example 12
Source File: ConvolutionalIterationListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void onForwardPass(Model model, List<INDArray> activations) {
    int iteration = (model instanceof MultiLayerNetwork ? ((MultiLayerNetwork)model).getIterationCount() : ((ComputationGraph)model).getIterationCount());
    if (iteration % freq == 0) {

        List<INDArray> tensors = new ArrayList<>();
        int cnt = 0;
        Random rnd = new Random();
        BufferedImage sourceImage = null;
        if (model instanceof MultiLayerNetwork) {
            MultiLayerNetwork l = (MultiLayerNetwork) model;
            Layer[] layers = l.getLayers();
            if(layers.length != activations.size())
                throw new RuntimeException();
            for( int i=0; i<layers.length; i++ ){
                if(layers[i].type() == Layer.Type.CONVOLUTIONAL){
                    INDArray output = activations.get(i+1); //Offset by 1 - activations list includes input

                    if (output.shape()[0] - 1 > Integer.MAX_VALUE)
                        throw new ND4JArraySizeException();
                    int sampleDim = output.shape()[0] == 1 ? 0 : rnd.nextInt((int) output.shape()[0] - 1) + 1;
                    if (cnt == 0) {
                        INDArray inputs = layers[i].input();

                        try {
                            sourceImage = restoreRGBImage(
                                    inputs.tensorAlongDimension(sampleDim, new int[] {3, 2, 1}));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);

                    tensors.add(tad);

                    cnt++;
                }
            }
        } else {
            //Compgraph: no op (other forward pass method should be called instead)
            return;
        }
        BufferedImage render = rasterizeConvoLayers(tensors, sourceImage);
        Persistable p = new ConvolutionListenerPersistable(sessionID, workerID, System.currentTimeMillis(), render);
        ssr.putStaticInfo(p);

        minibatchNum++;
    }
}