@Test public void correctScoreResultForBadPerformingSample() throws Exception { INDArray label = Nd4j.create(1, 2); label.put(0, 0, 1); label.put(0, 1, 0); INDArray preOutput = Nd4j.create(label.shape()); preOutput.put(0, 0, 0); preOutput.put(0, 1, 4); INDArray mask = Nd4j.ones(1,1); IActivation tanH = new ActivationTanH(); DosSantosPairwiseRankingLoss rankingLoss = new DosSantosPairwiseRankingLoss(); INDArray scoreArray = rankingLoss.scoreArray(label, preOutput, tanH, mask); double score = scoreArray.sumNumber().doubleValue(); assertThat(scoreArray.getDouble(0,0), is(closeTo(5.01d, 0.05d))); assertThat(scoreArray.getDouble(0,1), is(closeTo(3.05d, 0.05d))); assertThat(score, is(closeTo(8.055d, 0.05d))); }
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mPluss = Nd4j.zeros(output.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExp = mPluss.subi(output).muli(gamma); INDArray positiveE = Transforms.exp(positiveExp); INDArray leftLog = Transforms.log(positiveE.addi(1)); INDArray negativeExp = mMinuss.addi(output).muli(gamma); INDArray negativeE = Transforms.exp(negativeExp); INDArray negativeExamples = output.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNeg = negativeExamples.argMax(1); INDArray rightLog = Transforms.log(negativeE.addi(1)); INDArray negWithHighestScore = Nd4j.zeros(preOutput.shape()); for(int i = 0; i < maxNeg.length(); i++) { int index = maxNeg.getInt(i); negWithHighestScore.put(i, index, 1); } leftLog = leftLog.muli(labels); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; }
DosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mPluss = Nd4j.zeros(output.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExp = mPluss.subi(output).muli(gamma); INDArray positiveE = Transforms.exp(positiveExp); INDArray leftLog = Transforms.log(positiveE.addi(1)); INDArray negativeExp = mMinuss.addi(output).muli(gamma); INDArray negativeE = Transforms.exp(negativeExp); INDArray negativeExamples = output.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNeg = negativeExamples.argMax(1); INDArray rightLog = Transforms.log(negativeE.addi(1)); INDArray negWithHighestScore = Nd4j.zeros(preOutput.shape()); for(int i = 0; i < maxNeg.length(); i++) { int index = maxNeg.getInt(i); negWithHighestScore.put(i, index, 1); } leftLog = leftLog.muli(labels); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } }
DosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mPluss = Nd4j.zeros(output.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExp = mPluss.subi(output).muli(gamma); INDArray positiveE = Transforms.exp(positiveExp); INDArray leftLog = Transforms.log(positiveE.addi(1)); INDArray negativeExp = mMinuss.addi(output).muli(gamma); INDArray negativeE = Transforms.exp(negativeExp); INDArray negativeExamples = output.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNeg = negativeExamples.argMax(1); INDArray rightLog = Transforms.log(negativeE.addi(1)); INDArray negWithHighestScore = Nd4j.zeros(preOutput.shape()); for(int i = 0; i < maxNeg.length(); i++) { int index = maxNeg.getInt(i); negWithHighestScore.put(i, index, 1); } leftLog = leftLog.muli(labels); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } DosSantosPairwiseRankingLoss();  DosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  DosSantosPairwiseRankingLoss(int gamma, int mPlus, int mMinus, int positiveClassExclusionFactor); }
DosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mPluss = Nd4j.zeros(output.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExp = mPluss.subi(output).muli(gamma); INDArray positiveE = Transforms.exp(positiveExp); INDArray leftLog = Transforms.log(positiveE.addi(1)); INDArray negativeExp = mMinuss.addi(output).muli(gamma); INDArray negativeE = Transforms.exp(negativeExp); INDArray negativeExamples = output.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNeg = negativeExamples.argMax(1); INDArray rightLog = Transforms.log(negativeE.addi(1)); INDArray negWithHighestScore = Nd4j.zeros(preOutput.shape()); for(int i = 0; i < maxNeg.length(); i++) { int index = maxNeg.getInt(i); negWithHighestScore.put(i, index, 1); } leftLog = leftLog.muli(labels); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } DosSantosPairwiseRankingLoss();  DosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  DosSantosPairwiseRankingLoss(int gamma, int mPlus, int mMinus, int 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); }
DosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mPluss = Nd4j.zeros(output.shape()).addi(mPlus); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExp = mPluss.subi(output).muli(gamma); INDArray positiveE = Transforms.exp(positiveExp); INDArray leftLog = Transforms.log(positiveE.addi(1)); INDArray negativeExp = mMinuss.addi(output).muli(gamma); INDArray negativeE = Transforms.exp(negativeExp); INDArray negativeExamples = output.addi(labels.mul(positiveClassExclusionFactor)); INDArray maxNeg = negativeExamples.argMax(1); INDArray rightLog = Transforms.log(negativeE.addi(1)); INDArray negWithHighestScore = Nd4j.zeros(preOutput.shape()); for(int i = 0; i < maxNeg.length(); i++) { int index = maxNeg.getInt(i); negWithHighestScore.put(i, index, 1); } leftLog = leftLog.muli(labels); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } DosSantosPairwiseRankingLoss();  DosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  DosSantosPairwiseRankingLoss(int gamma, int mPlus, int mMinus, int 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);  }