From 8e3d569f18d42504124cf47e47ccb00d7318707f Mon Sep 17 00:00:00 2001 From: Alex Black Date: Fri, 23 Aug 2019 22:50:07 +1000 Subject: [PATCH] Small fixes to subsampling layer (#158) Signed-off-by: AlexDBlack --- .../LayerHelperValidationUtil.java | 2 +- .../nn/mkldnn/ValidateMKLDNN.java | 94 ++++++++++--------- .../subsampling/SubsamplingLayer.java | 10 +- 3 files changed, 59 insertions(+), 47 deletions(-) diff --git a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/LayerHelperValidationUtil.java b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/LayerHelperValidationUtil.java index 2322fd3d3..59ef8c28e 100644 --- a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/LayerHelperValidationUtil.java +++ b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/LayerHelperValidationUtil.java @@ -190,7 +190,7 @@ public class LayerHelperValidationUtil { } else { System.out.println("OK: " + p); } - assertTrue("Gradients are not equal: " + p + " - highest relative error = " + maxRE + " > max relative error = " + t.getMaxRelError(), + assertTrue(t.getTestName() + " - Gradients are not equal: " + p + " - highest relative error = " + maxRE + " > max relative error = " + t.getMaxRelError(), maxRE < t.getMaxRelError()); } } diff --git a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/mkldnn/ValidateMKLDNN.java b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/mkldnn/ValidateMKLDNN.java index 5a0d158b6..a04553c37 100644 --- a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/mkldnn/ValidateMKLDNN.java +++ b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/mkldnn/ValidateMKLDNN.java @@ -57,56 +57,62 @@ public class ValidateMKLDNN extends BaseDL4JTest { for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Same, ConvolutionMode.Truncate}) { for (int[] kernel : new int[][]{{2, 2}, {2, 3}}) { for (int[] stride : new int[][]{{1, 1}, {2, 2}}) { + for (PoolingType pt : new PoolingType[]{PoolingType.MAX, PoolingType.AVG}) { - inputSize[0] = minibatch; - INDArray f = Nd4j.rand(DataType.FLOAT, inputSize); - INDArray l = TestUtils.randomOneHot(minibatch, 10).castTo(DataType.FLOAT); + inputSize[0] = minibatch; + INDArray f = Nd4j.rand(DataType.FLOAT, inputSize); + INDArray l = TestUtils.randomOneHot(minibatch, 10).castTo(DataType.FLOAT); - MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() - .updater(new Adam(0.01)) - .convolutionMode(cm) - .seed(12345) - .list() - .layer(new ConvolutionLayer.Builder().activation(Activation.TANH) - .kernelSize(kernel) - .stride(stride) - .padding(0,0) - .nOut(3) - .build()) - .layer(new SubsamplingLayer.Builder() - .kernelSize(kernel) - .stride(stride) - .padding(0,0) - .build()) - .layer(new ConvolutionLayer.Builder().activation(Activation.TANH) - .kernelSize(kernel) - .stride(stride) - .padding(0,0) - .nOut(3) - .build()) - .layer(new OutputLayer.Builder().nOut(10).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build()) - .setInputType(InputType.convolutional(inputSize[2], inputSize[3], inputSize[1])) - .build(); + MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() + .updater(new Adam(0.01)) + .convolutionMode(cm) + .seed(12345) + .list() + .layer(new ConvolutionLayer.Builder().activation(Activation.TANH) + .kernelSize(kernel) + .stride(stride) + .padding(0, 0) + .nOut(3) + .build()) + .layer(new SubsamplingLayer.Builder() + .poolingType(pt) + .kernelSize(kernel) + .stride(stride) + .padding(0, 0) + .build()) + .layer(new ConvolutionLayer.Builder().activation(Activation.TANH) + .kernelSize(kernel) + .stride(stride) + .padding(0, 0) + .nOut(3) + .build()) + .layer(new OutputLayer.Builder().nOut(10).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build()) + .setInputType(InputType.convolutional(inputSize[2], inputSize[3], inputSize[1])) + .build(); - MultiLayerNetwork netWith = new MultiLayerNetwork(conf.clone()); - netWith.init(); + MultiLayerNetwork netWith = new MultiLayerNetwork(conf.clone()); + netWith.init(); - MultiLayerNetwork netWithout = new MultiLayerNetwork(conf.clone()); - netWithout.init(); + MultiLayerNetwork netWithout = new MultiLayerNetwork(conf.clone()); + netWithout.init(); - LayerHelperValidationUtil.TestCase tc = LayerHelperValidationUtil.TestCase.builder() - .allowHelpersForClasses(Arrays.>asList(org.deeplearning4j.nn.layers.convolution.subsampling.SubsamplingLayer.class, - org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.class)) - .testForward(true) - .testScore(true) - .testBackward(true) - .testTraining(true) - .features(f) - .labels(l) - .data(new SingletonDataSetIterator(new DataSet(f,l))) - .build(); + String name = pt + ", mb=" + minibatch + ", cm=" + cm + ", kernel=" + Arrays.toString(kernel) + ", stride=" + Arrays.toString(stride); + LayerHelperValidationUtil.TestCase tc = LayerHelperValidationUtil.TestCase.builder() + .testName(name) + .allowHelpersForClasses(Arrays.>asList(org.deeplearning4j.nn.layers.convolution.subsampling.SubsamplingLayer.class, + org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.class)) + .testForward(true) + .testScore(true) + .testBackward(true) + .testTraining(true) + .features(f) + .labels(l) + .data(new SingletonDataSetIterator(new DataSet(f, l))) + .build(); - LayerHelperValidationUtil.validateMLN(netWith, tc); + System.out.println("Starting test: " + name); + LayerHelperValidationUtil.validateMLN(netWith, tc); + } } } } diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java index 494d9c374..5f54cf8e0 100644 --- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java +++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java @@ -289,8 +289,14 @@ public class SubsamplingLayer extends AbstractLayer