Java Code Examples for org.nd4j.linalg.ops.transforms.Transforms#min()

The following examples show how to use org.nd4j.linalg.ops.transforms.Transforms#min() . 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: SameDiffTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJaccardDistance() {
    Nd4j.getRandom().setSeed(12345);

    INDArray a = Nd4j.rand(new long[]{3, 4}).addi(0.1);
    INDArray b = Nd4j.rand(new long[]{3, 4}).addi(0.1);

    SameDiff sd = SameDiff.create();
    SDVariable in1 = sd.var("in1", a);
    SDVariable in2 = sd.var("in2", b);

    SDVariable jaccard = sd.jaccardDistance("out", in1, in2);

    INDArray min = Transforms.min(a, b);
    INDArray max = Transforms.max(a, b);

    double minSum = min.sumNumber().doubleValue();
    double maxSum = max.sumNumber().doubleValue();
    double jd = 1.0 - minSum / maxSum;

    INDArray out = sd.execAndEndResult();
    assertEquals(1, out.length());

    assertEquals(jd, out.getDouble(0), 1e-6);
}
 
Example 2
Source File: TransformsTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testScalarMinMax1() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray xCopy = x.dup();
    INDArray exp1 = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray exp2 = Nd4j.create(new double[] {1e-5, 1e-5, 1e-5, 1e-5});

    INDArray z1 = Transforms.max(x, Nd4j.EPS_THRESHOLD, true);
    INDArray z2 = Transforms.min(x, Nd4j.EPS_THRESHOLD, true);

    assertEquals(exp1, z1);
    assertEquals(exp2, z2);
    // Assert that x was not modified
    assertEquals(x, xCopy);

    INDArray exp3 = Nd4j.create(new double[] {10, 10, 10, 10});
    Transforms.max(x, 10, false);
    assertEquals(x, exp3);

    Transforms.min(x, Nd4j.EPS_THRESHOLD, false);
    assertEquals(x, exp2);
}
 
Example 3
Source File: LossKLD.java    From nd4j with Apache License 2.0 6 votes vote down vote up
private INDArray scoreArray(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 output = activationFn.getActivation(preOutput.dup(), true);

    // Clip output and labels to be between Nd4j.EPS_THREsHOLD and 1, i.e. a valid non-zero probability
    output = Transforms.min(Transforms.max(output, Nd4j.EPS_THRESHOLD, false), 1, false);
    labels = Transforms.min(Transforms.max(labels, Nd4j.EPS_THRESHOLD, true), 1, false);

    INDArray logRatio = Transforms.log(output.rdivi(labels), false);

    INDArray scoreArr = logRatio.muli(labels);
    if (mask != null) {
        LossUtil.applyMask(scoreArr, mask);
    }
    return scoreArr;
}
 
Example 4
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJaccardDistance() {
    Nd4j.getRandom().setSeed(12345);

    INDArray a = Nd4j.rand(new long[]{3, 4}).addi(0.1);
    INDArray b = Nd4j.rand(new long[]{3, 4}).addi(0.1);

    SameDiff sd = SameDiff.create();
    SDVariable in1 = sd.var("in1", a);
    SDVariable in2 = sd.var("in2", b);

    SDVariable jaccard = sd.math().jaccardDistance("out", in1, in2);

    INDArray min = Transforms.min(a, b);
    INDArray max = Transforms.max(a, b);

    double minSum = min.sumNumber().doubleValue();
    double maxSum = max.sumNumber().doubleValue();
    double jd = 1.0 - minSum / maxSum;

    INDArray out = jaccard.eval();
    assertEquals(1, out.length());

    assertEquals(jd, out.getDouble(0), 1e-6);
}
 
