Search in sources :

Example 1 with ConvolutionHelper

use of org.deeplearning4j.nn.layers.convolution.ConvolutionHelper in project deeplearning4j by deeplearning4j.

the class CuDNNGradientChecks method testConvolutional.

@Test
public void testConvolutional() throws Exception {
    //Parameterized test, testing combinations of:
    // (a) activation function
    // (b) Whether to test at random initialization, or after some learning (i.e., 'characteristic mode of operation')
    // (c) Loss function (with specified output activations)
    String[] activFns = { "sigmoid", "tanh" };
    //If true: run some backprop steps first
    boolean[] characteristic = { false, true };
    int[] minibatchSizes = { 1, 4 };
    int width = 6;
    int height = 6;
    int inputDepth = 2;
    int nOut = 3;
    Field f = org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.class.getDeclaredField("helper");
    f.setAccessible(true);
    Random r = new Random(12345);
    for (String afn : activFns) {
        for (boolean doLearningFirst : characteristic) {
            for (int minibatchSize : minibatchSizes) {
                INDArray input = Nd4j.rand(new int[] { minibatchSize, inputDepth, height, width });
                INDArray labels = Nd4j.zeros(minibatchSize, nOut);
                for (int i = 0; i < minibatchSize; i++) {
                    labels.putScalar(i, r.nextInt(nOut), 1.0);
                }
                MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().regularization(false).optimizationAlgo(OptimizationAlgorithm.CONJUGATE_GRADIENT).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(-1, 1)).updater(Updater.NONE).seed(12345L).list().layer(0, new ConvolutionLayer.Builder(2, 2).stride(2, 2).padding(1, 1).nOut(3).activation(afn).build()).layer(1, new ConvolutionLayer.Builder(2, 2).stride(2, 2).padding(0, 0).nOut(3).activation(afn).build()).layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nOut(nOut).build()).setInputType(InputType.convolutional(height, width, inputDepth)).pretrain(false).backprop(true);
                MultiLayerConfiguration conf = builder.build();
                MultiLayerNetwork mln = new MultiLayerNetwork(conf);
                mln.init();
                org.deeplearning4j.nn.layers.convolution.ConvolutionLayer c0 = (org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) mln.getLayer(0);
                ConvolutionHelper ch0 = (ConvolutionHelper) f.get(c0);
                assertTrue(ch0 instanceof CudnnConvolutionHelper);
                org.deeplearning4j.nn.layers.convolution.ConvolutionLayer c1 = (org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) mln.getLayer(1);
                ConvolutionHelper ch1 = (ConvolutionHelper) f.get(c1);
                assertTrue(ch1 instanceof CudnnConvolutionHelper);
                //-------------------------------
                //For debugging/comparison to no-cudnn case: set helper field to null
                //                    f.set(c0, null);
                //                    f.set(c1, null);
                //                    assertNull(f.get(c0));
                //                    assertNull(f.get(c1));
                //-------------------------------
                String name = new Object() {
                }.getClass().getEnclosingMethod().getName();
                if (doLearningFirst) {
                    //Run a number of iterations of learning
                    mln.setInput(input);
                    mln.setLabels(labels);
                    mln.computeGradientAndScore();
                    double scoreBefore = mln.score();
                    for (int j = 0; j < 10; j++) mln.fit(input, labels);
                    mln.computeGradientAndScore();
                    double scoreAfter = mln.score();
                    //Can't test in 'characteristic mode of operation' if not learning
                    String msg = name + " - score did not (sufficiently) decrease during learning - activationFn=" + afn + ", doLearningFirst= " + doLearningFirst + " (before=" + scoreBefore + ", scoreAfter=" + scoreAfter + ")";
                    assertTrue(msg, scoreAfter < 0.8 * scoreBefore);
                }
                if (PRINT_RESULTS) {
                    System.out.println(name + " - activationFn=" + afn + ", doLearningFirst=" + doLearningFirst);
                    for (int j = 0; j < mln.getnLayers(); j++) System.out.println("Layer " + j + " # params: " + mln.getLayer(j).numParams());
                }
                boolean gradOK = GradientCheckUtil.checkGradients(mln, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);
                assertTrue(gradOK);
            }
        }
    }
}
Also used : CudnnConvolutionHelper(org.deeplearning4j.nn.layers.convolution.CudnnConvolutionHelper) ConvolutionHelper(org.deeplearning4j.nn.layers.convolution.ConvolutionHelper) Field(java.lang.reflect.Field) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) Random(java.util.Random) org.deeplearning4j.nn.conf.layers(org.deeplearning4j.nn.conf.layers) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) UniformDistribution(org.deeplearning4j.nn.conf.distribution.UniformDistribution) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) CudnnConvolutionHelper(org.deeplearning4j.nn.layers.convolution.CudnnConvolutionHelper) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 Random (java.util.Random)1 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)1 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)1 UniformDistribution (org.deeplearning4j.nn.conf.distribution.UniformDistribution)1 org.deeplearning4j.nn.conf.layers (org.deeplearning4j.nn.conf.layers)1 ConvolutionHelper (org.deeplearning4j.nn.layers.convolution.ConvolutionHelper)1 CudnnConvolutionHelper (org.deeplearning4j.nn.layers.convolution.CudnnConvolutionHelper)1 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)1 Test (org.junit.Test)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1