use of org.nd4j.linalg.api.ops.DynamicCustomOp in project nd4j by deeplearning4j.
the class ConvolutionTestsC method testMaxPoolBackprop.
@Test
@Ignore
public void testMaxPoolBackprop() {
Nd4j.getRandom().setSeed(12345);
for (int i = 0; i < 5; i++) {
int[] inputShape = { 1, 1, 4, 3 };
int[] kernel = { 2, 2 };
int[] strides = { 1, 1 };
int[] pad = { 0, 0 };
// TODO non 1-1 dilation
int[] dilation = { 1, 1 };
boolean same = true;
String fn = "maxpool2d_bp";
int nIArgs = 11;
int[] a = new int[nIArgs];
a[0] = kernel[0];
a[1] = kernel[1];
a[2] = strides[0];
a[3] = strides[1];
a[4] = pad[0];
a[5] = pad[1];
a[6] = dilation[0];
a[7] = dilation[1];
a[8] = same ? 1 : 0;
// a[9]: Not used with max pooling
// For NCHW
a[10] = 0;
List<Pair<INDArray, String>> inputs = NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, inputShape);
for (Pair<INDArray, String> pIn : inputs) {
INDArray input = pIn.getFirst();
int[] outShapeHW = getOutputSize(input, kernel, strides, pad, same);
List<Pair<INDArray, String>> eps = NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, inputShape[0], inputShape[1], outShapeHW[0], outShapeHW[1]);
for (Pair<INDArray, String> pEps : eps) {
INDArray epsilon = pEps.getFirst();
INDArray epsNext = Nd4j.create(inputShape, 'c');
// Runs fine with dups:
// input = input.dup('c');
epsilon = epsilon.dup('c');
DynamicCustomOp op = DynamicCustomOp.builder(fn).addInputs(input, epsilon).addOutputs(epsNext).addIntegerArguments(a).build();
Nd4j.getExecutioner().exec(op);
INDArray expEpsNext = expGradMaxPoolBackPropSame(input, epsilon, kernel, strides, same);
String msg = "input=" + pIn.getSecond() + ", eps=" + pEps.getSecond();
assertEquals(msg, expEpsNext, epsNext);
}
}
}
}
Aggregations