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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#javaTensorAlongDimension() . 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: TestTensorAlongDimension.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaVsNative() {
    long totalJavaTime = 0;
    long totalCTime = 0;
    long n = 10;
    INDArray row = Nd4j.create(1, 100);

    for (int i = 0; i < n; i++) {
        StopWatch javaTiming = new StopWatch();
        javaTiming.start();
        row.javaTensorAlongDimension(0, 0);
        javaTiming.stop();
        StopWatch cTiming = new StopWatch();
        cTiming.start();
        row.tensorAlongDimension(0, 0);
        cTiming.stop();
        totalJavaTime += javaTiming.getNanoTime();
        totalCTime += cTiming.getNanoTime();
    }

    System.out.println("Java timing " + (totalJavaTime / n) + " C time " + (totalCTime / n));

}
 
Example 2
Source File: TADTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testMysteriousCrash() {
    INDArray arrayF = Nd4j.create(new int[] {1, 1, 4, 4}, 'f');
    INDArray arrayC = Nd4j.create(new int[] {1, 1, 4, 4}, 'c');
    INDArray javaCTad = arrayC.javaTensorAlongDimension(0, 2, 3);
    INDArray javaFTad = arrayF.javaTensorAlongDimension(0, 2, 3);
    Pair<DataBuffer, DataBuffer> tadBuffersF =
                    Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(arrayF, new int[] {2, 3});
    Pair<DataBuffer, DataBuffer> tadBuffersC =
                    Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(arrayC, new int[] {2, 3});

    log.info("Got TADShapeF: {}", Arrays.toString(tadBuffersF.getFirst().asInt()) + " with java "
                    + javaFTad.shapeInfoDataBuffer());
    log.info("Got TADShapeC: {}", Arrays.toString(tadBuffersC.getFirst().asInt()) + " with java "
                    + javaCTad.shapeInfoDataBuffer());

}
 
Example 3
Source File: OpExecutionerTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMeanSumSimple() {
    System.out.println("3d");
    INDArray arr = Nd4j.ones(1, 4, 4);
    assertEquals(Nd4j.ones(1), arr.mean(1, 2));
    assertEquals(Nd4j.ones(1).muli(16), arr.sum(1, 2));

    System.out.println("4d");
    INDArray arr4 = Nd4j.ones(1, 1, 4, 4);
    INDArray arr4m = arr4.mean(2, 3);
    INDArray arr4s = arr4.sum(2, 3);
    for (int i = 0; i < arr4m.length(); i++)
        assertEquals(arr4m.getDouble(i), 1, 1e-1);
    for (int i = 0; i < arr4s.length(); i++)
        assertEquals(arr4s.getDouble(i), 16, 1e-1);

    System.out.println("5d");
    INDArray arr5 = Nd4j.ones(1, 1, 4, 4, 4);
    INDArray arr5m = arr5.mean(2, 3);
    INDArray arr5s = arr5.sum(2, 3);
    for (int i = 0; i < arr5m.length(); i++)
        assertEquals(arr5m.getDouble(i), 1, 1e-1);
    for (int i = 0; i < arr5s.length(); i++)
        assertEquals(arr5s.getDouble(i), 16, 1e-1);
    System.out.println("6d");
    INDArray arr6 = Nd4j.ones(1, 1, 4, 4, 4, 4);
    INDArray arr6Tad = arr6.javaTensorAlongDimension(0, 2, 3);
    INDArray arr6s = arr6.sum(2, 3);
    for (int i = 0; i < arr6s.length(); i++)
        assertEquals(arr6s.getDouble(i), 16, 1e-1);

    INDArray arr6m = arr6.mean(2, 3);
    for (int i = 0; i < arr6m.length(); i++)
        assertEquals(arr6m.getDouble(i), 1, 1e-1);

}
 
Example 4
Source File: LoneTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testTad() {
    int[] someShape = {2, 1, 3, 3};
    INDArray a = Nd4j.linspace(1, 18, 18).reshape(someShape);
    INDArray java = a.javaTensorAlongDimension(0, 2, 3);
    INDArray tad = a.tensorAlongDimension(0, 2, 3);
    //assertTrue(a.tensorAlongDimension(0,2,3).rank() == 2); //is rank 3 with an extra 1
    assertEquals(java, tad);
}
 
