@Test public void testConvolutionalGradient() { final LayerParameter convolutionalLayerParams = convolutionalLayer.generateParameterGradient(input, nextError); assertTrue(compare(expectedConvolutionalLayerParams, convolutionalLayerParams, TOLERANCE)); }
@Override public LayerParameter generateParameterGradient(final Matrix input, final Matrix error) { final Matrix weightGradient = matrixFactory.create(kernelHeight * kernelWidth * inputChannel, outputShape[0]); for (int n = 0; n < input.getColumns(); ++n) { final Matrix row = im2row(n, input); weightGradient.addi(row.transpose().mmul(error.getColumn(n) .reshape(outputShape[1] * outputShape[2], outputShape[0]))); } final Matrix biasGradient = error.rowSums(); return LayerParameter.newBuilder() .setWeightParam(weightGradient) .setBiasParam(biasGradient) .build(); }
ConvolutionalLayer extends LayerBase { @Override public LayerParameter generateParameterGradient(final Matrix input, final Matrix error) { final Matrix weightGradient = matrixFactory.create(kernelHeight * kernelWidth * inputChannel, outputShape[0]); for (int n = 0; n < input.getColumns(); ++n) { final Matrix row = im2row(n, input); weightGradient.addi(row.transpose().mmul(error.getColumn(n) .reshape(outputShape[1] * outputShape[2], outputShape[0]))); } final Matrix biasGradient = error.rowSums(); return LayerParameter.newBuilder() .setWeightParam(weightGradient) .setBiasParam(biasGradient) .build(); } }
ConvolutionalLayer extends LayerBase { @Override public LayerParameter generateParameterGradient(final Matrix input, final Matrix error) { final Matrix weightGradient = matrixFactory.create(kernelHeight * kernelWidth * inputChannel, outputShape[0]); for (int n = 0; n < input.getColumns(); ++n) { final Matrix row = im2row(n, input); weightGradient.addi(row.transpose().mmul(error.getColumn(n) .reshape(outputShape[1] * outputShape[2], outputShape[0]))); } final Matrix biasGradient = error.rowSums(); return LayerParameter.newBuilder() .setWeightParam(weightGradient) .setBiasParam(biasGradient) .build(); } @Inject private  ConvolutionalLayer(@Parameter(LayerIndex.class) final int index,
                             @Parameter(LayerInputShape.class) final String inputShape,
                             @Parameter(PaddingHeight.class) final int paddingHeight,
                             @Parameter(PaddingWidth.class) final int paddingWidth,
                             @Parameter(StrideHeight.class) final int strideHeight,
                             @Parameter(StrideWidth.class) final int strideWidth,
                             @Parameter(KernelHeight.class) final int kernelHeight,
                             @Parameter(KernelWidth.class) final int kernelWidth,
                             final LayerParameterInitializer layerParameterInitializer,
                             final MatrixFactory matrixFactory); }
ConvolutionalLayer extends LayerBase { @Override public LayerParameter generateParameterGradient(final Matrix input, final Matrix error) { final Matrix weightGradient = matrixFactory.create(kernelHeight * kernelWidth * inputChannel, outputShape[0]); for (int n = 0; n < input.getColumns(); ++n) { final Matrix row = im2row(n, input); weightGradient.addi(row.transpose().mmul(error.getColumn(n) .reshape(outputShape[1] * outputShape[2], outputShape[0]))); } final Matrix biasGradient = error.rowSums(); return LayerParameter.newBuilder() .setWeightParam(weightGradient) .setBiasParam(biasGradient) .build(); } @Inject private  ConvolutionalLayer(@Parameter(LayerIndex.class) final int index,
                             @Parameter(LayerInputShape.class) final String inputShape,
                             @Parameter(PaddingHeight.class) final int paddingHeight,
                             @Parameter(PaddingWidth.class) final int paddingWidth,
                             @Parameter(StrideHeight.class) final int strideHeight,
                             @Parameter(StrideWidth.class) final int strideWidth,
                             @Parameter(KernelHeight.class) final int kernelHeight,
                             @Parameter(KernelWidth.class) final int kernelWidth,
                             final LayerParameterInitializer layerParameterInitializer,
                             final MatrixFactory matrixFactory); @Override int[] getOutputShape(); @Override boolean isLearnable(); @Override Matrix feedForward(final Matrix input); @Override Matrix backPropagate(final Matrix input, final Matrix activation, final Matrix nextError); @Override LayerParameter generateParameterGradient(final Matrix input, final Matrix error); }
ConvolutionalLayer extends LayerBase { @Override public LayerParameter generateParameterGradient(final Matrix input, final Matrix error) { final Matrix weightGradient = matrixFactory.create(kernelHeight * kernelWidth * inputChannel, outputShape[0]); for (int n = 0; n < input.getColumns(); ++n) { final Matrix row = im2row(n, input); weightGradient.addi(row.transpose().mmul(error.getColumn(n) .reshape(outputShape[1] * outputShape[2], outputShape[0]))); } final Matrix biasGradient = error.rowSums(); return LayerParameter.newBuilder() .setWeightParam(weightGradient) .setBiasParam(biasGradient) .build(); } @Inject private  ConvolutionalLayer(@Parameter(LayerIndex.class) final int index,
                             @Parameter(LayerInputShape.class) final String inputShape,
                             @Parameter(PaddingHeight.class) final int paddingHeight,
                             @Parameter(PaddingWidth.class) final int paddingWidth,
                             @Parameter(StrideHeight.class) final int strideHeight,
                             @Parameter(StrideWidth.class) final int strideWidth,
                             @Parameter(KernelHeight.class) final int kernelHeight,
                             @Parameter(KernelWidth.class) final int kernelWidth,
                             final LayerParameterInitializer layerParameterInitializer,
                             final MatrixFactory matrixFactory); @Override int[] getOutputShape(); @Override boolean isLearnable(); @Override Matrix feedForward(final Matrix input); @Override Matrix backPropagate(final Matrix input, final Matrix activation, final Matrix nextError); @Override LayerParameter generateParameterGradient(final Matrix input, final Matrix error);  }