Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#transpose()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#transpose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testTranspose() {
    INDArray n = Nd4j.create(Nd4j.ones(100).castTo(DataType.DOUBLE).data(), new long[] {5, 5, 4});
    INDArray transpose = n.transpose();
    assertEquals(n.length(), transpose.length());
    assertEquals(true, Arrays.equals(new long[] {4, 5, 5}, transpose.shape()));

    INDArray rowVector = Nd4j.linspace(1, 10, 10, DataType.DOUBLE).reshape(1, -1);
    assertTrue(rowVector.isRowVector());
    INDArray columnVector = rowVector.transpose();
    assertTrue(columnVector.isColumnVector());


    INDArray linspaced = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    INDArray transposed = Nd4j.create(new double[] {1, 3, 2, 4}, new long[] {2, 2});
    assertEquals(transposed, linspaced.transpose());

    linspaced = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    //fortran ordered
    INDArray transposed2 = Nd4j.create(new double[] {1, 3, 2, 4}, new long[] {2, 2});
    transposed = linspaced.transpose();
    assertEquals(transposed, transposed2);


}
 
Example 2
Source File: PLNetDyadRanker.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
private INDArray computeScaledGradient(final INDArray dyadMatrix) {
	int dyadRankingLength = dyadMatrix.rows();
	List<INDArray> activations = this.plNet.feedForward(dyadMatrix);
	INDArray output = activations.get(activations.size() - 1);
	output = output.transpose();
	INDArray deltaW = Nd4j.zeros(this.plNet.params().length());
	Gradient deltaWk = null;
	MultiLayerNetwork plNetClone = this.plNet.clone();
	for (int k = 0; k < dyadRankingLength; k++) {
		// compute derivative of loss w.r.t. k
		plNetClone.setInput(dyadMatrix.getRow(k));
		plNetClone.feedForward(true, false);
		INDArray lossGradient = PLNetLoss.computeLossGradient(output, k);
		// compute backprop gradient for weight updates w.r.t. k
		Pair<Gradient, INDArray> p = plNetClone.backpropGradient(lossGradient, null);
		deltaWk = p.getFirst();
		this.plNet.getUpdater().update(this.plNet, deltaWk, this.iteration, this.epoch, 1, LayerWorkspaceMgr.noWorkspaces());
		deltaW.addi(deltaWk.gradient());
	}

	return deltaW;
}
 
Example 3
Source File: LoneTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexingColVec() {
    int elements = 5;
    INDArray rowVector = Nd4j.linspace(1, elements, elements).reshape(1, elements);
    INDArray colVector = rowVector.transpose();
    int j;
    INDArray jj;
    for (int i = 0; i < elements; i++) {
        j = i + 1;
        assertEquals(i + 1,colVector.getRow(i).getInt(0));
        assertEquals(i + 1,rowVector.getColumn(i).getInt(0));
        assertEquals(i + 1,rowVector.get(NDArrayIndex.interval(i, j)).getInt(0));
        assertEquals(i + 1,colVector.get(NDArrayIndex.interval(i, j)).getInt(0));
        System.out.println("Making sure index interval will not crash with begin/end vals...");
        jj = colVector.get(NDArrayIndex.interval(i, i + 10));
        jj = colVector.get(NDArrayIndex.interval(i, i + 10));
    }
}
 
Example 4
Source File: Nd4jMatrix.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
// TODO 准备与dotProduct整合
public MathMatrix accumulateProduct(MathVector rowVector, MathVector columnVector, MathCalculator mode) {
    if (rowVector instanceof Nd4jVector && columnVector instanceof Nd4jVector) {
        Nd4jEnvironmentThread thread = EnvironmentThread.getThread(Nd4jEnvironmentThread.class);
        try (MemoryWorkspace workspace = thread.getSpace()) {
            INDArray leftArray = Nd4jVector.class.cast(rowVector).getArray();
            // TODO 此处需要想方案优化,否则存在性能问题.
            if (leftArray.isView()) {
                // 此处执行复制是由于gemm不支持视图向量.
                leftArray = leftArray.dup();
            }
            if (leftArray.rows() == 1) {
                leftArray = leftArray.transpose();
            }
            INDArray rightArray = Nd4jVector.class.cast(columnVector).getArray();
            if (rightArray.isView()) {
                // 此处执行复制是由于gemm不支持视图向量.
                rightArray = rightArray.dup();
            }
            if (rightArray.columns() == 1) {
                rightArray = rightArray.transpose();
            }
            INDArray dataArray = this.getArray();
            INDArray cacheArray = Nd4j.zeros(dataArray.shape(), dataArray.ordering());
            leftArray.mmul(rightArray, cacheArray);
            dataArray.addi(cacheArray);
            return this;
        }
    } else {
        return MathMatrix.super.accumulateProduct(rowVector, columnVector, mode);
    }
}
 
