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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#offset() . 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: OpExecutionerUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/** Tensor1DStats, used to efficiently iterate through tensors on a matrix (2d NDArray) for element-wise ops
 * For example, the offset of each 1d tensor can be calculated using only a single tensorAlongDimension method call,
 * hence is potentially faster than approaches requiring multiple tensorAlongDimension calls.<br>
 * Note that this can only (generally) be used for 2d NDArrays. For certain 3+d NDArrays, the tensor starts may not
 * be in increasing order
 */
public static Tensor1DStats get1DTensorStats(INDArray array, int... dimension) {
    long tensorLength = array.size(dimension[0]);

    //As per tensorssAlongDimension:
    long numTensors = array.tensorssAlongDimension(dimension);

    //First tensor always starts with the first element in the NDArray, regardless of dimension
    long firstTensorOffset = array.offset();

    //Next: Need to work out the separation between the start (first element) of each 1d tensor
    long tensorStartSeparation;
    int elementWiseStride; //Separation in buffer between elements in the tensor
    if (numTensors == 1) {
        tensorStartSeparation = -1; //Not applicable
        elementWiseStride = array.elementWiseStride();
    } else {
        INDArray secondTensor = array.tensorAlongDimension(1, dimension);
        tensorStartSeparation = secondTensor.offset() - firstTensorOffset;
        elementWiseStride = secondTensor.elementWiseStride();
    }

    return new Tensor1DStats(firstTensorOffset, tensorStartSeparation, numTensors, tensorLength, elementWiseStride);
}
 
Example 2
Source File: BlasOpErrorMessage.java    From nd4j with Apache License 2.0 4 votes vote down vote up
private String shapeInfo(INDArray arr) {
    return Arrays.toString(arr.shape()) + " and stride " + Arrays.toString(arr.stride()) + " and offset "
                    + arr.offset() + " and blas stride of " + BlasBufferUtil.getBlasStride(arr);
}
 
Example 3
Source File: GemvParameters.java    From nd4j with Apache License 2.0 4 votes vote down vote up
private INDArray copyIfNecessaryVector(INDArray vec) {
    if (vec.offset() != 0)
        return vec.dup();
    return vec;
}
 
Example 4
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 5
Source File: GemvParameters.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private INDArray copyIfNecessaryVector(INDArray vec) {
    if (vec.offset() != 0)
        return vec.dup();
    return vec;
}
 
Example 6
Source File: CheckUtil.java    From deeplearning4j 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 7
Source File: BlasBufferUtil.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get blas stride for the
 * given array
 * @param arr the array
 * @return the blas stride
 */
public static int getBlasOffset(INDArray arr) {
    // FIXME: LONG
    return (int) arr.offset();
}
 
Example 8
Source File: BlasBufferUtil.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get blas stride for the
 * given array
 * @param arr the array
 * @return the blas stride
 */
public static long getBlasOffset(INDArray arr) {
    return arr.offset();
}