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); }
private INDArray calculateBernoulli(INDArray minorityLabels, INDArray labelMask, double targetMinorityDist) { INDArray minorityClass = minorityLabels.dup().muli(labelMask); INDArray majorityClass = Transforms.not(minorityLabels).muli(labelMask); //all minorityLabel class, keep masks as is //presence of minoriy class and donotmask minority windows set to true return label as is if (majorityClass.sumNumber().intValue() == 0 || (minorityClass.sumNumber().intValue() > 0 && donotMaskMinorityWindows)) return labelMask; //all majority class and set to not mask all majority windows sample majority class by 1-targetMinorityDist if (minorityClass.sumNumber().intValue() == 0 && !maskAllMajorityWindows) return labelMask.muli(1 - targetMinorityDist); //Probabilities to be used for bernoulli sampling INDArray minoritymajorityRatio = minorityClass.sum(1).div(majorityClass.sum(1)); INDArray majorityBernoulliP = minoritymajorityRatio.muli(1 - targetMinorityDist).divi(targetMinorityDist); BooleanIndexing.replaceWhere(majorityBernoulliP, 1.0, Conditions.greaterThan(1.0)); //if minority ratio is already met round down to 1.0 return majorityClass.muliColumnVector(majorityBernoulliP).addi(minorityClass); }
private INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray output = activationFn.getActivation(preOutput.dup(), true); if(activationFn instanceof ActivationSoftmax && softmaxClipEps > 0.0){ BooleanIndexing.replaceWhere(output, softmaxClipEps, Conditions.lessThan(softmaxClipEps)); BooleanIndexing.replaceWhere(output, 1.0-softmaxClipEps, Conditions.greaterThan(1.0-softmaxClipEps)); } INDArray scoreArr = Transforms.log(output, false).muli(labels); //Weighted loss function if (weights != null) { if (weights.length() != scoreArr.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + preOutput.size(1)); } scoreArr.muliRowVector(weights); } if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
BooleanIndexing.replaceWhere(bitMaskRowCol, 1.0, Conditions.greaterThan(0.0));
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException( "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray scoreArr = scoreArray(labels, preOutput, activationFn, mask); INDArray bitMaskRowCol = scoreArr.dup(); /* bit mask is 0 if 1-sigma(y*yhat) is neg, bit mask is 1 if 1-sigma(y*yhat) is +ve */ BooleanIndexing.replaceWhere(bitMaskRowCol, 0.0, Conditions.lessThan(0.0)); BooleanIndexing.replaceWhere(bitMaskRowCol, 1.0, Conditions.greaterThan(0.0)); INDArray dLda = scoreArr.muli(2).muli(labels.neg()); dLda.muli(bitMaskRowCol); if (mask != null && LossUtil.isPerOutputMasking(dLda, mask)) { //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - though buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with params if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
Conditions.lessThan(3)); INDArray greaterThan5 = CustomOperations.booleanOp(CustomOperations.randInt(new int[]{3, 3}, 10), Conditions.greaterThan(5)); INDArray compared = CustomOperations.compare(lessThan3, greaterThan5, new Predicate<Boolean[]>() { @Override
private INDArray ensurePositive(INDArray comboFeatures) { BooleanIndexing.applyWhere(comboFeatures, Conditions.lessThan(0.0f), new Value(0.0f)); BooleanIndexing.applyWhere(comboFeatures, Conditions.greaterThan(0.0f), new Value(1.0f)); return comboFeatures; }
private INDArray calculateBernoulli(INDArray minorityLabels, INDArray labelMask, double targetMinorityDist) { INDArray minorityClass = minorityLabels.dup().muli(labelMask); INDArray majorityClass = Transforms.not(minorityLabels).muli(labelMask); //all minorityLabel class, keep masks as is //presence of minoriy class and donotmask minority windows set to true return label as is if (majorityClass.sumNumber().intValue() == 0 || (minorityClass.sumNumber().intValue() > 0 && donotMaskMinorityWindows)) return labelMask; //all majority class and set to not mask all majority windows sample majority class by 1-targetMinorityDist if (minorityClass.sumNumber().intValue() == 0 && !maskAllMajorityWindows) return labelMask.muli(1-targetMinorityDist); //Probabilities to be used for bernoulli sampling INDArray minoritymajorityRatio = minorityClass.sum(1).div(majorityClass.sum(1)); INDArray majorityBernoulliP = minoritymajorityRatio.muli(1 - targetMinorityDist).divi(targetMinorityDist); BooleanIndexing.replaceWhere(majorityBernoulliP,1.0, Conditions.greaterThan(1.0)); //if minority ratio is already met round down to 1.0 return majorityClass.muliColumnVector(majorityBernoulliP).addi(minorityClass); }
private INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException("Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray output = activationFn.getActivation(preOutput.dup(), true); if(activationFn instanceof ActivationSoftmax && softmaxClipEps > 0.0){ BooleanIndexing.replaceWhere(output, softmaxClipEps, Conditions.lessThan(softmaxClipEps)); BooleanIndexing.replaceWhere(output, 1.0-softmaxClipEps, Conditions.greaterThan(1.0-softmaxClipEps)); } INDArray scoreArr = Transforms.log(output, false).muli(labels); //Weighted loss function if (weights != null) { if (weights.length() != scoreArr.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + preOutput.size(1)); } scoreArr.muliRowVector(weights); } if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
case ClipElementWiseAbsoluteValue: if (layerGradientView != null) { BooleanIndexing.replaceWhere(layerGradientView, threshold, Conditions.greaterThan(threshold)); BooleanIndexing.replaceWhere(layerGradientView, -threshold, Conditions.lessThan(-threshold));
gains = gains.add(.2).muli(dY.cond(Conditions.greaterThan(0)).neqi(iY.cond(Conditions.greaterThan(0)))) .addi(gains.mul(0.8).muli(dY.cond(Conditions.greaterThan(0)) .eqi(iY.cond(Conditions.greaterThan(0)))));
gains = gains.add(.2).muli(dY.cond(Conditions.greaterThan(0)).neqi(iY.cond(Conditions.greaterThan(0)))) .addi(gains.mul(0.8).muli(dY.cond(Conditions.greaterThan(0)) .eqi(iY.cond(Conditions.greaterThan(0)))));
BooleanIndexing.replaceWhere(bitMaskRowCol, 1.0, Conditions.greaterThan(0.0));
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { if (labels.size(1) != preOutput.size(1)) { throw new IllegalArgumentException("Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer" + " number of outputs (nOut = " + preOutput.size(1) + ") "); } INDArray scoreArr = scoreArray(labels, preOutput, activationFn, mask); INDArray bitMaskRowCol = scoreArr.dup(); /* bit mask is 0 if 1-sigma(y*yhat) is neg, bit mask is 1 if 1-sigma(y*yhat) is +ve */ BooleanIndexing.replaceWhere(bitMaskRowCol, 0.0, Conditions.lessThan(0.0)); BooleanIndexing.replaceWhere(bitMaskRowCol, 1.0, Conditions.greaterThan(0.0)); INDArray dLda = scoreArr.muli(2).muli(labels.neg()); dLda.muli(bitMaskRowCol); if(mask != null && LossUtil.isPerOutputMasking(dLda, mask)){ //For *most* activation functions: we don't actually need to mask dL/da in addition to masking dL/dz later //but: some, like softmax, require both (due to dL/dz_i being a function of dL/da_j, for i != j) //We could add a special case for softmax (activationFn instanceof ActivationSoftmax) but that would be // error prone - though buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO activation functions with params if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
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) {