Example 5
Source File: TransformsTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testScalarMinMax1() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray xCopy = x.dup();
    INDArray exp1 = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray exp2 = Nd4j.create(new double[] {1e-5, 1e-5, 1e-5, 1e-5});

    INDArray z1 = Transforms.max(x, Nd4j.EPS_THRESHOLD, true);
    INDArray z2 = Transforms.min(x, Nd4j.EPS_THRESHOLD, true);

    assertEquals(exp1, z1);
    assertEquals(exp2, z2);
    // Assert that x was not modified
    assertEquals(x, xCopy);

    INDArray exp3 = Nd4j.create(new double[] {10, 10, 10, 10});
    Transforms.max(x, 10, false);
    assertEquals(exp3, x);

    Transforms.min(x, Nd4j.EPS_THRESHOLD, false);
    assertEquals(exp2, x);
}
 
Example 6
Source File: LossKLD.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private INDArray scoreArray(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 output = activationFn.getActivation(preOutput.dup(), true);

    // Clip output and labels to be between Nd4j.EPS_THREsHOLD and 1, i.e. a valid non-zero probability
    output = Transforms.min(Transforms.max(output, Nd4j.EPS_THRESHOLD, false), 1, false);
    labels = Transforms.min(Transforms.max(labels, Nd4j.EPS_THRESHOLD, true), 1, false);

    INDArray logRatio = Transforms.log(output.rdivi(labels), false);

    INDArray scoreArr = logRatio.muli(labels);
    if (mask != null) {
        LossUtil.applyMask(scoreArr, mask);
    }
    return scoreArr;
}
 
Example 7
Source File: TransformsTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayMinMax() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray y = Nd4j.create(new double[] {2, 2, 6, 6});
    INDArray xCopy = x.dup();
    INDArray yCopy = y.dup();
    INDArray expMax = Nd4j.create(new double[] {2, 3, 6, 7});
    INDArray expMin = Nd4j.create(new double[] {1, 2, 5, 6});

    INDArray z1 = Transforms.max(x, y, true);
    INDArray z2 = Transforms.min(x, y, true);

    assertEquals(expMax, z1);
    assertEquals(expMin, z2);
    // Assert that x was not modified
    assertEquals(xCopy, x);

    Transforms.max(x, y, false);
    // Assert that x was modified
    assertEquals(expMax, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);

    // Reset the modified x
    x = xCopy.dup();

    Transforms.min(x, y, false);
    // Assert that X was modified
    assertEquals(expMin, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);
}
 
Example 8
Source File: MinMaxStats.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Add rows of data to the statistics
 *
 * @param data the matrix containing multiple rows of data to include
 * @param mask (optionally) the mask of the data, useful for e.g. time series
 */
public MinMaxStats.Builder add(@NonNull INDArray data, INDArray mask) {
    data = DataSetUtil.tailor2d(data, mask);
    if (data == null) {
        // Nothing to add. Either data is empty or completely masked. Just skip it, otherwise we will get
        // null pointer exceptions.
        return this;
    }

    INDArray tad = data.javaTensorAlongDimension(0, 0);
    INDArray batchMin = data.min(0);
    INDArray batchMax = data.max(0);
    if (!Arrays.equals(batchMin.shape(), batchMax.shape()))
        throw new IllegalStateException(
                        "Data min and max must be same shape. Likely a bug in the operation changing the input?");
    if (runningLower == null) {
        // First batch
        // Create copies because min and max are views to the same data set, which will cause problems with the
        // side effects of Transforms.min and Transforms.max
        runningLower = batchMin.dup();
        runningUpper = batchMax.dup();
    } else {
        // Update running bounds
        Transforms.min(runningLower, batchMin, false);
        Transforms.max(runningUpper, batchMax, false);
    }

    return this;
}
 