Example 5
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMmulF() {

    DataBuffer data = Nd4j.linspace(1, 10, 10, DataType.DOUBLE).data();
    INDArray n = Nd4j.create(data, new long[] {1, 10});
    INDArray transposed = n.transpose();
    assertEquals(true, n.isRowVector());
    assertEquals(true, transposed.isColumnVector());


    INDArray innerProduct = n.mmul(transposed);

    INDArray scalar = Nd4j.scalar(385.0).reshape(1,1);
    assertEquals(getFailureMessage(), scalar, innerProduct);
}
 
Example 6
Source File: Nd4jVector.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
// TODO 准备与dotProduct整合
public MathVector accumulateProduct(MathVector leftVector, MathMatrix rightMatrix, boolean transpose, MathCalculator mode) {
    if (leftVector instanceof Nd4jVector && rightMatrix instanceof Nd4jMatrix) {
        Nd4jEnvironmentThread thread = EnvironmentThread.getThread(Nd4jEnvironmentThread.class);
        try (MemoryWorkspace workspace = thread.getSpace()) {
            INDArray leftArray = Nd4jVector.class.cast(leftVector).getArray();
            if (leftArray.isView()) {
                // 此处执行复制是由于gemm不支持视图向量.
                leftArray = leftArray.dup();
            }
            if (leftArray.columns() == 1) {
                leftArray = leftArray.transpose();
            }
            INDArray rightArray = transpose ? Nd4jMatrix.class.cast(rightMatrix).getArray().transpose() : Nd4jMatrix.class.cast(rightMatrix).getArray();
            INDArray dataArray = this.getArray();
            INDArray cacheArray = Nd4j.zeros(dataArray.shape(), dataArray.ordering());
            leftArray.mmul(rightArray, cacheArray);
            dataArray.addi(cacheArray);
            // Nd4j.getBlasWrapper().level3().gemm(leftArray, rightArray, dataArray, false,
            // false, one, zero);
            return this;
        }
    } else {
        return MathVector.super.accumulateProduct(leftVector, rightMatrix, transpose, mode);
    }
}
 
Example 7
Source File: MtcnnService.java    From mtcnn-java with Apache License 2.0 5 votes vote down vote up
/**
 * Detects faces in an image, and returns bounding boxes and points for them.
 * @param image3HW image to detect the faces in. Expected dimensions [ 3 x H x W ]
 * @return Array of face bounding boxes found in the image
 */
public FaceAnnotation[] faceDetection(INDArray image3HW) throws IOException {

	INDArray[] outputStageResult = this.rawFaceDetection(image3HW);

	// Convert result into Bounding Box array
	INDArray totalBoxes = outputStageResult[0];
	INDArray points = outputStageResult[1];
	//if (!totalBoxes.isEmpty() && totalBoxes.size(0) > 1) { // 1.0.0-beta2
	if (!totalBoxes.isEmpty() && totalBoxes.size(0) > 0) { // 1.0.0-SNAPSHOT
		points = points.transpose();
	}

	return MtcnnUtil.toFaceAnnotation(totalBoxes, points);
}
 
Example 8
Source File: F1Optimizer.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This evaluates F1
 *
 * @param rrt
 * @param u
 * @param x
 * @return
 */
public double getCost(final INDArray u) {
	INDArray z1 = this.x.mmul(u);
	INDArray z2 = z1.transpose();
	INDArray z = z1.mmul(z2);
	INDArray q = this.rrt.sub(z);
	double cost = 0;
	int n = q.columns();
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cost += Math.pow(q.getDouble(i,j),2);
		}
	}
	return cost;
}
 
