Java Code Examples for org.nd4j.linalg.factory.Nd4j#ENFORCE_NUMERICAL_STABILITY

The following examples show how to use org.nd4j.linalg.factory.Nd4j#ENFORCE_NUMERICAL_STABILITY . 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: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * in place (element wise) division of two matrices
 *
 * @param other  the second ndarray to divide
 * @param result the result ndarray
 * @return the result of the divide
 */
@Override
public INDArray divi(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return divi(other.getDouble(0), result);
    }

    if (isScalar()) {
        return other.rdivi(getDouble(0), result);
    }


    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        Nd4j.getExecutioner().exec(new BroadcastDivOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }


    LinAlgExceptions.assertSameShape(other, result);
    Nd4j.getExecutioner().exec(new OldDivOp(this, other, result, length()));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 2
Source File: MtcnnServiceBenchmark.java    From mtcnn-java with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Trial)
public void setUp() throws IOException {
	Nd4j.ENFORCE_NUMERICAL_STABILITY = false;

	mtcnnService = new MtcnnService(30, 0.709, new double[] { 0.6, 0.7, 0.7 });

	image = new Java2DNativeImageLoader().asMatrix(
			new DefaultResourceLoader().getResource("classpath:/VikiMaxiAdi.jpg").getInputStream())
			.get(point(0), all(), all(), all()).dup();
}
 
