Search in sources :

Example 6 with ManhattanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance in project nd4j by deeplearning4j.

the class HalfOpsTests method testReduce3_1.

@Test
public void testReduce3_1() throws Exception {
    INDArray array1 = Nd4j.create(new float[] { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f });
    INDArray array2 = Nd4j.create(new float[] { 0.5f, 1.5f, 2.5f, 3.5f, 4.5f });
    double result = Nd4j.getExecutioner().execAndReturn(new ManhattanDistance(array1, array2)).getFinalResult().doubleValue();
    assertEquals(2.5, result, 0.01);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) ManhattanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance) Test(org.junit.Test)

Example 7 with ManhattanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance in project nd4j by deeplearning4j.

the class CudaExecutionerTest method testPinnedManhattanDistance2.

@Test
public void testPinnedManhattanDistance2() throws Exception {
    // simple way to stop test if we're not on CUDA backend here
    INDArray array1 = Nd4j.linspace(1, 1000, 1000);
    INDArray array2 = Nd4j.linspace(1, 900, 1000);
    double result = Nd4j.getExecutioner().execAndReturn(new ManhattanDistance(array1, array2)).getFinalResult().doubleValue();
    assertEquals(50000.0, result, 0.001f);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) ManhattanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance) Test(org.junit.Test)

Example 8 with ManhattanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance in project nd4j by deeplearning4j.

the class CudaReduce3Tests method testPinnedManhattanDistance3.

@Test
public void testPinnedManhattanDistance3() throws Exception {
    INDArray array1 = Nd4j.linspace(1, 10000, 98);
    INDArray array2 = Nd4j.linspace(1, 9000, 98);
    float result = Nd4j.getExecutioner().execAndReturn(new ManhattanDistance(array1, array2)).getFinalResult().floatValue();
    assertEquals(49000.004f, result, 0.01f);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) ManhattanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance) Test(org.junit.Test)

Example 9 with ManhattanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance in project nd4j by deeplearning4j.

the class CudaReduce3Tests method testPinnedManhattanDistance2.

@Test
public void testPinnedManhattanDistance2() throws Exception {
    // simple way to stop test if we're not on CUDA backend here
    INDArray array1 = Nd4j.linspace(1, 1000, 1000);
    INDArray array2 = Nd4j.linspace(1, 900, 1000);
    double result = Nd4j.getExecutioner().execAndReturn(new ManhattanDistance(array1, array2)).getFinalResult().doubleValue();
    assertEquals(50000.0, result, 0.001f);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) ManhattanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance) Test(org.junit.Test)

Example 10 with ManhattanDistance

use of org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance in project nd4j by deeplearning4j.

the class OpExecutionerTestsC method testEuclideanManhattanDistanceAlongDimension_Rank4.

@Test
public void testEuclideanManhattanDistanceAlongDimension_Rank4() {
    DataBuffer.Type initialType = Nd4j.dataType();
    DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE);
    Nd4j.getRandom().setSeed(12345);
    INDArray firstOneExample = Nd4j.linspace(1, 8, 8).reshape('c', new int[] { 1, 2, 2, 2 });
    INDArray secondOneExample = firstOneExample.add(1);
    double[] d1 = firstOneExample.data().asDouble();
    double[] d2 = secondOneExample.data().asDouble();
    double sumSquaredDiff = 0.0;
    double expManhattanDistance = 0.0;
    for (int i = 0; i < d1.length; i++) {
        double diff = d1[i] - d2[i];
        sumSquaredDiff += diff * diff;
        expManhattanDistance += Math.abs(diff);
    }
    double expectedEuclidean = Math.sqrt(sumSquaredDiff);
    System.out.println("Expected, Euclidean: " + expectedEuclidean);
    System.out.println("Expected, Manhattan: " + expManhattanDistance);
    int mb = 2;
    INDArray firstOrig = Nd4j.create(mb, 2, 2, 2);
    INDArray secondOrig = Nd4j.create(mb, 2, 2, 2);
    for (int i = 0; i < mb; i++) {
        firstOrig.put(new INDArrayIndex[] { point(i), all(), all(), all() }, firstOneExample);
        secondOrig.put(new INDArrayIndex[] { point(i), all(), all(), all() }, secondOneExample);
    }
    for (char order : new char[] { 'c', 'f' }) {
        INDArray first = firstOrig.dup(order);
        INDArray second = secondOrig.dup(order);
        assertEquals(firstOrig, first);
        assertEquals(secondOrig, second);
        INDArray out = Nd4j.getExecutioner().exec(new EuclideanDistance(first, second), 1, 2, 3);
        for (int i = 0; i < first.tensorssAlongDimension(1, 2, 3); i++) {
            assertEquals(first.javaTensorAlongDimension(i, 1, 2, 3).shapeInfoDataBuffer(), first.tensorAlongDimension(i, 1, 2, 3).shapeInfoDataBuffer());
        }
        Pair<DataBuffer, DataBuffer> firstTadInfo = Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(first, 1, 2, 3);
        Pair<DataBuffer, DataBuffer> secondTadInfo = Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(second, 1, 2, 3);
        for (int i = 0; i < first.tensorssAlongDimension(1, 2, 3); i++) {
            assertEquals(first.javaTensorAlongDimension(i, 1, 2, 3).offset(), firstTadInfo.getSecond().getLong(i));
            assertEquals(second.javaTensorAlongDimension(i, 1, 2, 3).offset(), secondTadInfo.getSecond().getLong(i));
        }
        INDArray outManhattan = Nd4j.getExecutioner().exec(new ManhattanDistance(first, second), 1, 2, 3);
        System.out.println("\n\nOrder: " + order);
        System.out.println("Euclidean:");
        System.out.println(Arrays.toString(out.getRow(0).dup().data().asDouble()));
        System.out.println(Arrays.toString(out.getRow(1).dup().data().asDouble()));
        assertEquals(out.getRow(0), out.getRow(1));
        System.out.println("Manhattan:");
        System.out.println(Arrays.toString(outManhattan.getRow(0).dup().data().asDouble()));
        System.out.println(Arrays.toString(outManhattan.getRow(1).dup().data().asDouble()));
        assertEquals(expManhattanDistance, outManhattan.getRow(0).getDouble(0), 1e-5);
        assertEquals(expectedEuclidean, out.getRow(0).getDouble(0), 1e-5);
    }
    DataTypeUtil.setDTypeForContext(initialType);
}
Also used : EuclideanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NDArrayIndex.point(org.nd4j.linalg.indexing.NDArrayIndex.point) ManhattanDistance(org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest) Test(org.junit.Test)

Aggregations

INDArray (org.nd4j.linalg.api.ndarray.INDArray)10 ManhattanDistance (org.nd4j.linalg.api.ops.impl.accum.distances.ManhattanDistance)10 Test (org.junit.Test)9 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)2 EuclideanDistance (org.nd4j.linalg.api.ops.impl.accum.distances.EuclideanDistance)2 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)1 IMax (org.nd4j.linalg.api.ops.impl.indexaccum.IMax)1 LogSoftMax (org.nd4j.linalg.api.ops.impl.transforms.LogSoftMax)1 OldSoftMax (org.nd4j.linalg.api.ops.impl.transforms.OldSoftMax)1 SoftMaxDerivative (org.nd4j.linalg.api.ops.impl.transforms.SoftMaxDerivative)1 Sqrt (org.nd4j.linalg.api.ops.impl.transforms.Sqrt)1 NDArrayIndex.point (org.nd4j.linalg.indexing.NDArrayIndex.point)1