Example 9
Source File: PCA.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates pca factors of a matrix, for a flags number of reduced features
 * returns the factors to scale observations 
 *
 * The return is a factor matrix to reduce (normalized) feature sets
 *
 * @see pca(INDArray, int, boolean)
 *
 * @param A the array of features, rows are results, columns are features - will be changed
 * @param nDims the number of components on which to project the features 
 * @param normalize whether to normalize (adjust each feature to have zero mean)
 * @return the reduced feature set
 */
public static INDArray pca_factor(INDArray A, int nDims, boolean normalize) {

    if (normalize) {
        // Normalize to mean 0 for each feature ( each column has 0 mean )
        INDArray mean = A.mean(0);
        A.subiRowVector(mean);
    }

    long m = A.rows();
    long n = A.columns();

    // The prepare SVD results, we'll decomp A to UxSxV'
    INDArray s = Nd4j.create(A.dataType(), m < n ? m : n);
    INDArray VT = Nd4j.create(A.dataType(), new long[]{n, n}, 'f');

    // Note - we don't care about U 
    Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);

    // for comparison k & nDims are the equivalent values in both methods implementing PCA

    // So now let's rip out the appropriate number of left singular vectors from
    // the V output (note we pulls rows since VT is a transpose of V)
    INDArray V = VT.transpose();
    INDArray factor = Nd4j.create(A.dataType(),new long[]{n, nDims}, 'f');
    for (int i = 0; i < nDims; i++) {
        factor.putColumn(i, V.getColumn(i));
    }

    return factor;
}
 
Example 10
Source File: BaseNDArrayFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Rotate a matrix 90 degrees
 *
 * @param toRotate the matrix to rotate
 * @return the rotated matrix
 */
@Override
public void rot90(INDArray toRotate) {
    if (!toRotate.isMatrix())
        throw new IllegalArgumentException("Only rotating matrices");

    INDArray start = toRotate.transpose();
    for (int i = 0; i < start.rows(); i++)
        start.putRow(i, reverse(start.getRow(i)));

}
 
Example 11
Source File: PCA.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates pca factors of a matrix, for a flags number of reduced features
 * returns the factors to scale observations 
 *
 * The return is a factor matrix to reduce (normalized) feature sets
 *
 * @see pca(INDArray, int, boolean)
 *
 * @param A the array of features, rows are results, columns are features - will be changed
 * @param nDims the number of components on which to project the features 
 * @param normalize whether to normalize (adjust each feature to have zero mean)
 * @return the reduced feature set
 */
public static INDArray pca_factor(INDArray A, int nDims, boolean normalize) {

    if (normalize) {
        // Normalize to mean 0 for each feature ( each column has 0 mean )
        INDArray mean = A.mean(0);
        A.subiRowVector(mean);
    }

    long m = A.rows();
    long n = A.columns();

    // The prepare SVD results, we'll decomp A to UxSxV'
    INDArray s = Nd4j.create(m < n ? m : n);
    INDArray VT = Nd4j.create(n, n, 'f');

    // Note - we don't care about U 
    Nd4j.getBlasWrapper().lapack().gesvd(A, s, null, VT);

    // for comparison k & nDims are the equivalent values in both methods implementing PCA

    // So now let's rip out the appropriate number of left singular vectors from
    // the V output (note we pulls rows since VT is a transpose of V)
    INDArray V = VT.transpose();
    INDArray factor = Nd4j.create(n, nDims, 'f');
    for (int i = 0; i < nDims; i++) {
        factor.putColumn(i, V.getColumn(i));
    }

    return factor;
}
 
Example 12
Source File: BaseNDArrayFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Rotate a matrix 90 degrees
 *
 * @param toRotate the matrix to rotate
 * @return the rotated matrix
 */
@Override
public void rot90(INDArray toRotate) {
    if (!toRotate.isMatrix())
        throw new IllegalArgumentException("Only rotating matrices");

    INDArray start = toRotate.transpose();
    for (int i = 0; i < start.rows(); i++)
        start.putRow(i, reverse(start.getRow(i)));

}
 
