Search in sources :

Example 1 with CudnnLocalResponseNormalizationHelper

use of org.deeplearning4j.nn.layers.normalization.CudnnLocalResponseNormalizationHelper in project deeplearning4j by deeplearning4j.

the class CuDNNGradientChecks method testLRN.

@Test
public void testLRN() throws Exception {
    Nd4j.getRandom().setSeed(12345);
    int minibatch = 10;
    int depth = 6;
    int hw = 5;
    int nOut = 4;
    INDArray input = Nd4j.rand(new int[] { minibatch, depth, hw, hw });
    INDArray labels = Nd4j.zeros(minibatch, nOut);
    Random r = new Random(12345);
    for (int i = 0; i < minibatch; i++) {
        labels.putScalar(i, r.nextInt(nOut), 1.0);
    }
    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().learningRate(1.0).regularization(false).updater(Updater.NONE).seed(12345L).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 2)).list().layer(0, new ConvolutionLayer.Builder().nOut(6).kernelSize(2, 2).stride(1, 1).activation(Activation.TANH).build()).layer(1, new LocalResponseNormalization.Builder().build()).layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nOut(nOut).build()).setInputType(InputType.convolutional(hw, hw, depth)).pretrain(false).backprop(true);
    MultiLayerNetwork mln = new MultiLayerNetwork(builder.build());
    mln.init();
    Field f = org.deeplearning4j.nn.layers.normalization.LocalResponseNormalization.class.getDeclaredField("helper");
    f.setAccessible(true);
    org.deeplearning4j.nn.layers.normalization.LocalResponseNormalization l = (org.deeplearning4j.nn.layers.normalization.LocalResponseNormalization) mln.getLayer(1);
    LocalResponseNormalizationHelper lrn = (LocalResponseNormalizationHelper) f.get(l);
    assertTrue(lrn instanceof CudnnLocalResponseNormalizationHelper);
    if (PRINT_RESULTS) {
        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 : CudnnLocalResponseNormalizationHelper(org.deeplearning4j.nn.layers.normalization.CudnnLocalResponseNormalizationHelper) LocalResponseNormalizationHelper(org.deeplearning4j.nn.layers.normalization.LocalResponseNormalizationHelper) Field(java.lang.reflect.Field) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) Random(java.util.Random) CudnnLocalResponseNormalizationHelper(org.deeplearning4j.nn.layers.normalization.CudnnLocalResponseNormalizationHelper) org.deeplearning4j.nn.conf.layers(org.deeplearning4j.nn.conf.layers) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) 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 NormalDistribution (org.deeplearning4j.nn.conf.distribution.NormalDistribution)1 org.deeplearning4j.nn.conf.layers (org.deeplearning4j.nn.conf.layers)1 CudnnLocalResponseNormalizationHelper (org.deeplearning4j.nn.layers.normalization.CudnnLocalResponseNormalizationHelper)1 LocalResponseNormalizationHelper (org.deeplearning4j.nn.layers.normalization.LocalResponseNormalizationHelper)1 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)1 Test (org.junit.Test)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1