Search in sources :

Example 6 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class OpExecutionerUtil method checkForNaN.

public static void checkForNaN(INDArray z) {
    if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.NAN_PANIC && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.ANY_PANIC)
        return;
    int match = 0;
    if (!z.isScalar()) {
        MatchCondition condition = new MatchCondition(z, Conditions.isNan());
        match = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    } else {
        if (z.data().dataType() == DataBuffer.Type.DOUBLE) {
            if (Double.isNaN(z.getDouble(0)))
                match = 1;
        } else {
            if (Float.isNaN(z.getFloat(0)))
                match = 1;
        }
    }
    if (match > 0)
        throw new ND4JIllegalStateException("P.A.N.I.C.! Op.Z() contains " + match + " NaN value(s): ");
}
Also used : MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 7 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class OpExecutionerUtil method checkForInf.

public static void checkForInf(INDArray z) {
    if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.INF_PANIC && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.ANY_PANIC)
        return;
    int match = 0;
    if (!z.isScalar()) {
        MatchCondition condition = new MatchCondition(z, Conditions.isInfinite());
        match = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    } else {
        if (z.data().dataType() == DataBuffer.Type.DOUBLE) {
            if (Double.isInfinite(z.getDouble(0)))
                match = 1;
        } else {
            if (Float.isInfinite(z.getFloat(0)))
                match = 1;
        }
    }
    if (match > 0)
        throw new ND4JIllegalStateException("P.A.N.I.C.! Op.Z() contains " + match + " Inf value(s)");
}
Also used : MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 8 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class CpuThreshold method compress.

@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[] { 1, (int) buffer.length() }).getFirst());
    MatchCondition condition = new MatchCondition(temp, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    if (cntAbs < 2)
        return null;
    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);
    CompressionDescriptor descriptor = new CompressionDescriptor();
    // sizeOf(INT)
    descriptor.setCompressedLength(compressedLength * 4);
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());
    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());
    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);
    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.THRESHOLD, pointer, buffer.length());
    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);
    return cbuff;
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) INDArray(org.nd4j.linalg.api.ndarray.INDArray) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) IntPointer(org.bytedeco.javacpp.IntPointer) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition)

Example 9 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class NativeOpExecutioner method thresholdEncode.

@Override
public INDArray thresholdEncode(INDArray input, double threshold, Integer boundary) {
    MatchCondition condition = new MatchCondition(input, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    if (cntAbs < 2)
        return null;
    if (boundary != null)
        cntAbs = Math.min(cntAbs, boundary);
    DataBuffer buffer = input.data();
    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    DataBuffer encodedBuffer = Nd4j.getMemoryManager().getCurrentWorkspace() == null ? Nd4j.getDataBufferFactory().createInt(4 + cntAbs, false) : Nd4j.getDataBufferFactory().createInt(4 + cntAbs, false, Nd4j.getMemoryManager().getCurrentWorkspace());
    encodedBuffer.put(0, cntAbs);
    encodedBuffer.put(1, (int) buffer.length());
    encodedBuffer.put(2, Float.floatToIntBits((float) threshold));
    // format id
    encodedBuffer.put(3, ThresholdCompression.FLEXIBLE_ENCODING);
    CompressionDescriptor descriptor = new CompressionDescriptor();
    // sizeOf(INT)
    descriptor.setCompressedLength(compressedLength * 4);
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());
    descriptor.setCompressionAlgorithm("THRESHOLD");
    descriptor.setCompressionType(CompressionType.LOSSLESS);
    // CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);
    Nd4j.getNDArrayFactory().convertDataEx(AbstractCompressor.getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.THRESHOLD, encodedBuffer.addressPointer(), buffer.length());
    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);
    return Nd4j.createArrayFromShapeBuffer(encodedBuffer, input.shapeInfoDataBuffer());
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 10 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class LossFunctionTest method testClippingXENT.

@Test
public void testClippingXENT() throws Exception {
    ILossFunction l1 = new LossBinaryXENT(0);
    ILossFunction l2 = new LossBinaryXENT();
    INDArray labels = Nd4j.getExecutioner().exec(new BernoulliDistribution(Nd4j.create(3, 5), 0.5));
    INDArray preOut = Nd4j.valueArrayOf(3, 5, -1000.0);
    IActivation a = new ActivationSigmoid();
    double score1 = l1.computeScore(labels, preOut.dup(), a, null, false);
    assertTrue(Double.isNaN(score1));
    double score2 = l2.computeScore(labels, preOut.dup(), a, null, false);
    assertFalse(Double.isNaN(score2));
    INDArray grad1 = l1.computeGradient(labels, preOut.dup(), a, null);
    INDArray grad2 = l2.computeGradient(labels, preOut.dup(), a, null);
    MatchCondition c1 = new MatchCondition(grad1, Conditions.isNan());
    MatchCondition c2 = new MatchCondition(grad2, Conditions.isNan());
    int match1 = Nd4j.getExecutioner().exec(c1, Integer.MAX_VALUE).getInt(0);
    int match2 = Nd4j.getExecutioner().exec(c2, Integer.MAX_VALUE).getInt(0);
    assertTrue(match1 > 0);
    assertEquals(0, match2);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) BernoulliDistribution(org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution) ActivationSigmoid(org.nd4j.linalg.activations.impl.ActivationSigmoid) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) IActivation(org.nd4j.linalg.activations.IActivation) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

MatchCondition (org.nd4j.linalg.api.ops.impl.accum.MatchCondition)17 INDArray (org.nd4j.linalg.api.ndarray.INDArray)12 Test (org.junit.Test)8 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)6 BaseCondition (org.nd4j.linalg.indexing.conditions.BaseCondition)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CoordinateFunction (org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction)2 CompressionDescriptor (org.nd4j.linalg.compression.CompressionDescriptor)2 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)2 IntPointer (org.bytedeco.javacpp.IntPointer)1 IActivation (org.nd4j.linalg.activations.IActivation)1 ActivationSigmoid (org.nd4j.linalg.activations.impl.ActivationSigmoid)1 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)1 BernoulliDistribution (org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution)1 DefaultRandom (org.nd4j.linalg.api.rng.DefaultRandom)1 Random (org.nd4j.linalg.api.rng.Random)1 Distribution (org.nd4j.linalg.api.rng.distribution.Distribution)1 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)1 NativeRandom (org.nd4j.rng.NativeRandom)1