Example 13
Source File: GemmParams.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public GemmParams(INDArray a, INDArray b, INDArray c, boolean transposeA, boolean transposeB) {
    this(transposeA ? a.transpose() : a, transposeB ? b.transpose() : b, c);
}
 
Example 14
Source File: CheckUtil.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public static boolean checkGemm(INDArray a, INDArray b, INDArray c, boolean transposeA, boolean transposeB,
                double alpha, double beta, double maxRelativeDifference, double minAbsDifference) {
    long commonDimA = (transposeA ? a.rows() : a.columns());
    long commonDimB = (transposeB ? b.columns() : b.rows());
    if (commonDimA != commonDimB)
        throw new IllegalArgumentException("Common dimensions don't match: a.shape=" + Arrays.toString(a.shape())
                        + ", b.shape=" + Arrays.toString(b.shape()) + ", tA=" + transposeA + ", tb=" + transposeB);
    long outRows = (transposeA ? a.columns() : a.rows());
    long outCols = (transposeB ? b.rows() : b.columns());
    if (c.rows() != outRows || c.columns() != outCols)
        throw new IllegalArgumentException("C does not match outRows or outCols");
    if (c.offset() != 0 || c.ordering() != 'f')
        throw new IllegalArgumentException("Invalid c");

    INDArray aConvert = transposeA ? a.transpose() : a;
    RealMatrix rmA = convertToApacheMatrix(aConvert);
    INDArray bConvet = transposeB ? b.transpose() : b;
    RealMatrix rmB = convertToApacheMatrix(bConvet);
    RealMatrix rmC = convertToApacheMatrix(c);
    RealMatrix rmExpected = rmA.scalarMultiply(alpha).multiply(rmB).add(rmC.scalarMultiply(beta));
    INDArray cCopy1 = Nd4j.create(c.shape(), 'f');
    cCopy1.assign(c);
    INDArray cCopy2 = Nd4j.create(c.shape(), 'f');
    cCopy2.assign(c);

    INDArray out = Nd4j.gemm(a, b, c, transposeA, transposeB, alpha, beta);
    if (out != c) {
        System.out.println("Returned different array than c");
        return false;
    }
    if (!checkShape(rmExpected, out))
        return false;
    boolean ok = checkEntries(rmExpected, out, maxRelativeDifference, minAbsDifference);
    if (!ok) {
        INDArray aCopy = Shape.toOffsetZeroCopy(a);
        INDArray bCopy = Shape.toOffsetZeroCopy(b);
        INDArray onCopies = Nd4j.gemm(aCopy, bCopy, cCopy1, transposeA, transposeB, alpha, beta);
        printGemmFailureDetails(a, b, cCopy2, transposeA, transposeB, alpha, beta, rmExpected, out, onCopies);
    }
    return ok;
}
 
Example 15
Source File: NDArrayCreationUtil.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public static Pair<INDArray, String> getTransposedMatrixWithShape(char ordering, int rows, int cols, int seed) {
    Nd4j.getRandom().setSeed(seed);
    INDArray out = Nd4j.linspace(1, rows * cols, rows * cols).reshape(ordering, cols, rows);
    return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")");
}
 
