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); }
if (n == 1) { isPositiveLabel = labels; isNegativeLabel = Transforms.not(isPositiveLabel); pClass0 = output.rsub(1.0); pClass1 = output;
INDArray forward = and(greaterThanOrEqual(upperBound,lowerDist),not(central)); hAdjusted.put(forward,min(h.get(forward),upperBound2.get(forward).mul(0.5).divi(numSteps))); oneSided.put(forward,Nd4j.scalar(1.0)); INDArray backward = and(upperBound2.lt(lowerBound),not(central)); hAdjusted.put(backward,min(h.get(backward),lowerDist.get(backward).mul(0.5).divi(numSteps))); oneSided.put(backward,Nd4j.scalar(1.0)); INDArray adjustedCentral = and(not(central),lessThanOrEqual(abs(hAdjusted),minDist)); hAdjusted.put(adjustedCentral,minDist.get(adjustedCentral)); oneSided.put(adjustedCentral,Nd4j.scalar(0.0));
NDArrayIndex.interval(currentTimeSliceStart, currentTimeSliceEnd)); if (minorityLabel == 0) { currentLabel = Transforms.not(currentLabel);
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); }
if(n == 1){ isPositiveLabel = labels; isNegativeLabel = Transforms.not(isPositiveLabel); pClass0 = output.rsub(1.0); pClass1 = output;
case MAX: INDArray negInfMask = Transforms.not(mask); BooleanIndexing.replaceWhere(negInfMask, Double.NEGATIVE_INFINITY, Conditions.equals(1.0));
case MAX: INDArray negInfMask = Transforms.not(mask); BooleanIndexing.replaceWhere(negInfMask, Double.NEGATIVE_INFINITY, Conditions.equals(1.0));
case MAX: INDArray negInfMask = Transforms.not(mask); BooleanIndexing.replaceWhere(negInfMask, Double.NEGATIVE_INFINITY, Conditions.equals(1.0));
public INDArray adjustMasks(INDArray label, INDArray labelMask, int minorityLabel, double targetDist) { if (labelMask == null) { labelMask = Nd4j.ones(label.size(0), label.size(2)); } validateData(label,labelMask); INDArray bernoullis = Nd4j.zeros(labelMask.shape()); int currentTimeSliceEnd = label.size(2); //iterate over each tbptt window while (currentTimeSliceEnd > 0) { int currentTimeSliceStart = Math.max(currentTimeSliceEnd - tbpttWindowSize, 0); //get views for current time slice INDArray currentWindowBernoulli = bernoullis.get(NDArrayIndex.all(), NDArrayIndex.interval(currentTimeSliceStart, currentTimeSliceEnd)); INDArray currentMask = labelMask.get(NDArrayIndex.all(), NDArrayIndex.interval(currentTimeSliceStart, currentTimeSliceEnd)); INDArray currentLabel; if (label.size(1) == 2) { //if one hot grab the right index currentLabel = label.get(NDArrayIndex.all(), NDArrayIndex.point(minorityLabel), NDArrayIndex.interval(currentTimeSliceStart, currentTimeSliceEnd)); } else { currentLabel = label.get(NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.interval(currentTimeSliceStart, currentTimeSliceEnd)); if (minorityLabel == 0) { currentLabel = Transforms.not(currentLabel); } } //calculate required probabilities and write into the view currentWindowBernoulli.assign(calculateBernoulli(currentLabel, currentMask, targetDist)); currentTimeSliceEnd = currentTimeSliceStart; } return Nd4j.getExecutioner().exec(new BernoulliDistribution(Nd4j.createUninitialized(bernoullis.shape()), bernoullis), Nd4j.getRandom()); }
case MAX: INDArray negInfMask = Transforms.not(mask); BooleanIndexing.replaceWhere(negInfMask, Double.NEGATIVE_INFINITY, Conditions.equals(1.0));
INDArray notLabels = Transforms.not(labels); for (int j = 0; j < histogramNumBins; j++) { INDArray geqBinLower = labelsSubPredicted.gte(j * binSize);