Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#mulColumnVector()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#mulColumnVector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LossCosineProximity.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
    if (labels.size(1) != preOutput.size(1)) {
        throw new IllegalArgumentException(
                        "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
                                        + " number of outputs (nOut = " + preOutput.size(1) + ") ");

    }
    INDArray yhat = activationFn.getActivation(preOutput.dup(), true);
    INDArray yL2norm = labels.norm2(1);

    INDArray yhatL2norm = yhat.norm2(1);
    INDArray yhatL2normSq = yhatL2norm.mul(yhatL2norm);

    //Note: This is not really the L1 norm since I am not taking abs values
    INDArray yhatDotyL1norm = labels.mul(yhat).sum(1);

    INDArray dLda = labels.mulColumnVector(yhatL2normSq);
    dLda.subi(yhat.mulColumnVector(yhatDotyL1norm));

    // transform vals to avoid nans before div
    yL2norm = Transforms.max(yL2norm, Nd4j.EPS_THRESHOLD, false);
    yhatL2norm = Transforms.max(yhatL2norm, Nd4j.EPS_THRESHOLD, false);
    yhatL2normSq = Transforms.max(yhatL2normSq, Nd4j.EPS_THRESHOLD, false);

    dLda.diviColumnVector(yL2norm);
    dLda.diviColumnVector(yhatL2norm.mul(yhatL2normSq));
    dLda.muli(-1);

    //dL/dz
    INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO loss functions with params

    if (mask != null) {
        gradients.muliColumnVector(mask);
    }

    return gradients;
}
 
Example 2
Source File: ReductionBpOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNorm1AlongDimensionBP() {
    //dL/dIn = dL/dOut * dOut/dIn
    //       = dL/dOut * sgn(in)

    for (boolean keepDims : new boolean[]{false, true}) {

        long[] reducedShape_0 = (keepDims ? new long[]{1, 4} : new long[]{4});
        INDArray preReduceInput = Nd4j.linspace(-5, 6, 12).addi(0.1).reshape(3, 4);
        INDArray sgn = Transforms.sign(preReduceInput, true);
        INDArray dLdOut_0 = Nd4j.create(new double[]{1, 2, 3, 4}, reducedShape_0);
        INDArray dLdInExpected_0 = sgn.mulRowVector(dLdOut_0);

        INDArray dLdIn = Nd4j.createUninitialized(3, 4);

        String err = OpValidation.validate(new OpTestCase(new Norm1Bp(preReduceInput, dLdOut_0, dLdIn, keepDims, 0))
                .expectedOutput(0, dLdInExpected_0));
        assertNull(err, err);


        long[] reducedShape_1 = (keepDims ? new long[]{3, 1} : new long[]{3});
        INDArray dLdOut_1 = Nd4j.create(new double[]{1, 2, 3}, reducedShape_1);
        INDArray dLdInExpected_1 = sgn.mulColumnVector(dLdOut_1);
        dLdIn = Nd4j.createUninitialized(3, 4);

        err = OpValidation.validate(new OpTestCase(new Norm1Bp(preReduceInput, dLdOut_1, dLdIn, keepDims, 1))
                .expectedOutput(0, dLdInExpected_1));

        assertNull(err, err);
    }
}
 
Example 3
Source File: LossCosineProximity.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
    if(!labels.equalShapes(preOutput)){
        Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape());
    }
    labels = labels.castTo(preOutput.dataType());   //No-op if already correct dtype
    INDArray yhat = activationFn.getActivation(preOutput.dup(), true);
    INDArray yL2norm = labels.norm2(1);

    INDArray yhatL2norm = yhat.norm2(1);
    INDArray yhatL2normSq = yhatL2norm.mul(yhatL2norm);

    //Note: This is not really the L1 norm since I am not taking abs values
    INDArray yhatDotyL1norm = labels.mul(yhat).sum(true,1);

    INDArray dLda = labels.mulColumnVector(yhatL2normSq);
    dLda.subi(yhat.mulColumnVector(yhatDotyL1norm));

    // transform vals to avoid nans before div
    yL2norm = Transforms.max(yL2norm, Nd4j.EPS_THRESHOLD, false);
    yhatL2norm = Transforms.max(yhatL2norm, Nd4j.EPS_THRESHOLD, false);
    yhatL2normSq = Transforms.max(yhatL2normSq, Nd4j.EPS_THRESHOLD, false);

    dLda.diviColumnVector(yL2norm);
    dLda.diviColumnVector(yhatL2norm.mul(yhatL2normSq));
    dLda.muli(-1);

    //dL/dz
    INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO loss functions with params

    if (mask != null) {
        gradients.muliColumnVector(mask);
    }

    return gradients;
}
 