Example 16
Source File: VariationalAutoencoder.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr) {
    assertInputSet(true);
    if (!zeroedPretrainParamGradients) {
        for (Map.Entry<String, INDArray> entry : gradientViews.entrySet()) {
            if (isPretrainParam(entry.getKey())) {
                entry.getValue().assign(0);
            }
        }
        zeroedPretrainParamGradients = true;
    }

    INDArray input = this.input.castTo(dataType);

    Gradient gradient = new DefaultGradient();

    VAEFwdHelper fwd = doForward(true, true, workspaceMgr);
    INDArray currentDelta = pzxActivationFn.backprop(fwd.pzxMeanPreOut, epsilon).getFirst();

    //Finally, calculate mean value:
    INDArray meanW = getParamWithNoise(VariationalAutoencoderParamInitializer.PZX_MEAN_W, true, workspaceMgr);
    INDArray dLdMeanW = gradientViews.get(VariationalAutoencoderParamInitializer.PZX_MEAN_W); //f order
    INDArray lastEncoderActivation = fwd.encoderActivations[fwd.encoderActivations.length - 1];
    Nd4j.gemm(lastEncoderActivation, currentDelta, dLdMeanW, true, false, 1.0, 0.0);
    INDArray dLdMeanB = gradientViews.get(VariationalAutoencoderParamInitializer.PZX_MEAN_B);
    currentDelta.sum(dLdMeanB, 0); //dLdMeanB is initialized/zeroed first in sum op

    gradient.gradientForVariable().put(VariationalAutoencoderParamInitializer.PZX_MEAN_W, dLdMeanW);
    gradient.gradientForVariable().put(VariationalAutoencoderParamInitializer.PZX_MEAN_B, dLdMeanB);

    epsilon = meanW.mmul(currentDelta.transpose()).transpose();

    int nEncoderLayers = encoderLayerSizes.length;

    IActivation afn = layerConf().getActivationFn();
    for (int i = nEncoderLayers - 1; i >= 0; i--) {
        String wKey = "e" + i + WEIGHT_KEY_SUFFIX;
        String bKey = "e" + i + BIAS_KEY_SUFFIX;

        INDArray weights = getParamWithNoise(wKey, true, workspaceMgr);

        INDArray dLdW = gradientViews.get(wKey);
        INDArray dLdB = gradientViews.get(bKey);

        INDArray preOut = fwd.encoderPreOuts[i];

        currentDelta = afn.backprop(preOut, epsilon).getFirst();

        INDArray actInput;
        if (i == 0) {
            actInput = input;
        } else {
            actInput = fwd.encoderActivations[i - 1];
        }
        Nd4j.gemm(actInput, currentDelta, dLdW, true, false, 1.0, 0.0);
        currentDelta.sum(dLdB, 0); //dLdB is initialized/zeroed first in sum op

        gradient.gradientForVariable().put(wKey, dLdW);
        gradient.gradientForVariable().put(bKey, dLdB);

        if(i == 0) {
            epsilon = workspaceMgr.createUninitialized(ArrayType.ACTIVATION_GRAD, currentDelta.dataType(), new long[]{weights.size(0), currentDelta.size(0)}, 'f');
            weights.mmuli(currentDelta.transpose(), epsilon);
            epsilon = epsilon.transpose();
        } else {
            epsilon = weights.mmul(currentDelta.transpose()).transpose();
        }
    }

    return new Pair<>(gradient, epsilon);
}
 
Example 17
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMmulGradients(){
    int[] aShape = new int[]{2,3};
    int[] bShape = new int[]{3,4};
    List<String> failed = new ArrayList<>();

    for( char aOrder : new char[]{'c', 'f'}) {
        for (char bOrder : new char[]{'c', 'f'}) {
            for (boolean transposeA : new boolean[]{false, true}) {
                for (boolean transposeB : new boolean[]{false, true}) {
                    for (boolean transposeResult : new boolean[]{false, true}) {    //https://github.com/deeplearning4j/deeplearning4j/issues/5648
                        Nd4j.getRandom().setSeed(12345);

                        INDArray aArr = Nd4j.rand(DataType.DOUBLE, t(transposeA, aShape)).dup(aOrder);
                        INDArray bArr = Nd4j.rand(DataType.DOUBLE, t(transposeB, bShape)).dup(bOrder);

                        SameDiff sd = SameDiff.create();
                        SDVariable a = sd.var("a", aArr);
                        SDVariable b = sd.var("b", bArr);

                        SDVariable mmul = sd.mmul(a, b, transposeA, transposeB, transposeResult);

                        INDArray exp = (transposeA ? aArr.transpose() : aArr);
                        exp = exp.mmul(transposeB ? bArr.transpose() : bArr);
                        exp = (transposeResult ? exp.transpose() : exp);

                        SDVariable loss = mmul.std(true);

                        String name = aOrder + "," + bOrder + ",tA=" + transposeA + ",tB=" + transposeB +
                                ",tRes=" + transposeResult;
                        TestCase tc = new TestCase(sd).testName(name)
                                .expected(mmul, exp);

                        String err = OpValidation.validate(tc, true);
                        if(err != null)
                            failed.add(err);
                    }
                }
            }
        }
    }

    assertEquals(failed.toString(), 0, failed.size());
}
 