Example 9
Source File: TransformsTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayMinMax() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray y = Nd4j.create(new double[] {2, 2, 6, 6});
    INDArray xCopy = x.dup();
    INDArray yCopy = y.dup();
    INDArray expMax = Nd4j.create(new double[] {2, 3, 6, 7});
    INDArray expMin = Nd4j.create(new double[] {1, 2, 5, 6});

    INDArray z1 = Transforms.max(x, y, true);
    INDArray z2 = Transforms.min(x, y, true);

    assertEquals(expMax, z1);
    assertEquals(expMin, z2);
    // Assert that x was not modified
    assertEquals(xCopy, x);

    Transforms.max(x, y, false);
    // Assert that x was modified
    assertEquals(expMax, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);

    // Reset the modified x
    x = xCopy.dup();

    Transforms.min(x, y, false);
    // Assert that X was modified
    assertEquals(expMin, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);
}
 
Example 10
Source File: MinMaxStats.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Add rows of data to the statistics
 *
 * @param data the matrix containing multiple rows of data to include
 * @param mask (optionally) the mask of the data, useful for e.g. time series
 */
public MinMaxStats.Builder add(@NonNull INDArray data, INDArray mask) {
    data = DataSetUtil.tailor2d(data, mask);
    if (data == null) {
        // Nothing to add. Either data is empty or completely masked. Just skip it, otherwise we will get
        // null pointer exceptions.
        return this;
    }

    INDArray batchMin = data.min(0).reshape(1, data.size(1));
    INDArray batchMax = data.max(0).reshape(1, data.size(1));
    if (!Arrays.equals(batchMin.shape(), batchMax.shape()))
        throw new IllegalStateException(
                        "Data min and max must be same shape. Likely a bug in the operation changing the input?");
    if (runningLower == null) {
        // First batch
        // Create copies because min and max are views to the same data set, which will cause problems with the
        // side effects of Transforms.min and Transforms.max
        runningLower = batchMin.dup();
        runningUpper = batchMax.dup();
    } else {
        // Update running bounds
        Transforms.min(runningLower, batchMin, false);
        Transforms.max(runningUpper, batchMax, false);
    }

    return this;
}
 
Example 11
Source File: MtcnnUtil.java    From mtcnn-java with Apache License 2.0 4 votes vote down vote up
/**
 * Non Maximum Suppression - greedily selects the boxes with high confidence. Keep the boxes that have overlap area
 * below the threshold and discards the others.
 *
 * original code:
 *  - https://github.com/kpzhang93/MTCNN_face_detection_alignment/blob/master/code/codes/MTCNNv2/nms.m
 *  - https://github.com/davidsandberg/facenet/blob/master/src/align/detect_face.py#L687
 *
 * @param boxes nd array with bounding boxes: [[x1, y1, x2, y2 score]]
 * @param threshold NMS threshold -  retain overlap <= thresh
 * @param nmsType NMS method to apply. Available values ('Min', 'Union')
 * @return Returns the NMS result
 */
