@Override public Number scan(Condition condition) { MatchCondition op = new MatchCondition(this, condition); return Nd4j.getExecutioner().exec(op, Integer.MAX_VALUE).getDouble(0); }
@Override public String tensorflowName() { throw new NoOpNameFoundException("No tensorflow op opName found for " + opName()); }
/** * Or over the whole ndarray given some condition * * @param n * @param cond * @return */ public static boolean or(final INDArray n, final Condition cond) { if (cond instanceof BaseCondition) { long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0); if (val > 0) return true; else return false; } else { boolean ret = false; final AtomicBoolean a = new AtomicBoolean(ret); Shape.iterate(n, new CoordinateFunction() { @Override public void process(long[]... coord) { if (!a.get()) a.compareAndSet(false, a.get() || cond.apply(n.getDouble(coord[0]))); } }); return a.get(); } }
@Override public String onnxName() { throw new NoOpNameFoundException("No onnx op opName found for " + opName()); }
/** * And over the whole ndarray given some condition * * @param n the ndarray to test * @param cond the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean and(final INDArray n, final Condition cond) { if (cond instanceof BaseCondition) { long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0); if (val == n.lengthLong()) return true; else return false; } else { boolean ret = true; final AtomicBoolean a = new AtomicBoolean(ret); Shape.iterate(n, new CoordinateFunction() { @Override public void process(long[]... coord) { if (a.get()) a.compareAndSet(true, a.get() && cond.apply(n.getDouble(coord[0]))); } }); return a.get(); } }
/** * Or over the whole ndarray given some condition, with respect to dimensions * * @param n the ndarray to test * @param condition the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean[] or(final INDArray n, final Condition condition, int... dimension) { if (!(condition instanceof BaseCondition)) throw new UnsupportedOperationException("Only static Conditions are supported"); MatchCondition op = new MatchCondition(n, condition); INDArray arr = Nd4j.getExecutioner().exec(op, dimension); // FIXME: int cast boolean[] result = new boolean[(int) arr.length()]; for (int i = 0; i < arr.length(); i++) { if (arr.getDouble(i) > 0) result[i] = true; else result[i] = false; } return result; }
/** * And over the whole ndarray given some condition, with respect to dimensions * * @param n the ndarray to test * @param condition the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean[] and(final INDArray n, final Condition condition, int... dimension) { if (!(condition instanceof BaseCondition)) throw new UnsupportedOperationException("Only static Conditions are supported"); MatchCondition op = new MatchCondition(n, condition); INDArray arr = Nd4j.getExecutioner().exec(op, dimension); boolean[] result = new boolean[(int) arr.length()]; long tadLength = Shape.getTADLength(n.shape(), dimension); for (int i = 0; i < arr.length(); i++) { if (arr.getDouble(i) == tadLength) result[i] = true; else result[i] = false; } return result; }
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 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)"); }
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 Number scan(Condition condition) { MatchCondition op = new MatchCondition(this, condition); return Nd4j.getExecutioner().exec(op, Integer.MAX_VALUE).getDouble(0); }
/** * Or over the whole ndarray given some condition * * @param n * @param cond * @return */ public static boolean or(final INDArray n, final Condition cond) { if (cond instanceof BaseCondition) { long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0); if (val > 0) return true; else return false; } else { boolean ret = false; final AtomicBoolean a = new AtomicBoolean(ret); Shape.iterate(n, new CoordinateFunction() { @Override public void process(int[]... coord) { if (!a.get()) a.compareAndSet(false, a.get() || cond.apply(n.getDouble(coord[0]))); } }); return a.get(); } }
/** * And over the whole ndarray given some condition * * @param n the ndarray to test * @param cond the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean and(final INDArray n, final Condition cond) { if (cond instanceof BaseCondition) { long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0); if (val == n.lengthLong()) return true; else return false; } else { boolean ret = true; final AtomicBoolean a = new AtomicBoolean(ret); Shape.iterate(n, new CoordinateFunction() { @Override public void process(int[]... coord) { if (a.get()) a.compareAndSet(true, a.get() && cond.apply(n.getDouble(coord[0]))); } }); return a.get(); } }
/** * Or over the whole ndarray given some condition, with respect to dimensions * * @param n the ndarray to test * @param condition the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean[] or(final INDArray n, final Condition condition, int... dimension) { if (!(condition instanceof BaseCondition)) throw new UnsupportedOperationException("Only static Conditions are supported"); MatchCondition op = new MatchCondition(n, condition); INDArray arr = Nd4j.getExecutioner().exec(op, dimension); boolean[] result = new boolean[arr.length()]; for (int i = 0; i < arr.length(); i++) { if (arr.getDouble(i) > 0) result[i] = true; else result[i] = false; } return result; }
/** * And over the whole ndarray given some condition, with respect to dimensions * * @param n the ndarray to test * @param condition the condition to test against * @return true if all of the elements meet the specified * condition false otherwise */ public static boolean[] and(final INDArray n, final Condition condition, int... dimension) { if (!(condition instanceof BaseCondition)) throw new UnsupportedOperationException("Only static Conditions are supported"); MatchCondition op = new MatchCondition(n, condition); INDArray arr = Nd4j.getExecutioner().exec(op, dimension); boolean[] result = new boolean[arr.length()]; long tadLength = Shape.getTADLength(n.shape(), dimension); for (int i = 0; i < arr.length(); i++) { if (arr.getDouble(i) == tadLength) result[i] = true; else result[i] = false; } return result; }
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 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)"); }
MatchCondition condition = new MatchCondition(totalCountBins, Conditions.equals(0)); int numZeroBins = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0); if (numZeroBins != 0) {
INDArray row = guesses.getRow(i); int countGreaterThan = (int) Nd4j.getExecutioner() .exec(new MatchCondition(row, Conditions.greaterThan(prob)), Integer.MAX_VALUE) .getDouble(0); if (countGreaterThan < topN) {