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): ");
}
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)");
}
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;
}
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());
}
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);
}
Aggregations