LossFunctions { public static double reconEntropy(INDArray input,INDArray hBias,INDArray vBias,INDArray W,ActivationFunction activationFunction) { INDArray preSigH = input.mmul(W).addRowVector(hBias); INDArray sigH = activationFunction.apply(preSigH); assert !Nd4j.hasInvalidNumber(sigH); INDArray preSigV = sigH.mmul(W.transpose()).addRowVector(vBias); INDArray sigV = activationFunction.apply(preSigV); assert !Nd4j.hasInvalidNumber(sigH); INDArray inner = input.mul(log(sigV)) .add(input.rsub(1) .mul(log(sigV.rsub(1)))); INDArray rows = inner.sum(1); INDArray mean = rows.mean(Integer.MAX_VALUE); double ret = mean.getDouble(0); ret /= input.rows(); 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 testReconEntropy() { Nd4j.factory().setOrder('f'); INDArray input = Nd4j.create( new double[]{1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, new int[]{7, 6}); INDArray w = Nd4j.create( new double[]{-0.005221025740321007, -0.0025006434506737304, -0.013585431005440437, -0.021996946700655134, 0.007678447599654643, -0.0037941287958231052, -0.014933056402715545, -0.012875289265542541, 0.001635482018910717, 0.00893829162129914, 0.017003519496588012, -0.004271078749979736, 0.0015816435136811352, 0.008638074705740708, -0.004393004605647038, -0.006249587919004255, -0.011017655538216209, -0.0015862988109404338, 0.01079760516931169, -0.0010491291520692704, 0.006626023289526534, 0.004658989751677583, -0.0022132443508813535, -0.00979834812384658}, new int[]{6, 4}); INDArray vBias = Nd4j.create( new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, new int[]{6}); INDArray hBias = Nd4j.create( new double[]{0.0, 0.0, 0.0, 0.0}, new int[]{4}); double reconEntropy = LossFunctions.reconEntropy(input,hBias,vBias,w, Activations.sigmoid()); double assertion = -0.5937198421625942; assertEquals(assertion,reconEntropy,1e-1); }