LossFunctions { public static double score(INDArray labels,LossFunction lossFunction,INDArray output,double l2,boolean useRegularization) { assert !Nd4j.hasInvalidNumber(output) : "Invalid output on labels. Must not contain nan or infinite numbers."; double ret = 0.0f; double reg = 0.5 * l2; INDArray z = output; assert labels.length() == output.length() : "Output and labels must be same length"; switch (lossFunction) { case RECONSTRUCTION_CROSSENTROPY: INDArray xEntLogZ2 = Transforms.log(z.dup()); INDArray xEntOneMinusLabelsOut2 = labels.rsub(1); INDArray xEntOneMinusLogOneMinusZ2 = Transforms.log(z).rsubi(1); ret = - labels.mul(xEntLogZ2).add(xEntOneMinusLabelsOut2).mul(xEntOneMinusLogOneMinusZ2).sum(1).sum(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case MCXENT: INDArray columnSums = labels.mul(log(z)); ret = - columnSums.mean(1).mean(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case XENT: INDArray xEntLogZ = log(z); INDArray xEntOneMinusLabelsOut = labels.rsub(1); INDArray xEntOneMinusLogOneMinusZ = log(z).rsubi(1); ret = -labels.mul(xEntLogZ).add(xEntOneMinusLabelsOut).mul(xEntOneMinusLogOneMinusZ).sum(1).sum(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case RMSE_XENT: INDArray rmseXentDiff = labels.sub(z); INDArray squaredrmseXentDiff = pow(rmseXentDiff,2); INDArray sqrt = sqrt(squaredrmseXentDiff); ret = sqrt.sum(1).sum(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case MSE: INDArray mseDelta = labels.sub(z); ret = 0.5f * pow(mseDelta, 2).sum(1).sum(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case EXPLL: INDArray expLLLogZ = log(z); ret = - z.sub(labels.mul(expLLLogZ)).sum(1).sum(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case SQUARED_LOSS: ret = pow(labels.sub(z), 2).sum(1).sum(Integer.MAX_VALUE).getDouble(0) / (double) labels.rows(); break; case NEGATIVELOGLIKELIHOOD: ret = - Nd4j.mean(Nd4j.sum( labels.mul(log(z)) .addi(labels.rsub(1).muli(log(z.rsub(1)))) ,1)).getDouble(0) / (double) labels.rows(); break; } return ret; }  static double score(INDArray labels,LossFunction lossFunction,INDArray output,double l2,boolean useRegularization); static double reconEntropy(INDArray input,INDArray hBias,INDArray vBias,INDArray W,ActivationFunction activationFunction);  }
@Test public void testRMseXent() { INDArray in = Nd4j.create(new float[][]{{1, 2}, {3, 4}}); INDArray out = Nd4j.create(new float[][]{{5, 6}, {7, 8}}); double diff = LossFunctions.score(in, LossFunctions.LossFunction.RMSE_XENT,out,0,false); assertEquals(8,diff,1e-1); }
@Test public void testMcXent() { INDArray in = Nd4j.create(new float[][]{{1, 2}, {3, 4}}); INDArray out = Nd4j.create(new float[][]{{5, 6}, {7, 8}}); LossFunctions.score(in, LossFunctions.LossFunction.MCXENT,out,0,false); }
@Test public void testNegativeLogLikelihood() { INDArray softmax = Nd4j.create(new double[][]{{0.6,0.4},{0.7,0.3}}); INDArray trueLabels = Nd4j.create(new double[][]{{1,0},{0,1}}); double score = LossFunctions.score(trueLabels, LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD,softmax,0,false); assertEquals(1.71479842809,score,1e-1); INDArray softmax2 = Nd4j.create(new double[][]{{0.33,0.33,0.33},{0.33,0.33,0.33}}); INDArray trueLabels2 = Nd4j.create(new double[][]{{1,0,0},{1,0,0}}); double score2 = LossFunctions.score(trueLabels2, LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD,softmax2,0,false); assertEquals(1.90961775772,score2,1e-1); }