@Test public void correctDerivative() throws Exception { INDArray label = Nd4j.create(2, 3); label.put(0, 0, 1); label.put(0, 1, 0); label.put(0, 2, 0); label.put(1, 0, 1); label.put(1, 1, 0); label.put(1, 2, 0); INDArray preOutput = Nd4j.create(label.shape()); preOutput.put(0, 0, 1); preOutput.put(0, 1, 0); preOutput.put(0, 2, -2); preOutput.put(1, 0, 1); preOutput.put(1, 1, -2); preOutput.put(1, 2, 0); INDArray scoreArray = rankingLoss.computeDlDx(label, preOutput); double score = scoreArray.sumNumber().doubleValue(); assertThat(score, is(closeTo(-0.8860, 0.05d))); }
public INDArray computeDlDx(INDArray labels, INDArray predictedScores) { INDArray mPluss = Nd4j.zeros(predictedScores.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(predictedScores.shape()).addi(mMinus); INDArray gammaDivNumCorrectLabels = Transforms.max(labels.sum(1), 1).rdivi(gamma); INDArray positiveExamples = predictedScores.mul(labels); INDArray leftHandExp = Transforms.exp((positiveExamples.sum(1).rsubi(mPlus)).muli(gammaDivNumCorrectLabels)); INDArray leftHandNumerator = leftHandExp.mulColumnVector(gammaDivNumCorrectLabels); INDArray leftHandDenominator = leftHandExp.addi(1); INDArray leftHand = leftHandNumerator.divi(leftHandDenominator); leftHand = labels.mulColumnVector(leftHand.negi()); INDArray rightHandExp = Transforms.exp((mMinuss.addi(predictedScores)).muli(gamma)); INDArray rightHandNumerator = rightHandExp.mul(gamma); INDArray rightHandDenominator = rightHandExp.addi(1); INDArray rightHand = rightHandNumerator.divi(rightHandDenominator); predictedScores = predictedScores.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNegative = predictedScores.argMax(1); INDArray negWithHighestScoreMask = Nd4j.zeros(predictedScores.shape()); for(int i = 0; i < maxNegative.length(); i++) { int index = maxNegative.getInt(i); negWithHighestScoreMask.put(i, index, 1); } leftHand = leftHand.muli(labels); rightHand = rightHand.muli(negWithHighestScoreMask); rightHand = rightHand.mulColumnVector(Transforms.min(labels.sum(1),1)); return leftHand.addi(rightHand); }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray computeDlDx(INDArray labels, INDArray predictedScores) { INDArray mPluss = Nd4j.zeros(predictedScores.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(predictedScores.shape()).addi(mMinus); INDArray gammaDivNumCorrectLabels = Transforms.max(labels.sum(1), 1).rdivi(gamma); INDArray positiveExamples = predictedScores.mul(labels); INDArray leftHandExp = Transforms.exp((positiveExamples.sum(1).rsubi(mPlus)).muli(gammaDivNumCorrectLabels)); INDArray leftHandNumerator = leftHandExp.mulColumnVector(gammaDivNumCorrectLabels); INDArray leftHandDenominator = leftHandExp.addi(1); INDArray leftHand = leftHandNumerator.divi(leftHandDenominator); leftHand = labels.mulColumnVector(leftHand.negi()); INDArray rightHandExp = Transforms.exp((mMinuss.addi(predictedScores)).muli(gamma)); INDArray rightHandNumerator = rightHandExp.mul(gamma); INDArray rightHandDenominator = rightHandExp.addi(1); INDArray rightHand = rightHandNumerator.divi(rightHandDenominator); predictedScores = predictedScores.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNegative = predictedScores.argMax(1); INDArray negWithHighestScoreMask = Nd4j.zeros(predictedScores.shape()); for(int i = 0; i < maxNegative.length(); i++) { int index = maxNegative.getInt(i); negWithHighestScoreMask.put(i, index, 1); } leftHand = leftHand.muli(labels); rightHand = rightHand.muli(negWithHighestScoreMask); rightHand = rightHand.mulColumnVector(Transforms.min(labels.sum(1),1)); return leftHand.addi(rightHand); } }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray computeDlDx(INDArray labels, INDArray predictedScores) { INDArray mPluss = Nd4j.zeros(predictedScores.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(predictedScores.shape()).addi(mMinus); INDArray gammaDivNumCorrectLabels = Transforms.max(labels.sum(1), 1).rdivi(gamma); INDArray positiveExamples = predictedScores.mul(labels); INDArray leftHandExp = Transforms.exp((positiveExamples.sum(1).rsubi(mPlus)).muli(gammaDivNumCorrectLabels)); INDArray leftHandNumerator = leftHandExp.mulColumnVector(gammaDivNumCorrectLabels); INDArray leftHandDenominator = leftHandExp.addi(1); INDArray leftHand = leftHandNumerator.divi(leftHandDenominator); leftHand = labels.mulColumnVector(leftHand.negi()); INDArray rightHandExp = Transforms.exp((mMinuss.addi(predictedScores)).muli(gamma)); INDArray rightHandNumerator = rightHandExp.mul(gamma); INDArray rightHandDenominator = rightHandExp.addi(1); INDArray rightHand = rightHandNumerator.divi(rightHandDenominator); predictedScores = predictedScores.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNegative = predictedScores.argMax(1); INDArray negWithHighestScoreMask = Nd4j.zeros(predictedScores.shape()); for(int i = 0; i < maxNegative.length(); i++) { int index = maxNegative.getInt(i); negWithHighestScoreMask.put(i, index, 1); } leftHand = leftHand.muli(labels); rightHand = rightHand.muli(negWithHighestScoreMask); rightHand = rightHand.mulColumnVector(Transforms.min(labels.sum(1),1)); return leftHand.addi(rightHand); } MultiClassDosSantosPairwiseRankingLoss();  MultiClassDosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  MultiClassDosSantosPairwiseRankingLoss(double gamma, double mPlus, double mMinus, double positiveClassExclusionFactor); }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray computeDlDx(INDArray labels, INDArray predictedScores) { INDArray mPluss = Nd4j.zeros(predictedScores.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(predictedScores.shape()).addi(mMinus); INDArray gammaDivNumCorrectLabels = Transforms.max(labels.sum(1), 1).rdivi(gamma); INDArray positiveExamples = predictedScores.mul(labels); INDArray leftHandExp = Transforms.exp((positiveExamples.sum(1).rsubi(mPlus)).muli(gammaDivNumCorrectLabels)); INDArray leftHandNumerator = leftHandExp.mulColumnVector(gammaDivNumCorrectLabels); INDArray leftHandDenominator = leftHandExp.addi(1); INDArray leftHand = leftHandNumerator.divi(leftHandDenominator); leftHand = labels.mulColumnVector(leftHand.negi()); INDArray rightHandExp = Transforms.exp((mMinuss.addi(predictedScores)).muli(gamma)); INDArray rightHandNumerator = rightHandExp.mul(gamma); INDArray rightHandDenominator = rightHandExp.addi(1); INDArray rightHand = rightHandNumerator.divi(rightHandDenominator); predictedScores = predictedScores.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNegative = predictedScores.argMax(1); INDArray negWithHighestScoreMask = Nd4j.zeros(predictedScores.shape()); for(int i = 0; i < maxNegative.length(); i++) { int index = maxNegative.getInt(i); negWithHighestScoreMask.put(i, index, 1); } leftHand = leftHand.muli(labels); rightHand = rightHand.muli(negWithHighestScoreMask); rightHand = rightHand.mulColumnVector(Transforms.min(labels.sum(1),1)); return leftHand.addi(rightHand); } MultiClassDosSantosPairwiseRankingLoss();  MultiClassDosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  MultiClassDosSantosPairwiseRankingLoss(double gamma, double mPlus, double mMinus, double positiveClassExclusionFactor); INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask); @Override double computeScore(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask, boolean average); @Override INDArray computeScoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask); @Override INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask); INDArray computeDlDx(INDArray labels, INDArray predictedScores); @Override Pair<Double, INDArray> computeGradientAndScore(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask, boolean average); @Override String name(); Number getGamma(); void setGamma(Number gamma); Number getmPlus(); void setmPlus(Number mPlus); Number getmMinus(); void setmMinus(Number mMinus); Number getPositiveClassExclusionFactor(); void setPositiveClassExclusionFactor(Number positiveClassExclusionFactor); }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray computeDlDx(INDArray labels, INDArray predictedScores) { INDArray mPluss = Nd4j.zeros(predictedScores.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(predictedScores.shape()).addi(mMinus); INDArray gammaDivNumCorrectLabels = Transforms.max(labels.sum(1), 1).rdivi(gamma); INDArray positiveExamples = predictedScores.mul(labels); INDArray leftHandExp = Transforms.exp((positiveExamples.sum(1).rsubi(mPlus)).muli(gammaDivNumCorrectLabels)); INDArray leftHandNumerator = leftHandExp.mulColumnVector(gammaDivNumCorrectLabels); INDArray leftHandDenominator = leftHandExp.addi(1); INDArray leftHand = leftHandNumerator.divi(leftHandDenominator); leftHand = labels.mulColumnVector(leftHand.negi()); INDArray rightHandExp = Transforms.exp((mMinuss.addi(predictedScores)).muli(gamma)); INDArray rightHandNumerator = rightHandExp.mul(gamma); INDArray rightHandDenominator = rightHandExp.addi(1); INDArray rightHand = rightHandNumerator.divi(rightHandDenominator); predictedScores = predictedScores.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNegative = predictedScores.argMax(1); INDArray negWithHighestScoreMask = Nd4j.zeros(predictedScores.shape()); for(int i = 0; i < maxNegative.length(); i++) { int index = maxNegative.getInt(i); negWithHighestScoreMask.put(i, index, 1); } leftHand = leftHand.muli(labels); rightHand = rightHand.muli(negWithHighestScoreMask); rightHand = rightHand.mulColumnVector(Transforms.min(labels.sum(1),1)); return leftHand.addi(rightHand); } MultiClassDosSantosPairwiseRankingLoss();  MultiClassDosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  MultiClassDosSantosPairwiseRankingLoss(double gamma, double mPlus, double mMinus, double positiveClassExclusionFactor); INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask); @Override double computeScore(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask, boolean average); @Override INDArray computeScoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask); @Override INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask); INDArray computeDlDx(INDArray labels, INDArray predictedScores); @Override Pair<Double, INDArray> computeGradientAndScore(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask, boolean average); @Override String name(); Number getGamma(); void setGamma(Number gamma); Number getmPlus(); void setmPlus(Number mPlus); Number getmMinus(); void setmMinus(Number mMinus); Number getPositiveClassExclusionFactor(); void setPositiveClassExclusionFactor(Number positiveClassExclusionFactor);  }