/** * Reverse subtraction (in-place) * * @param other the other ndarray to subtract * @param result the result ndarray * @return the ndarray with the operation applied */ @Override public IComplexNDArray rsubi(INDArray other, INDArray result) { return (IComplexNDArray) other.subi(this, result); }
INDArray dLdZSigma = (labelsMinusMuSquared.div(variance).subi(mLabelWidth)).muli(-1).muli(pi);
/** * Reverse subtraction (in-place) * * @param other the other ndarray to subtract * @param result the result ndarray * @return the ndarray with the operation applied */ @Override public INDArray rsubi(INDArray other, INDArray result) { return other.subi(this, result); }
@Override public IComplexNDArray sub(IComplexNumber n) { return dup().subi(n); }
@Override public IComplexNDArray sub(IComplexNumber n, IComplexNDArray result) { return dup().subi(n, result); }
@Override public void revertFeatures(INDArray[] features) { for( int i=0; i<featureIndices.length; i++ ){ INDArray f = features[featureIndices[i]]; if (minRange != 0) { f.subi(minRange); } if (maxRange - minRange != 1.0) { f.divi(maxRange - minRange); } f.muli(this.maxPixelVal); } }
@Override public void revertFeatures(INDArray features) { if (minRange != 0) { features.subi(minRange); } if (maxRange - minRange != 1.0) { features.divi(maxRange - minRange); } features.muli(this.maxPixelVal); }
/** * @param lower row vector of lower bounds * @param upper row vector of upper bounds */ public MinMaxStats(@NonNull INDArray lower, @NonNull INDArray upper) { // Check for 0 differences and round up to epsilon INDArray diff = upper.sub(lower); INDArray addedPadding = Transforms.max(diff, Nd4j.EPS_THRESHOLD).subi(diff); // If any entry in `addedPadding` is not 0, then we had to add something to prevent 0 difference, Add this same // value to the upper bounds to actually apply the padding, and log about it if (addedPadding.sumNumber().doubleValue() > 0) { log.info("API_INFO: max val minus min val found to be zero. Transform will round up to epsilon to avoid nans."); upper.addi(addedPadding); } this.lower = lower; this.upper = upper; }
newDifferences[j] = plusParams[j].subi(minusParams[j]).divi(epsilon);
@Override public void exec() { INDArray softmaxed = Nd4j.getExecutioner().execAndReturn(new OldSoftMax(x)); INDArray mulled = softmaxed.muli(y); INDArray summed = mulled.sum(-1); softmaxed.muliColumnVector(summed); mulled.subi(softmaxed); }
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); }
/** * * @param relStep * @param x * @return */ public static INDArray computeAbsoluteStep(INDArray relStep,INDArray x) { if(relStep == null) { relStep = pow(Nd4j.scalar(getEpsRelativeTo(x)),0.5); } INDArray signX0 = x.gte(0).muli(2).subi(1); return signX0.mul(relStep).muli(max(abs(x),1.0)); }
private INDArray labelsMinusMu(INDArray labels, INDArray mu) { // Now that we have the mixtures, let's compute the negative // log likelihodd of the label against the long nSamples = labels.size(0); long labelsPerSample = labels.size(1); // This worked, but was actually much // slower than the for loop below. // labels = samples, mixtures, labels // mu = samples, mixtures // INDArray labelMinusMu = labels // .reshape('f', nSamples, labelsPerSample, 1) // .repeat(2, mMixtures) // .permute(0, 2, 1) // .subi(mu); // The above code does the same thing as the loop below, // but it does it with index magix instead of a for loop. // It turned out to be way less efficient than the simple 'for' here. INDArray labelMinusMu = Nd4j.zeros(nSamples, mMixtures, labelsPerSample); for (int k = 0; k < mMixtures; k++) { labelMinusMu.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(k), NDArrayIndex.all()}, labels); } labelMinusMu.subi(mu); return labelMinusMu; }
v.muli(momentum).subi(gradient.dup(gradientReshapeOrder).muli(learningRate)); //Modify state array in-place
public 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 scoreArr; //INDArray output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray output = activationFn.getActivation(preOutput.dup(), true); scoreArr = output.subi(labels); Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform("abs", scoreArr)); //Weighted loss function if (weights != null) { if (weights.length() != output.size(1)) { throw new IllegalStateException("Weights vector (length " + weights.length() + ") does not match output.size(1)=" + output.size(1)); } scoreArr.muliRowVector(weights); } if (mask != null) { LossUtil.applyMask(scoreArr, mask); } return scoreArr; }
@Override public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { double[] d = computeScoreNumDenom(labels, preOutput, activationFn, mask, false); double numerator = d[0]; double denominator = d[1]; if (numerator == 0.0 && denominator == 0.0) { //Zero score -> zero gradient return Nd4j.create(preOutput.shape()); } double secondTerm = numerator / (denominator * denominator); INDArray dLdOut; if (labels.size(1) == 1) { //Single binary output case dLdOut = labels.mul(1 + beta * beta).divi(denominator).subi(secondTerm); } else { //Softmax case: the getColumn(1) here is to account for the fact that we're using prob(class1) // only in the score function; column(1) is equivalent to output for the single output case dLdOut = Nd4j.create(labels.shape()); dLdOut.getColumn(1).assign(labels.getColumn(1).mul(1 + beta * beta).divi(denominator).subi(secondTerm)); } //Negate relative to description in paper, as we want to *minimize* 1.0-fMeasure, which is equivalent to // maximizing fMeasure dLdOut.negi(); INDArray dLdPreOut = activationFn.backprop(preOutput, dLdOut).getFirst(); if (mask != null) { dLdPreOut.muliColumnVector(mask); } return dLdPreOut; }
@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 output = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup())); INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray dLda = output.subi(labels).muli(2); if (weights != null) { dLda.muliRowVector(weights); } 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 - but buy us a tiny bit of performance LossUtil.applyMask(dLda, mask); } INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO handle activation function parameter gradients //Loss function with masking if (mask != null) { LossUtil.applyMask(gradients, mask); } return gradients; }
grad = output.mulColumnVector(col).sub(temp); } else { grad = output.subi(labels);
/** * Denormalize a data array * * @param array the data to denormalize * @param stats statistics of the data population */ @Override public void revert(INDArray array, INDArray maskArray, MinMaxStats stats) { // Subtract target range minimum value array.subi(minRange); // Scale by target range array.divi(maxRange - minRange); if (array.rank() <= 2) { array.muliRowVector(stats.getRange()); array.addiRowVector(stats.getLower()); } else { Nd4j.getExecutioner().execAndReturn(new BroadcastMulOp(array, stats.getRange(), array, 1)); Nd4j.getExecutioner().execAndReturn(new BroadcastAddOp(array, stats.getLower(), array, 1)); } if (maskArray != null) { DataSetUtil.setMaskedValuesToZero(array, maskArray); } }
@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 yhat = activationFn.getActivation(preOutput.dup(), true); INDArray yL2norm = labels.norm2(1); INDArray yhatL2norm = yhat.norm2(1); INDArray yhatL2normSq = yhatL2norm.mul(yhatL2norm); //Note: This is not really the L1 norm since I am not taking abs values INDArray yhatDotyL1norm = labels.mul(yhat).sum(1); INDArray dLda = labels.mulColumnVector(yhatL2normSq); dLda.subi(yhat.mulColumnVector(yhatDotyL1norm)); // transform vals to avoid nans before div yL2norm = Transforms.max(yL2norm, Nd4j.EPS_THRESHOLD, false); yhatL2norm = Transforms.max(yhatL2norm, Nd4j.EPS_THRESHOLD, false); yhatL2normSq = Transforms.max(yhatL2normSq, Nd4j.EPS_THRESHOLD, false); dLda.diviColumnVector(yL2norm); dLda.diviColumnVector(yhatL2norm.mul(yhatL2normSq)); dLda.muli(-1); //dL/dz INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO loss functions with params if (mask != null) { gradients.muliColumnVector(mask); } return gradients; }