@Test public void correctScoreResult() 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, 4); preOutput.put(0, 1, 0); preOutput.put(0, 2, -1); preOutput.put(1, 0, 4); preOutput.put(1, 1, -1); preOutput.put(1, 2, 0); INDArray mask = Nd4j.ones(2, 1); IActivation tanH = new ActivationTanH(); INDArray scoreArray = rankingLoss.scoreArray(label, preOutput, tanH, mask); double score = scoreArray.sumNumber().doubleValue(); assertThat(score, is(closeTo(8.726d, 0.05d))); }
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExamples = output.mul(labels); positiveExamples = averageOverCorrectClasses(labels, positiveExamples); INDArray positiveExp = positiveExamples.rsubi(mPlus).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(Transforms.min(labels.sum(1),1)); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog.sum(1)); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExamples = output.mul(labels); positiveExamples = averageOverCorrectClasses(labels, positiveExamples); INDArray positiveExp = positiveExamples.rsubi(mPlus).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(Transforms.min(labels.sum(1),1)); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog.sum(1)); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExamples = output.mul(labels); positiveExamples = averageOverCorrectClasses(labels, positiveExamples); INDArray positiveExp = positiveExamples.rsubi(mPlus).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(Transforms.min(labels.sum(1),1)); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog.sum(1)); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } MultiClassDosSantosPairwiseRankingLoss();  MultiClassDosSantosPairwiseRankingLoss(Number gamma, Number mPlus, Number mMinus);  MultiClassDosSantosPairwiseRankingLoss(double gamma, double mPlus, double mMinus, double positiveClassExclusionFactor); }
MultiClassDosSantosPairwiseRankingLoss implements ILossFunction { public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExamples = output.mul(labels); positiveExamples = averageOverCorrectClasses(labels, positiveExamples); INDArray positiveExp = positiveExamples.rsubi(mPlus).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(Transforms.min(labels.sum(1),1)); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog.sum(1)); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } 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 scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) { INDArray scoreArr; INDArray output = activationFn.getActivation(preOutput.dup(), true); INDArray mMinuss = Nd4j.zeros(output.shape()).addi(mMinus); INDArray positiveExamples = output.mul(labels); positiveExamples = averageOverCorrectClasses(labels, positiveExamples); INDArray positiveExp = positiveExamples.rsubi(mPlus).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(Transforms.min(labels.sum(1),1)); rightLog = rightLog.muli(negWithHighestScore); scoreArr = leftLog.addi(rightLog.sum(1)); if(mask != null) { scoreArr.muliColumnVector(mask); } return scoreArr; } 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);  }