Example 3
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray rdivi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;
    Nd4j.getExecutioner().exec(new ScalarReverseDivision(this, null, result, result.length(), n));
    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 4
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray rsubi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;

    Nd4j.getExecutioner().exec(new ScalarReverseSubtraction(this, null, result, result.lengthLong(), n));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 5
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray muli(Number n, INDArray result) {
    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;
    Nd4j.getExecutioner().exec(new ScalarMultiplication(this, null, result, result.lengthLong(), n));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 6
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray subi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;

    Nd4j.getExecutioner().exec(new ScalarSubtraction(this, null, result, result.lengthLong(), n));
    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 7
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place (element wise) multiplication of two matrices
 *
 * @param other  the second ndarray to multiply
 * @param result the result ndarray
 * @return the result of the multiplication
 */
@Override
public INDArray muli(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return muli(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.muli(getDouble(0), result);
    }



    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        Nd4j.getExecutioner().exec(new BroadcastMulOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }

    LinAlgExceptions.assertSameShape(other, result);

    Nd4j.getExecutioner().exec(new OldMulOp(this, other, result, length()));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 8
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place subtraction of two matrices
 *
 * @param other  the second ndarray to subtract
 * @param result the result ndarray
 * @return the result of the subtraction
 */
@Override
public INDArray subi(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return subi(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.rsubi(getDouble(0), result);
    }


    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        Nd4j.getExecutioner().exec(new BroadcastSubOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }


    LinAlgExceptions.assertSameShape(other, result);


    Nd4j.getExecutioner().exec(new OldSubOp(this, other,result));

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 9
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place addition of two matrices
 *
 * @param other  the second ndarray to add
 * @param result the result ndarray
 * @return the result of the addition
 */
@Override
public INDArray addi(INDArray other, INDArray result) {
    if (other.isScalar()) {
        return result.addi(other.getDouble(0), result);
    }

    if (isScalar()) {
        return other.addi(getDouble(0), result);
    }

    if(!Shape.shapeEquals(this.shape(),other.shape())) {
        int[] broadcastDimensions = Shape.getBroadcastDimensions(this.shape(),other.shape());
        result = Nd4j.createUninitialized(Shape.broadcastOutputShape(this.shape(),other.shape()));
        Nd4j.getExecutioner().exec(new BroadcastAddOp(this,other,result,broadcastDimensions),broadcastDimensions);
        return result;
    }

    LinAlgExceptions.assertSameShape(other, result);

    Nd4j.getExecutioner().exec(new OldAddOp(this, other, result, length()));


    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 10
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Perform an copy matrix multiplication
 *
 * @param other  the other matrix to perform matrix multiply with
 * @param result the result ndarray
 * @return the result of the matrix multiplication
 */
@Override
public INDArray mmuli(INDArray other, INDArray result) {
    LinAlgExceptions.assertMultiplies(this, other);


    if (other.isScalar()) {
        return muli(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.muli(getDouble(0), result);
    }

    /* check sizes and resize if necessary */


    if (result == this || result == other) {
        /* actually, blas cannot do multiplications in-place. Therefore, we will fake by
         * allocating a temporary object on the side and copy the result later.
         */
        INDArray temp = Nd4j.create(result.shape(), Nd4j.getStrides(result.shape(), 'f'));

        if (other.columns() == 1 || other.rank() == 1) {
            Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(result),
                    BlasBufferUtil.getCharForTranspose(this), 1.0, this, other, 0.0, temp);
        }

        else {
            Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(result),
                    BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(temp), 1.0,
                    this, other, 0.0, temp);
            Nd4j.getBlasWrapper().level3().gemm(
                    BlasBufferUtil.getCharForTranspose(result),
                    BlasBufferUtil.getCharForTranspose(this),
                    BlasBufferUtil.getCharForTranspose(temp),
                    1.0,
                    this,
                    other,
                    0.0,
                    temp);
        }

        result.assign(temp);


    } else {

        //We require that the result array is 'f' (fortran) order
        // However, user might have called mmuli with a c order array for the result
        // In which case, we need to allocate a temporary f order array, and later do an assign to the real result array

        boolean requiresTemp = result.ordering() == 'c';
        INDArray gemmResultArr;
        if (requiresTemp) {
            //Can use createUninitialized due to beta==0.0 parameter in gemm
            gemmResultArr = Nd4j.createUninitialized(result.shape(), 'f');
        } else {
            gemmResultArr = result;
        }

        if (other.columns() == 1 || other.rank() == 1) {
            Nd4j.getBlasWrapper().level2().gemv(
                    ordering(),
                    BlasBufferUtil.getCharForTranspose(other),
                    1.0,
                    this,
                    other,
                    0.0,
                    gemmResultArr);
        } else {
            //gemm doesn't support strides so vectors and views
            //don't work
            Nd4j.getBlasWrapper().level3().gemm(ordering(),
                    BlasBufferUtil.getCharForTranspose(other),
                    BlasBufferUtil.getCharForTranspose(gemmResultArr),
                    1.0,
                    this,
                    other,
                    0.0,
                    gemmResultArr);
        }

        if (requiresTemp) {
            result.assign(gemmResultArr);
        }
    }

    // 1D edge case: reshape back to vector
    if (other.rank() == 1)
        result = result.reshape(result.length());

    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);
    return result;
}
 
Example 11
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray mmuli(INDArray other, INDArray result) {
    LinAlgExceptions.assertMultiplies(this, other);


    if (other.isScalar()) {
        return muli(other.getDouble(0), result);
    }
    if (isScalar()) {
        return other.muli(getDouble(0), result);
    }

    /* check sizes and resize if necessary */



    //We require that the result array is 'f' (fortran) order
    // However, user might have called mmuli with a c order array for the result
    // In which case, we need to allocate a temporary f order array, and later do an assign to the real result array

    boolean requiresTemp = result.ordering() == 'c';
    INDArray gemmResultArr;
    if (requiresTemp) {
        //Can use createUninitialized due to beta==0.0 parameter in gemm
        gemmResultArr = Nd4j.createUninitialized(result.shape(), 'f');
    } else {
        gemmResultArr = result;
    }

    if (other.columns() == 1) {
        Nd4j.getBlasWrapper().level2().gemv(ordering(), BlasBufferUtil.getCharForTranspose(other), 1.0, this, other,
                        0.0, gemmResultArr);
    } else {
        //gemm doesn't support strides so vectors and views
        //don't work
        if (isView() && isVector()) {
            return dup().mmuli(other, gemmResultArr);
        }

        Nd4j.getBlasWrapper().level3().gemm(ordering(), BlasBufferUtil.getCharForTranspose(other),
                        BlasBufferUtil.getCharForTranspose(gemmResultArr), 1.0, this, other, 0.0, gemmResultArr);
    }

    if (requiresTemp) {
        result.assign(gemmResultArr);
    }


    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}
 
Example 12
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 3 votes vote down vote up
@Override
public INDArray divi(Number n, INDArray result) {

    if (Double.isNaN(n.doubleValue()))
        n = Nd4j.EPS_THRESHOLD;
    Nd4j.getExecutioner().exec(new ScalarDivision(this, null, result, result.lengthLong(), n));


    if (Nd4j.ENFORCE_NUMERICAL_STABILITY)
        Nd4j.clearNans(result);

    return result;
}