Example 4
Source File: LossFMeasure.java    From nd4j with Apache License 2.0 4 votes vote down vote up
private double[] computeScoreNumDenom(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask,
                boolean average) {
    INDArray output = activationFn.getActivation(preOutput.dup(), true);

    long n = labels.size(1);
    if (n != 1 && n != 2) {
        throw new UnsupportedOperationException(
                        "For binary classification: expect output size of 1 or 2. Got: " + n);
    }

    //First: determine positives and negatives
    INDArray isPositiveLabel;
    INDArray isNegativeLabel;
    INDArray pClass0;
    INDArray pClass1;
    if (n == 1) {
        isPositiveLabel = labels;
        isNegativeLabel = Transforms.not(isPositiveLabel);
        pClass0 = output.rsub(1.0);
        pClass1 = output;
    } else {
        isPositiveLabel = labels.getColumn(1);
        isNegativeLabel = labels.getColumn(0);
        pClass0 = output.getColumn(0);
        pClass1 = output.getColumn(1);
    }

    if (mask != null) {
        isPositiveLabel = isPositiveLabel.mulColumnVector(mask);
        isNegativeLabel = isNegativeLabel.mulColumnVector(mask);
    }

    double tp = isPositiveLabel.mul(pClass1).sumNumber().doubleValue();
    double fp = isNegativeLabel.mul(pClass1).sumNumber().doubleValue();
    double fn = isPositiveLabel.mul(pClass0).sumNumber().doubleValue();

    double numerator = (1.0 + beta * beta) * tp;
    double denominator = (1.0 + beta * beta) * tp + beta * beta * fn + fp;

    return new double[] {numerator, denominator};
}
 
Example 5
Source File: LossFMeasure.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private double[] computeScoreNumDenom(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask,
                boolean average) {
    INDArray output = activationFn.getActivation(preOutput.dup(), true);

    long n = labels.size(1);
    if (n != 1 && n != 2) {
        throw new UnsupportedOperationException(
                        "For binary classification: expect output size of 1 or 2. Got: " + n);
    }

    //First: determine positives and negatives
    INDArray isPositiveLabel;
    INDArray isNegativeLabel;
    INDArray pClass0;
    INDArray pClass1;
    if (n == 1) {
        isPositiveLabel = labels;
        isNegativeLabel = isPositiveLabel.rsub(1.0);
        pClass0 = output.rsub(1.0);
        pClass1 = output;
    } else {
        isPositiveLabel = labels.getColumn(1);
        isNegativeLabel = labels.getColumn(0);
        pClass0 = output.getColumn(0);
        pClass1 = output.getColumn(1);
    }

    if (mask != null) {
        isPositiveLabel = isPositiveLabel.mulColumnVector(mask);
        isNegativeLabel = isNegativeLabel.mulColumnVector(mask);
    }

    double tp = isPositiveLabel.mul(pClass1).sumNumber().doubleValue();
    double fp = isNegativeLabel.mul(pClass1).sumNumber().doubleValue();
    double fn = isPositiveLabel.mul(pClass0).sumNumber().doubleValue();

    double numerator = (1.0 + beta * beta) * tp;
    double denominator = (1.0 + beta * beta) * tp + beta * beta * fn + fp;

    return new double[] {numerator, denominator};
}
 
Example 6
Source File: TestGraphNodes.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testL2Node() {
    Nd4j.getRandom().setSeed(12345);
    GraphVertex l2 = new L2Vertex(null, "", -1, 1e-8, Nd4j.dataType());

    INDArray in1 = Nd4j.rand(5, 2);
    INDArray in2 = Nd4j.rand(5, 2);

    l2.setInputs(in1, in2);
    INDArray out = l2.doForward(false, LayerWorkspaceMgr.noWorkspaces());

    INDArray expOut = Nd4j.create(5, 1);
    for (int i = 0; i < 5; i++) {
        double d2 = 0.0;
        for (int j = 0; j < in1.size(1); j++) {
            double temp = (in1.getDouble(i, j) - in2.getDouble(i, j));
            d2 += temp * temp;
        }
        d2 = Math.sqrt(d2);
        expOut.putScalar(i, 0, d2);
    }

    assertEquals(expOut, out);



    INDArray epsilon = Nd4j.rand(5, 1); //dL/dlambda
    INDArray diff = in1.sub(in2);
    //Out == sqrt(s) = s^1/2. Therefore: s^(-1/2) = 1/out
    INDArray sNegHalf = out.rdiv(1.0);

    INDArray dLda = diff.mulColumnVector(epsilon.mul(sNegHalf));
    INDArray dLdb = diff.mulColumnVector(epsilon.mul(sNegHalf)).neg();



    l2.setEpsilon(epsilon);
    Pair<Gradient, INDArray[]> p = l2.doBackward(false, LayerWorkspaceMgr.noWorkspaces());
    assertEquals(dLda, p.getSecond()[0]);
    assertEquals(dLdb, p.getSecond()[1]);
}