public static void main(String[] args){ int nRows = 3; int nCols = 5; long rngSeed = 12345; //Generate random numbers between -1 and +1 INDArray random = Nd4j.rand(nRows, nCols, rngSeed).muli(2).subi(1); System.out.println("Array values:"); System.out.println(random); //For example, we can conditionally replace values less than 0.0 with 0.0: INDArray randomCopy = random.dup(); BooleanIndexing.replaceWhere(randomCopy, 0.0, Conditions.lessThan(0.0)); System.out.println("After conditionally replacing negative values:\n" + randomCopy); //Or conditionally replace NaN values: INDArray hasNaNs = Nd4j.create(new double[]{1.0,1.0,Double.NaN,1.0}); BooleanIndexing.replaceWhere(hasNaNs,0.0, Conditions.isNan()); System.out.println("hasNaNs after replacing NaNs with 0.0:\n" + hasNaNs); //Or we can conditionally copy values from one array to another: randomCopy = random.dup(); INDArray tens = Nd4j.valueArrayOf(nRows, nCols, 10.0); BooleanIndexing.replaceWhere(randomCopy, tens, Conditions.lessThan(0.0)); System.out.println("Conditionally copying values from array 'tens', if original value is less than 0.0\n" + randomCopy); //One simple task is to count the number of values that match the condition MatchCondition op = new MatchCondition(random, Conditions.greaterThan(0.0)); int countGreaterThanZero = Nd4j.getExecutioner().exec(op,Integer.MAX_VALUE).getInt(0); //MAX_VALUE = "along all dimensions" or equivalently "for entire array" System.out.println("Number of values matching condition 'greater than 0': " + countGreaterThanZero); }
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): "); }
public double setScoreFor(INDArray parameters) { if (Nd4j.ENFORCE_NUMERICAL_STABILITY) { BooleanIndexing.applyWhere(parameters, Conditions.isNan(), new Value(Nd4j.EPS_THRESHOLD)); } layer.setParams(parameters); layer.computeGradientAndScore(); return layer.score(); }
private static INDArray logZ(INDArray z) { INDArray log = log(z, true); // log approaches -Infinity as z approaches zero. Replace -Infinity with the least possible value. // Caveat: does not handle +Infinity since z is assumed to be 0 <= z <= 1. switch (log.data().dataType()) { case FLOAT: BooleanIndexing.applyWhere(log, new Or(Conditions.isNan(), Conditions.isInfinite()), new StableNumber(StableNumber.Type.FLOAT)); break; case DOUBLE: BooleanIndexing.applyWhere(log, new Or(Conditions.isNan(), Conditions.isInfinite()), new StableNumber(StableNumber.Type.DOUBLE)); break; case INT: BooleanIndexing.applyWhere(log, new Or(Conditions.isNan(), Conditions.isInfinite()), new Value(-Integer.MAX_VALUE)); break; default: throw new RuntimeException("unsupported data type: " + log.data().dataType()); } return log; }
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): "); }
@Override public INDArray gradient(INDArray x, INDArray preOutDistributionParams) { INDArray output = preOutDistributionParams.dup(); activationFn.getActivation(output, true); INDArray diff = x.sub(output); INDArray outOneMinusOut = output.rsub(1.0).muli(output); INDArray grad = diff.divi(outOneMinusOut); grad = activationFn.backprop(preOutDistributionParams.dup(), grad).getFirst(); //Issue: if output == 0 or output == 1, then (assuming sigmoid output or similar) //sigmaPrime == 0, sigmaPrime * (x-out) / (out*(1-out)) == 0 * (x-out) / 0 -> 0/0 -> NaN. But taking limit, we want //0*(x-out)/0 == 0 -> implies 0 gradient at the far extremes (0 or 1) of the output BooleanIndexing.replaceWhere(grad, 0.0, Conditions.isNan()); return grad.negi(); }
BooleanIndexing.applyWhere(p, Conditions.isNan(), new Value(1e-12));
BooleanIndexing.applyWhere(p, Conditions.isNan(), new Value(1e-12));