Example 5
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 6
Source File: NDArrayCreationUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static List<Pair<INDArray, String>> get3dTensorAlongDimensionWithShape(long seed, long... shape) {
    List<Pair<INDArray, String>> list = new ArrayList<>();
    String baseMsg = "get3dTensorAlongDimensionWithShape(" + seed + "," + Arrays.toString(shape) + ")";

    //Create some 4d arrays and get subsets using 3d TAD on them
    //This is not an exhaustive list of possible 3d arrays from 4d via TAD

    Nd4j.getRandom().setSeed(seed);
    //            int[] shape4d1 = {shape[2],shape[1],shape[0],3};
    val shape4d1 = new long[]{shape[0], shape[1], shape[2], 3};
    int lenshape4d1 = ArrayUtil.prod(shape4d1);
    INDArray orig1a = Nd4j.linspace(1, lenshape4d1, lenshape4d1).reshape(shape4d1);
    INDArray tad1a = orig1a.javaTensorAlongDimension(0, 0, 1, 2);
    INDArray orig1b = Nd4j.linspace(1, lenshape4d1, lenshape4d1).reshape(shape4d1);
    INDArray tad1b = orig1b.javaTensorAlongDimension(1, 0, 1, 2);

    list.add(new Pair<>(tad1a, baseMsg + ".get(0)"));
    list.add(new Pair<>(tad1b, baseMsg + ".get(1)"));

    long[] shape4d2 = {3, shape[0], shape[1], shape[2]};
    int lenshape4d2 = ArrayUtil.prod(shape4d2);
    INDArray orig2 = Nd4j.linspace(1, lenshape4d2, lenshape4d2).reshape(shape4d2);
    INDArray tad2 = orig2.javaTensorAlongDimension(1, 1, 2, 3);
    list.add(new Pair<>(tad2, baseMsg + ".get(2)"));

    long[] shape4d3 = {shape[0], shape[1], 3, shape[2]};
    int lenshape4d3 = ArrayUtil.prod(shape4d3);
    INDArray orig3 = Nd4j.linspace(1, lenshape4d3, lenshape4d3).reshape(shape4d3);
    INDArray tad3 = orig3.javaTensorAlongDimension(1, 1, 3, 0);
    list.add(new Pair<>(tad3, baseMsg + ".get(3)"));

    long[] shape4d4 = {shape[0], 3, shape[1], shape[2]};
    int lenshape4d4 = ArrayUtil.prod(shape4d4);
    INDArray orig4 = Nd4j.linspace(1, lenshape4d4, lenshape4d4).reshape(shape4d4);
    INDArray tad4 = orig4.javaTensorAlongDimension(1, 2, 0, 3);
    list.add(new Pair<>(tad4, baseMsg + ".get(4)"));

    return list;
}
 
Example 7
Source File: NDArrayCreationUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static List<Pair<INDArray, String>> get4dTensorAlongDimensionWithShape(int seed, int... shape) {
    List<Pair<INDArray, String>> list = new ArrayList<>();
    String baseMsg = "get4dTensorAlongDimensionWithShape(" + seed + "," + Arrays.toString(shape) + ")";

    //Create some 5d arrays and get subsets using 4d TAD on them
    //This is not an exhausive list of possible 4d arrays from 5d via TAD
    Nd4j.getRandom().setSeed(seed);
    int[] shape4d1 = {3, shape[0], shape[1], shape[2], shape[3]};
    int len = ArrayUtil.prod(shape4d1);
    INDArray orig1a = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(shape4d1));
    INDArray tad1a = orig1a.javaTensorAlongDimension(0, 1, 2, 3, 4);
    INDArray orig1b = Nd4j.linspace(1, len, len).reshape(ArrayUtil.toLongArray(shape4d1));
    INDArray tad1b = orig1b.javaTensorAlongDimension(2, 1, 2, 3, 4);

    list.add(new Pair<>(tad1a, baseMsg + ".get(0)"));
    list.add(new Pair<>(tad1b, baseMsg + ".get(1)"));

    int[] shape4d2 = {3, shape[0], shape[1], shape[2], shape[3]};
    int len2 = ArrayUtil.prod(shape4d2);
    INDArray orig2 = Nd4j.linspace(1, len2, len2).reshape(ArrayUtil.toLongArray(shape4d2));
    INDArray tad2 = orig2.javaTensorAlongDimension(1, 3, 4, 2, 1);
    list.add(new Pair<>(tad2, baseMsg + ".get(2)"));

    int[] shape4d3 = {shape[0], shape[1], 3, shape[2], shape[3]};
    int len3 = ArrayUtil.prod(shape4d3);
    INDArray orig3 = Nd4j.linspace(1, len3, len3).reshape(ArrayUtil.toLongArray(shape4d3));
    INDArray tad3 = orig3.javaTensorAlongDimension(1, 4, 1, 3, 0);
    list.add(new Pair<>(tad3, baseMsg + ".get(3)"));

    int[] shape4d4 = {shape[0], shape[1], shape[2], shape[3], 3};
    int len4 = ArrayUtil.prod(shape4d4);
    INDArray orig4 = Nd4j.linspace(1, len4, len4).reshape(ArrayUtil.toLongArray(shape4d4));
    INDArray tad4 = orig4.javaTensorAlongDimension(1, 2, 0, 3, 1);
    list.add(new Pair<>(tad4, baseMsg + ".get(4)"));

    return list;
}
 
