Transforms { public static INDArray maxPool(INDArray input,int[] ds,boolean ignoreBorder) { assert input.length() >= 2 : "Max pooling requires an ndarray of >= length 2"; assert ds.length == 2: "Down sampling must be of length 2 (the factors used for each image size"; assert input.shape().length == 4 : "Only supports 4 dimensional tensors"; int batchSize = ArrayUtil.prod(new int[]{input.size(0) * input.size(1)}); int rows = input.size(2); int cols = input.size(3); INDArray signalNDArray = input.reshape(new int[]{batchSize,1,rows,cols}); INDArray zz= Nd4j.create(signalNDArray.shape()).assign(Float.MIN_VALUE); int rowIter = ignoreBorder ? rows / (int) Math.pow(ds[0],2) : rows; int colIter = ignoreBorder ? cols / (int) Math.pow(ds[1],2) : cols; for(int i = 0; i < signalNDArray.size(0); i++) { for(int j = 0; j < signalNDArray.size(1); j++) { for(int k = 0; k < rowIter; k++) { int zk = k / ds[0]; for(int l = 0; l < colIter; l++) { int zl = l / ds[1]; double num = input.getDouble(new int[]{i, j, k, l}); double zzGet = zz.getDouble(new int[]{i, j, zk, zl}); zz.putScalar(new int[]{i,j,zk,zl},Math.max(num,zzGet)); } } } } return zz.reshape(signalNDArray.shape()); }  static INDArray maxPool(INDArray input,int[] ds,boolean ignoreBorder); static INDArray downSample(INDArray d1,int[] stride); static INDArray pool(INDArray toPool,int[] stride); static INDArray upSample(INDArray d, INDArray scale); static double cosineSim(INDArray d1, INDArray d2); static INDArray normalizeZeroMeanAndUnitVariance(INDArray toNormalize); static INDArray unitVec(INDArray toScale); static INDArray neg(INDArray ndArray); static IComplexNDArray neg(IComplexNDArray ndArray); static INDArray eq(INDArray ndArray); static IComplexNDArray eq(IComplexNDArray ndArray); static INDArray neq(INDArray ndArray); static IComplexNDArray neq(IComplexNDArray ndArray); static INDArray floor(INDArray ndArray); static INDArray sign(IComplexNDArray toSign); static INDArray sign(INDArray toSign); static IComplexNDArray floor(IComplexNDArray ndArray); static INDArray gt(INDArray ndArray); static IComplexNDArray gt(IComplexNDArray ndArray); static INDArray lt(INDArray ndArray); static IComplexNDArray lt(IComplexNDArray ndArray); static INDArray stabilize(INDArray ndArray,double k); static IComplexNDArray stabilize(IComplexNDArray ndArray,double k); static INDArray abs(INDArray ndArray); static IComplexNDArray abs(IComplexNDArray ndArray); static INDArray exp(INDArray ndArray); static IComplexNDArray exp(IComplexNDArray ndArray); static INDArray hardTanh(INDArray ndArray); static IComplexNDArray hardTanh(IComplexNDArray ndArray); static INDArray identity(INDArray ndArray); static IComplexNDArray identity(IComplexNDArray ndArray); static INDArray max(INDArray ndArray); static INDArray max(INDArray ndArray,double max); static IComplexNDArray max(IComplexNDArray ndArray,double max); static IComplexNDArray max(IComplexNDArray ndArray); static INDArray pow(INDArray ndArray,Number power); static IComplexNDArray pow(IComplexNDArray ndArray,IComplexNumber power); static INDArray round(INDArray ndArray); static IComplexNDArray round(IComplexNDArray ndArray); static INDArray sigmoid(INDArray ndArray); static IComplexNDArray sigmoid(IComplexNDArray ndArray); static INDArray sqrt(INDArray ndArray); static IComplexNDArray sqrt(IComplexNDArray ndArray); static INDArray tanh(INDArray ndArray); static IComplexNDArray tanh(IComplexNDArray ndArray); static INDArray log(INDArray ndArray); static IComplexNDArray log(IComplexNDArray ndArray); static INDArray eq(INDArray ndArray,boolean dup); static IComplexNDArray eq(IComplexNDArray ndArray,boolean dup); static INDArray neq(INDArray ndArray,boolean dup); static IComplexNDArray neq(IComplexNDArray ndArray,boolean dup); static INDArray floor(INDArray ndArray,boolean dup); static INDArray sign(IComplexNDArray toSign,boolean dup); static INDArray sign(INDArray toSign,boolean dup); static IComplexNDArray floor(IComplexNDArray ndArray,boolean dup); static INDArray gt(INDArray ndArray,boolean dup); static IComplexNDArray gt(IComplexNDArray ndArray,boolean dup); static INDArray lt(INDArray ndArray,boolean dup); static IComplexNDArray lt(IComplexNDArray ndArray,boolean dup); static INDArray stabilize(INDArray ndArray,double k,boolean dup); static IComplexNDArray stabilize(IComplexNDArray ndArray,double k,boolean dup); static INDArray abs(INDArray ndArray,boolean dup); static IComplexNDArray abs(IComplexNDArray ndArray,boolean dup); static INDArray exp(INDArray ndArray,boolean dup); static IComplexNDArray exp(IComplexNDArray ndArray,boolean dup); static INDArray hardTanh(INDArray ndArray,boolean dup); static IComplexNDArray hardTanh(IComplexNDArray ndArray,boolean dup); static INDArray identity(INDArray ndArray,boolean dup); static IComplexNDArray identity(IComplexNDArray ndArray,boolean dup); static INDArray max(INDArray ndArray,boolean dup); static INDArray max(INDArray ndArray,double max,boolean dup); static IComplexNDArray max(IComplexNDArray ndArray,double max,boolean dup); static IComplexNDArray max(IComplexNDArray ndArray,boolean dup); static INDArray pow(INDArray ndArray,Number power,boolean dup); static IComplexNDArray pow(IComplexNDArray ndArray,IComplexNumber power,boolean dup); static INDArray round(INDArray ndArray,boolean dup); static IComplexNDArray round(IComplexNDArray ndArray,boolean dup); static INDArray sigmoid(INDArray ndArray,boolean dup); static IComplexNDArray sigmoid(IComplexNDArray ndArray,boolean dup); static INDArray sqrt(INDArray ndArray,boolean dup); static IComplexNDArray sqrt(IComplexNDArray ndArray,boolean dup); static INDArray tanh(INDArray ndArray,boolean dup); static IComplexNDArray tanh(IComplexNDArray ndArray,boolean dup); static INDArray log(INDArray ndArray,boolean dup); static IComplexNDArray log(IComplexNDArray ndArray,boolean dup); static INDArray neg(INDArray ndArray,boolean dup); static IComplexNDArray neg(IComplexNDArray ndArray,boolean dup);  }
@Test public void testMaxPooling() { INDArray nd = Nd4j.rand(new int[]{1,2,3,4}); INDArray pool = Transforms.maxPool(nd,new int[]{1,2},false); pool = Transforms.maxPool(nd,new int[]{1,2},true); }