public static INDArray nonMaxSuppression(INDArray boxes, double threshold, NonMaxSuppressionType nmsType) {

	if (boxes.isEmpty()) {
		return Nd4j.empty();
	}

	// TODO Try to prevent following duplications!
	INDArray x1 = boxes.get(all(), point(0)).dup();
	INDArray y1 = boxes.get(all(), point(1)).dup();
	INDArray x2 = boxes.get(all(), point(2)).dup();
	INDArray y2 = boxes.get(all(), point(3)).dup();
	INDArray s = boxes.get(all(), point(4)).dup();

	//area = (x2 - x1 + 1) * (y2 - y1 + 1)
	INDArray area = (x2.sub(x1).add(1)).mul(y2.sub(y1).add(1));

	// sorted_s = np.argsort(s)
	INDArray sortedS = Nd4j.sortWithIndices(s, 0, SORT_ASCENDING)[0];

	INDArray pick = Nd4j.zerosLike(s);
	int counter = 0;

	while (sortedS.size(0) > 0) {

		if (sortedS.size(0) == 1) {
			pick.put(counter++, sortedS.dup());
			break;
		}

		long lastIndex = sortedS.size(0) - 1;
		INDArray i = sortedS.get(point(lastIndex), all()); // last element
		INDArray idx = sortedS.get(interval(0, lastIndex), all()).transpose(); // all until last excluding
		pick.put(counter++, i.dup());

		INDArray xx1 = Transforms.max(x1.get(idx), x1.get(i).getInt(0));
		INDArray yy1 = Transforms.max(y1.get(idx), y1.get(i).getInt(0));
		INDArray xx2 = Transforms.min(x2.get(idx), x2.get(i).getInt(0));
		INDArray yy2 = Transforms.min(y2.get(idx), y2.get(i).getInt(0));

		// w = np.maximum(0.0, xx2 - xx1 + 1)
		// h = np.maximum(0.0, yy2 - yy1 + 1)
		// inter = w * h
		INDArray w = Transforms.max(xx2.sub(xx1).add(1), 0.0f);
		INDArray h = Transforms.max(yy2.sub(yy1).add(1), 0.0f);
		INDArray inter = w.mul(h);

		// if method is 'Min':
		//   o = inter / np.minimum(area[i], area[idx])
		// else:
		//   o = inter / (area[i] + area[idx] - inter)
		int areaI = area.get(i).getInt(0);
		INDArray o = (nmsType == NonMaxSuppressionType.Min) ?
				inter.div(Transforms.min(area.get(idx), areaI)) :
				inter.div(area.get(idx).add(areaI).sub(inter));

		INDArray oIdx = MtcnnUtil.getIndexWhereVector(o, value -> value <= threshold);
		//INDArray oIdx = getIndexWhereVector2(o, Conditions.lessThanOrEqual(threshold));

		if (oIdx.isEmpty()) {
			break;
		}

		sortedS = Nd4j.expandDims(sortedS.get(oIdx), 0).transpose();
	}

	//pick = pick[0:counter]
	return (counter == 0) ? Nd4j.empty() : pick.get(interval(0, counter));
}
 
Example 12
Source File: NDArrayScalarOpTransform.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public NDArrayWritable map(Writable w) {
    if (!(w instanceof NDArrayWritable)) {
        throw new IllegalArgumentException("Input writable is not an NDArrayWritable: is " + w.getClass());
    }

    //Make a copy - can't always assume that the original INDArray won't be used again in the future
    NDArrayWritable n = ((NDArrayWritable) w);
    INDArray a = n.get().dup();
    switch (mathOp) {
        case Add:
            a.addi(scalar);
            break;
        case Subtract:
            a.subi(scalar);
            break;
        case Multiply:
            a.muli(scalar);
            break;
        case Divide:
            a.divi(scalar);
            break;
        case Modulus:
            throw new UnsupportedOperationException(mathOp + " is not supported for NDArrayWritable");
        case ReverseSubtract:
            a.rsubi(scalar);
            break;
        case ReverseDivide:
            a.rdivi(scalar);
            break;
        case ScalarMin:
            Transforms.min(a, scalar, false);
            break;
        case ScalarMax:
            Transforms.max(a, scalar, false);
            break;
        default:
            throw new UnsupportedOperationException("Unknown or not supported op: " + mathOp);
    }

    //To avoid threading issues...
    Nd4j.getExecutioner().commit();

    return new NDArrayWritable(a);
}
 
Example 13
Source File: NDArrayScalarOpTransform.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public NDArrayWritable map(Writable w) {
    if (!(w instanceof NDArrayWritable)) {
        throw new IllegalArgumentException("Input writable is not an NDArrayWritable: is " + w.getClass());
    }

    //Make a copy - can't always assume that the original INDArray won't be used again in the future
    NDArrayWritable n = ((NDArrayWritable) w);
    INDArray a = n.get().dup();
    switch (mathOp) {
        case Add:
            a.addi(scalar);
            break;
        case Subtract:
            a.subi(scalar);
            break;
        case Multiply:
            a.muli(scalar);
            break;
        case Divide:
            a.divi(scalar);
            break;
        case Modulus:
            a.fmodi(scalar);
            break;
        case ReverseSubtract:
            a.rsubi(scalar);
            break;
        case ReverseDivide:
            a.rdivi(scalar);
            break;
        case ScalarMin:
            Transforms.min(a, scalar, false);
            break;
        case ScalarMax:
            Transforms.max(a, scalar, false);
            break;
        default:
            throw new UnsupportedOperationException("Unknown or not supported op: " + mathOp);
    }

    //To avoid threading issues...
    Nd4j.getExecutioner().commit();

    return new NDArrayWritable(a);
}
 