Example 8
Source File: NDArrayCreationUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static List<Pair<INDArray, String>> get5dTensorAlongDimensionWithShape(int seed, int... shape) {
    List<Pair<INDArray, String>> list = new ArrayList<>();
    String baseMsg = "get5dTensorAlongDimensionWithShape(" + seed + "," + Arrays.toString(shape) + ")";

    //Create some 6d arrays and get subsets using 5d TAD on them
    //This is not an exhausive list of possible 5d arrays from 6d via TAD
    Nd4j.getRandom().setSeed(seed);
    int[] shape4d1 = {3, shape[0], shape[1], shape[2], shape[3], shape[4]};
    INDArray orig1a = Nd4j.rand(shape4d1);
    INDArray tad1a = orig1a.javaTensorAlongDimension(0, 1, 2, 3, 4, 5);
    INDArray orig1b = Nd4j.rand(shape4d1);
    INDArray tad1b = orig1b.javaTensorAlongDimension(2, 1, 2, 3, 4, 5);

    list.add(new Pair<>(tad1a, baseMsg + ".get(0)"));
    list.add(new Pair<>(tad1b, baseMsg + ".get(1)"));

    int[] shape4d2 = {3, shape[0], shape[1], shape[2], shape[3], shape[4]};
    INDArray orig2 = Nd4j.rand(shape4d2);
    INDArray tad2 = orig2.javaTensorAlongDimension(1, 3, 5, 4, 2, 1);
    list.add(new Pair<>(tad2, baseMsg + ".get(2)"));

    int[] shape4d3 = {shape[0], shape[1], shape[2], shape[3], shape[4], 2};
    INDArray orig3 = Nd4j.rand(shape4d3);
    INDArray tad3 = orig3.javaTensorAlongDimension(1, 4, 1, 3, 2, 0);
    list.add(new Pair<>(tad3, baseMsg + ".get(3)"));

    int[] shape4d4 = {shape[0], shape[1], shape[2], shape[3], 3, shape[4]};
    INDArray orig4 = Nd4j.rand(shape4d4);
    INDArray tad4 = orig4.javaTensorAlongDimension(1, 5, 2, 0, 3, 1);
    list.add(new Pair<>(tad4, baseMsg + ".get(4)"));

    return list;
}
 
Example 9
Source File: ConcatTestsC.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testConcat3d() {
    INDArray first = Nd4j.linspace(1, 24, 24).reshape('c', 2, 3, 4);
    INDArray second = Nd4j.linspace(24, 36, 12).reshape('c', 1, 3, 4);
    INDArray third = Nd4j.linspace(36, 48, 12).reshape('c', 1, 3, 4);

    //ConcatV2, dim 0
    INDArray exp = Nd4j.create(2 + 1 + 1, 3, 4);
    exp.put(new INDArrayIndex[] {NDArrayIndex.interval(0, 2), NDArrayIndex.all(), NDArrayIndex.all()}, first);
    exp.put(new INDArrayIndex[] {NDArrayIndex.point(2), NDArrayIndex.all(), NDArrayIndex.all()}, second);
    exp.put(new INDArrayIndex[] {NDArrayIndex.point(3), NDArrayIndex.all(), NDArrayIndex.all()}, third);

    INDArray concat0 = Nd4j.concat(0, first, second, third);

    assertEquals(exp, concat0);

    //ConcatV2, dim 1
    second = Nd4j.linspace(24, 32, 8).reshape('c', 2, 1, 4);
    for (int i = 0; i < second.tensorssAlongDimension(1); i++) {
        INDArray secondTad = second.javaTensorAlongDimension(i, 1);
        System.out.println(second.tensorAlongDimension(i, 1));
    }

    third = Nd4j.linspace(32, 48, 16).reshape('c', 2, 2, 4);
    exp = Nd4j.create(2, 3 + 1 + 2, 4);
    exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.interval(0, 3), NDArrayIndex.all()}, first);
    exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(3), NDArrayIndex.all()}, second);
    exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.interval(4, 6), NDArrayIndex.all()}, third);

    INDArray concat1 = Nd4j.concat(1, first, second, third);

    assertEquals(exp, concat1);

    //ConcatV2, dim 2
    second = Nd4j.linspace(24, 36, 12).reshape('c', 2, 3, 2);
    third = Nd4j.linspace(36, 42, 6).reshape('c', 2, 3, 1);
    exp = Nd4j.create(2, 3, 4 + 2 + 1);

    exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 4)}, first);
    exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(4, 6)}, second);
    exp.put(new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(6)}, third);

    INDArray concat2 = Nd4j.concat(2, first, second, third);

    assertEquals(exp, concat2);
}