Example 18
Source File: NDArrayCreationUtil.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public static Pair<INDArray, String> getTransposedMatrixWithShape(long rows, long cols, long seed) {
    Nd4j.getRandom().setSeed(seed);
    INDArray out = Nd4j.linspace(1, rows * cols, rows * cols).reshape(cols, rows);
    return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")");
}
 
Example 19
Source File: NDArrayCreationUtil.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static Pair<INDArray, String> getTransposedMatrixWithShape(char ordering, int rows, int cols, int seed, DataType dataType) {
    Nd4j.getRandom().setSeed(seed);
    INDArray out = Nd4j.linspace(1, rows * cols, rows * cols, dataType).reshape(ordering, cols, rows);
    return new Pair<>(out.transpose(), "getTransposedMatrixWithShape(" + rows + "," + cols + "," + seed + ")");
}
 
Example 20
Source File: MtcnnUtil.java    From mtcnn-java with Apache License 2.0 4 votes vote down vote up
/**
 * Use heatmap to generate bounding boxes.
 *
 * original code:
 *  - https://github.com/kpzhang93/MTCNN_face_detection_alignment/blob/master/code/codes/MTCNNv2/generateBoundingBox.m
 *  - https://github.com/davidsandberg/facenet/blob/master/src/align/detect_face.py#L660
 *
 * @param imap
 * @param reg
 * @param scale
 * @param stepThreshold
 * @return Returns the generated bboxes
 */
public static INDArray[] generateBoundingBox(INDArray imap, INDArray reg, double scale, double stepThreshold) {

	int stride = 2;
	int cellSize = 12;

	// imap = np.transpose(imap)
	// y, x = np.where(imap >= t)
	// imap = imap.transpose();
	INDArray bb = MtcnnUtil.getIndexWhereMatrix(imap, v -> v >= stepThreshold);
	//INDArray bb = MtcnnUtil.getIndexWhere3(imap, Conditions.greaterThanOrEqual(stepThreshold));

	if (bb.isEmpty()) {
		return new INDArray[] { Nd4j.empty(), Nd4j.empty() };
	}

	INDArray yx = bb.transpose();

	// TODO : implement the following code fragment
	//  if y.shape[0] == 1:
	//    dx1 = np.flipud(dx1)
	//    dy1 = np.flipud(dy1)
	//    dx2 = np.flipud(dx2)
	//    dy2 = np.flipud(dy2)
	if (yx.size(0) == 1) {
		throw new IllegalStateException("TODO");
	}

	//    q1 = np.fix((stride*bb+1)/scale)
	//    q2 = np.fix((stride*bb+cellsize-1+1)/scale)
	INDArray q1 = Transforms.floor(bb.mul(stride).add(1).div(scale));
	INDArray q2 = Transforms.floor(bb.mul(stride).add(cellSize).div(scale));

	//    dx1 = np.transpose(reg[:,:,0])
	//    dy1 = np.transpose(reg[:,:,1])
	//    dx2 = np.transpose(reg[:,:,2])
	//    dy2 = np.transpose(reg[:,:,3])
	INDArray dx1 = reg.get(all(), all(), point(0));
	INDArray dy1 = reg.get(all(), all(), point(1));
	INDArray dx2 = reg.get(all(), all(), point(2));
	INDArray dy2 = reg.get(all(), all(), point(3));

	// reg = np.transpose(np.vstack([ dx1[(y,x)], dy1[(y,x)], dx2[(y,x)], dy2[(y,x)] ]))
	INDArray outReg = Nd4j.vstack(dx1.get(yx), dy1.get(yx), dx2.get(yx), dy2.get(yx)).transpose();

	//  if reg.size == 0:
	//    reg = np.empty(shape=(0, 3))
	if (outReg.isEmpty()) {
		outReg = Nd4j.empty();
	}

	INDArray score = imap.get(yx).transpose();

	INDArray boundingBox = Nd4j.hstack(q1, q2, score, outReg);

	return new INDArray[] { boundingBox, outReg };
}