Example 14
Source File: EvalCustomThreshold.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testEvaluationBinaryCustomThreshold() {

    //Sanity check: same results for 0.5 threshold vs. default (no threshold)
    int nExamples = 20;
    int nOut = 2;
    INDArray probs = Nd4j.rand(nExamples, nOut);
    INDArray labels = Nd4j.getExecutioner()
                    .exec(new BernoulliDistribution(Nd4j.createUninitialized(nExamples, nOut), 0.5));

    EvaluationBinary eStd = new EvaluationBinary();
    eStd.eval(labels, probs);

    EvaluationBinary eb05 = new EvaluationBinary(Nd4j.create(new double[] {0.5, 0.5}, new long[]{1,2}));
    eb05.eval(labels, probs);

    EvaluationBinary eb05v2 = new EvaluationBinary(Nd4j.create(new double[] {0.5, 0.5}, new long[]{1,2}));
    for (int i = 0; i < nExamples; i++) {
        eb05v2.eval(labels.getRow(i, true), probs.getRow(i, true));
    }

    for (EvaluationBinary eb2 : new EvaluationBinary[] {eb05, eb05v2}) {
        assertArrayEquals(eStd.getCountTruePositive(), eb2.getCountTruePositive());
        assertArrayEquals(eStd.getCountFalsePositive(), eb2.getCountFalsePositive());
        assertArrayEquals(eStd.getCountTrueNegative(), eb2.getCountTrueNegative());
        assertArrayEquals(eStd.getCountFalseNegative(), eb2.getCountFalseNegative());

        for (int j = 0; j < nOut; j++) {
            assertEquals(eStd.accuracy(j), eb2.accuracy(j), 1e-6);
            assertEquals(eStd.f1(j), eb2.f1(j), 1e-6);
        }
    }


    //Check with decision threshold of 0.25 and 0.125 (for different outputs)
    //In this test, we'll cheat a bit: multiply probabilities by 2 (max of 1.0) and threshold of 0.25 should give
    // an identical result to a threshold of 0.5
    //Ditto for 4x and 0.125 threshold

    INDArray probs2 = probs.mul(2);
    probs2 = Transforms.min(probs2, 1.0);

    INDArray probs4 = probs.mul(4);
    probs4 = Transforms.min(probs4, 1.0);

    EvaluationBinary ebThreshold = new EvaluationBinary(Nd4j.create(new double[] {0.25, 0.125}));
    ebThreshold.eval(labels, probs);

    EvaluationBinary ebStd2 = new EvaluationBinary();
    ebStd2.eval(labels, probs2);

    EvaluationBinary ebStd4 = new EvaluationBinary();
    ebStd4.eval(labels, probs4);

    assertEquals(ebThreshold.truePositives(0), ebStd2.truePositives(0));
    assertEquals(ebThreshold.trueNegatives(0), ebStd2.trueNegatives(0));
    assertEquals(ebThreshold.falsePositives(0), ebStd2.falsePositives(0));
    assertEquals(ebThreshold.falseNegatives(0), ebStd2.falseNegatives(0));

    assertEquals(ebThreshold.truePositives(1), ebStd4.truePositives(1));
    assertEquals(ebThreshold.trueNegatives(1), ebStd4.trueNegatives(1));
    assertEquals(ebThreshold.falsePositives(1), ebStd4.falsePositives(1));
    assertEquals(ebThreshold.falseNegatives(1), ebStd4.falseNegatives(1));
}