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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#slices() . 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: LoneTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void checkWithReshape() {
    INDArray arr = Nd4j.create(1, 3);
    INDArray reshaped = arr.reshape('f', 3, 1);
    for (int i=0;i<reshaped.length();i++) {
        log.info("C order element " + i + arr.getDouble(i));
        log.info("F order element " + i + reshaped.getDouble(i));
    }
    for (int j=0;j<arr.slices();j++) {
        for (int k=0;k<arr.slice(j).length();k++) {
            log.info("\nArr: slice " + j + " element " + k + " " + arr.slice(j).getDouble(k));
        }
    }
    for (int j=0;j<reshaped.slices();j++) {
        for (int k=0;k<reshaped.slice(j).length();k++) {
            log.info("\nReshaped: slice " + j + " element " + k + " " + reshaped.slice(j).getDouble(k));
        }
    }
}
 
Example 2
Source File: ReshapeTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeTwoTwoTwo() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12).reshape(3, 2, 2);
    INDArray sliceZero = Nd4j.create(new double[][] {{1, 7}, {4, 10}});
    INDArray sliceOne = Nd4j.create(new double[][] {{2, 8}, {5, 11}});
    INDArray sliceTwo = Nd4j.create(new double[][] {{3, 9}, {6, 12}});
    INDArray[] assertions = new INDArray[] {sliceZero, sliceOne, sliceTwo};

    for (int i = 0; i < threeTwoTwo.slices(); i++) {
        INDArray sliceI = threeTwoTwo.slice(i);
        assertEquals(assertions[i], sliceI);
    }

    INDArray linspaced = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArray[] assertionsTwo = new INDArray[] {Nd4j.create(new double[] {1, 3}), Nd4j.create(new double[] {2, 4})};

    for (int i = 0; i < assertionsTwo.length; i++)
        assertEquals(linspaced.slice(i), assertionsTwo[i]);
}
 
Example 3
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPointIndexes() {
    INDArray arr = Nd4j.create(4, 3, 2);
    INDArray get = arr.get(NDArrayIndex.all(), NDArrayIndex.point(1), NDArrayIndex.all());
    assertArrayEquals(new int[] {4, 2}, get.shape());
    INDArray linspaced = Nd4j.linspace(1, 24, 24).reshape(4, 3, 2);
    INDArray assertion = Nd4j.create(new double[][] {{3, 4}, {9, 10}, {15, 16}, {21, 22}});

    INDArray linspacedGet = linspaced.get(NDArrayIndex.all(), NDArrayIndex.point(1), NDArrayIndex.all());
    for (int i = 0; i < linspacedGet.slices(); i++) {
        INDArray sliceI = linspacedGet.slice(i);
        assertEquals(assertion.slice(i), sliceI);
    }
    assertArrayEquals(new int[] {6, 1}, linspacedGet.stride());
    assertEquals(assertion, linspacedGet);
}
 
Example 4
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPermute() {
    INDArray n = Nd4j.create(Nd4j.linspace(1, 20, 20).data(), new long[] {5, 4});
    INDArray transpose = n.transpose();
    INDArray permute = n.permute(1, 0);
    assertEquals(permute, transpose);
    assertEquals(transpose.length(), permute.length(), 1e-1);


    INDArray toPermute = Nd4j.create(Nd4j.linspace(0, 7, 8).data(), new long[] {2, 2, 2});
    INDArray permuted = toPermute.permute(2, 1, 0);
    assertNotEquals(toPermute, permuted);

    INDArray permuteOther = toPermute.permute(1, 2, 0);
    for (int i = 0; i < permuteOther.slices(); i++) {
        INDArray toPermutesliceI = toPermute.slice(i);
        INDArray permuteOtherSliceI = permuteOther.slice(i);
        permuteOtherSliceI.toString();
        assertNotEquals(toPermutesliceI, permuteOtherSliceI);
    }
    assertArrayEquals(permuteOther.shape(), toPermute.shape());
    assertNotEquals(toPermute, permuteOther);


}
 
Example 5
Source File: BaseNDArrayFactory.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a column vector where each entry is the nth bilinear
 * product of the nth slices of the two tensors.
 */
@Override
public INDArray bilinearProducts(INDArray curr, INDArray in) {
    Preconditions.checkArgument(curr.rank() == 3, "Argument 'curr' must be rank 3. Got input with rank: %s", curr.rank());
    if (in.columns() != 1) {
        throw new AssertionError("Expected a column vector");
    }
    if (in.rows() != curr.size(curr.shape().length - 1)) {
        throw new AssertionError("Number of rows in the input does not match number of columns in tensor");
    }
    if (curr.size(curr.shape().length - 2) != curr.size(curr.shape().length - 1)) {
        throw new AssertionError("Can only perform this operation on a SimpleTensor with square slices");
    }

    INDArray ret = Nd4j.create(curr.slices(), 1);
    INDArray inT = in.transpose();
    for (int i = 0; i < curr.slices(); i++) {
        INDArray slice = curr.slice(i);
        INDArray inTTimesSlice = inT.mmul(slice);
        ret.putScalar(i, Nd4j.getBlasWrapper().dot(inTTimesSlice, in));
    }
    return ret;
}
 
Example 6
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPermute() {
    INDArray n = Nd4j.create(Nd4j.linspace(1, 20, 20, DataType.DOUBLE).data(), new long[] {5, 4});
    INDArray transpose = n.transpose();
    INDArray permute = n.permute(1, 0);
    assertEquals(permute, transpose);
    assertEquals(transpose.length(), permute.length(), 1e-1);


    INDArray toPermute = Nd4j.create(Nd4j.linspace(0, 7, 8, DataType.DOUBLE).data(), new long[] {2, 2, 2});
    INDArray permuted = toPermute.dup().permute(2, 1, 0);
    boolean eq = toPermute.equals(permuted);
    assertNotEquals(toPermute, permuted);

    INDArray permuteOther = toPermute.permute(1, 2, 0);
    for (int i = 0; i < permuteOther.slices(); i++) {
        INDArray toPermutesliceI = toPermute.slice(i);
        INDArray permuteOtherSliceI = permuteOther.slice(i);
        permuteOtherSliceI.toString();
        assertNotEquals(toPermutesliceI, permuteOtherSliceI);
    }
    assertArrayEquals(permuteOther.shape(), toPermute.shape());
    assertNotEquals(toPermute, permuteOther);


}
 
Example 7
Source File: LoneTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void checkWithReshape() {
        INDArray arr = Nd4j.create(1, 3);
        INDArray reshaped = arr.reshape('f', 3, 1);
        for (int i=0;i<reshaped.length();i++) {
//            log.info("C order element " + i + arr.getDouble(i));
//            log.info("F order element " + i + reshaped.getDouble(i));
            arr.getDouble(i);
            reshaped.getDouble(i);
        }
        for (int j=0;j<arr.slices();j++) {
            for (int k=0;k<arr.slice(j).length();k++) {
//                log.info("\nArr: slice " + j + " element " + k + " " + arr.slice(j).getDouble(k));
                arr.slice(j).getDouble(k);
            }
        }
        for (int j=0;j<reshaped.slices();j++) {
            for (int k=0;k<reshaped.slice(j).length();k++) {
//                log.info("\nReshaped: slice " + j + " element " + k + " " + reshaped.slice(j).getDouble(k));
                reshaped.slice(j).getDouble(k);
            }
        }
    }
 
Example 8
Source File: ReshapeTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeTwoTwoTwo() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12, DataType.DOUBLE).reshape(3, 2, 2);
    INDArray sliceZero = Nd4j.create(new double[][] {{1, 7}, {4, 10}});
    INDArray sliceOne = Nd4j.create(new double[][] {{2, 8}, {5, 11}});
    INDArray sliceTwo = Nd4j.create(new double[][] {{3, 9}, {6, 12}});
    INDArray[] assertions = new INDArray[] {sliceZero, sliceOne, sliceTwo};

    for (int i = 0; i < threeTwoTwo.slices(); i++) {
        INDArray sliceI = threeTwoTwo.slice(i);
        assertEquals(assertions[i], sliceI);
    }

    INDArray linspaced = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    INDArray[] assertionsTwo = new INDArray[] {Nd4j.create(new double[] {1, 3}), Nd4j.create(new double[] {2, 4})};

    for (int i = 0; i < assertionsTwo.length; i++)
        assertEquals(linspaced.slice(i), assertionsTwo[i]);
}
 
Example 9
Source File: IndexingTestsC.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPointIndexes() {
    INDArray arr = Nd4j.create(DataType.DOUBLE, 4, 3, 2);
    INDArray get = arr.get(NDArrayIndex.all(), NDArrayIndex.point(1), NDArrayIndex.all());
    assertArrayEquals(new long[] {4, 2}, get.shape());
    INDArray linspaced = Nd4j.linspace(1, 24, 24, DataType.DOUBLE).reshape(4, 3, 2);
    INDArray assertion = Nd4j.create(new double[][] {{3, 4}, {9, 10}, {15, 16}, {21, 22}});

    INDArray linspacedGet = linspaced.get(NDArrayIndex.all(), NDArrayIndex.point(1), NDArrayIndex.all());
    for (int i = 0; i < linspacedGet.slices(); i++) {
        INDArray sliceI = linspacedGet.slice(i);
        assertEquals(assertion.slice(i), sliceI);
    }
    assertArrayEquals(new long[] {6, 1}, linspacedGet.stride());
    assertEquals(assertion, linspacedGet);
}
 
Example 10
Source File: VPTree.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create an ndarray
 * from the datapoints
 * @param data
 * @return
 */
public static INDArray buildFromData(List<DataPoint> data) {
    INDArray ret = Nd4j.create(data.size(), data.get(0).getD());
    for (int i = 0; i < ret.slices(); i++)
        ret.putSlice(i, data.get(i).getPoint());
    return ret;
}
 
Example 11
Source File: LoneTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void checkSliceofSlice() {
    /*
        Issue 1: Slice of slice with c order and f order views are not equal

        Comment out assert and run then -> Issue 2: Index out of bound exception with certain shapes when accessing elements with getDouble() in f order
        (looks like problem is when rank-1==1) eg. 1,2,1 and 2,2,1
     */
    int[] ranksToCheck = new int[]{2, 3, 4, 5};
    for (int rank = 0; rank < ranksToCheck.length; rank++) {
        log.info("\nRunning through rank " + ranksToCheck[rank]);
        List<Pair<INDArray, String>> allF = NDArrayCreationUtil.getTestMatricesWithVaryingShapes(ranksToCheck[rank], 'f');
        Iterator<Pair<INDArray, String>> iter = allF.iterator();
        while (iter.hasNext()) {
            Pair<INDArray, String> currentPair = iter.next();
            INDArray origArrayF = currentPair.getFirst();
            INDArray sameArrayC = origArrayF.dup('c');
            log.info("\nLooping through slices for shape " + currentPair.getSecond());
            log.info("\nOriginal array:\n" + origArrayF);
            INDArray viewF = origArrayF.slice(0);
            INDArray viewC = sameArrayC.slice(0);
            log.info("\nSlice 0, C order:\n" + viewC.toString());
            log.info("\nSlice 0, F order:\n" + viewF.toString());
            for (int i = 0; i < viewF.slices(); i++) {
                //assertEquals(viewF.slice(i),viewC.slice(i));
                for (int j = 0; j < viewF.slice(i).length(); j++) {
                    //if (j>0) break;
                    log.info("\nC order slice " + i + ", element 0 :" + viewC.slice(i).getDouble(j)); //C order is fine
                    log.info("\nF order slice " + i + ", element 0 :" + viewF.slice(i).getDouble(j)); //throws index out of bound err on F order
                }
            }
        }
    }
}
 
Example 12
Source File: BaseNDArrayFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Reverses the passed in matrix such that m[0] becomes m[m.length - 1] etc
 *
 * @param reverse the matrix to reverse
 * @return the reversed matrix
 */
@Override
public INDArray rot(INDArray reverse) {
    INDArray ret = Nd4j.create(reverse.shape());
    if (reverse.isVector())
        return reverse(reverse);
    else {
        for (int i = 0; i < reverse.slices(); i++) {
            ret.putSlice(i, reverse(reverse.slice(i)));
        }
    }
    return ret.reshape(reverse.shape());
}
 
Example 13
Source File: LoneTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void checkSliceofSlice() {
        /*
            Issue 1: Slice of slice with c order and f order views are not equal

            Comment out assert and run then -> Issue 2: Index out of bound exception with certain shapes when accessing elements with getDouble() in f order
            (looks like problem is when rank-1==1) eg. 1,2,1 and 2,2,1
         */
        int[] ranksToCheck = new int[]{2, 3, 4, 5};
        for (int rank = 0; rank < ranksToCheck.length; rank++) {
//            log.info("\nRunning through rank " + ranksToCheck[rank]);
            List<Pair<INDArray, String>> allF = NDArrayCreationUtil.getTestMatricesWithVaryingShapes(ranksToCheck[rank], 'f', DataType.FLOAT);
            Iterator<Pair<INDArray, String>> iter = allF.iterator();
            while (iter.hasNext()) {
                Pair<INDArray, String> currentPair = iter.next();
                INDArray origArrayF = currentPair.getFirst();
                INDArray sameArrayC = origArrayF.dup('c');
//                log.info("\nLooping through slices for shape " + currentPair.getSecond());
//                log.info("\nOriginal array:\n" + origArrayF);
                origArrayF.toString();
                INDArray viewF = origArrayF.slice(0);
                INDArray viewC = sameArrayC.slice(0);
//                log.info("\nSlice 0, C order:\n" + viewC.toString());
//                log.info("\nSlice 0, F order:\n" + viewF.toString());
                viewC.toString();
                viewF.toString();
                for (int i = 0; i < viewF.slices(); i++) {
                    //assertEquals(viewF.slice(i),viewC.slice(i));
                    for (int j = 0; j < viewF.slice(i).length(); j++) {
                        //if (j>0) break;
//                        log.info("\nC order slice " + i + ", element 0 :" + viewC.slice(i).getDouble(j)); //C order is fine
//                        log.info("\nF order slice " + i + ", element 0 :" + viewF.slice(i).getDouble(j)); //throws index out of bound err on F order
                        viewC.slice(i).getDouble(j);
                        viewF.slice(i).getDouble(j);
                    }
                }
            }
        }
    }
 
Example 14
Source File: BaseNDArrayFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Reverses the passed in matrix such that m[0] becomes m[m.length - 1] etc
 *
 * @param reverse the matrix to reverse
 * @return the reversed matrix
 */
@Override
public INDArray rot(INDArray reverse) {
    INDArray ret = Nd4j.create(reverse.shape());
    if (reverse.isVector())
        return reverse(reverse);
    else {
        for (int i = 0; i < reverse.slices(); i++) {
            ret.putSlice(i, reverse(reverse.slice(i)));
        }
    }
    return ret.reshape(reverse.shape());
}
 
Example 15
Source File: BaseComplexNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Perform an copy matrix multiplication
 *
 * @param other  the other matrix to perform matrix multiply with
 * @param result the result ndarray
 * @return the result of the matrix multiplication
 */
@Override
public IComplexNDArray mmuli(INDArray other, INDArray result) {


    IComplexNDArray otherArray = (IComplexNDArray) other;
    IComplexNDArray resultArray = (IComplexNDArray) result;

    if (other.shape().length > 2) {
        for (int i = 0; i < other.slices(); i++) {
            resultArray.putSlice(i, slice(i).mmul(otherArray.slice(i)));
        }

        return resultArray;

    }


    LinAlgExceptions.assertMultiplies(this, other);

    if (other.isScalar()) {
        return muli(otherArray.getComplex(0), resultArray);
    }
    if (isScalar()) {
        return otherArray.muli(getComplex(0), resultArray);
    }

    /* check sizes and resize if necessary */
    //assertMultipliesWith(other);


    if (result == this || result == other) {
        /* actually, blas cannot do multiplications in-place. Therefore, we will fake by
         * allocating a temporary object on the side and copy the result later.
         */
        IComplexNDArray temp = Nd4j.createComplex(resultArray.shape());

        if (otherArray.columns() == 1) {
            Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(temp),
                            BlasBufferUtil.getCharForTranspose(this), Nd4j.UNIT, this, otherArray, Nd4j.ZERO, temp);
        } else {
            Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(temp),
                            BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(other),
                            Nd4j.UNIT, this, otherArray, Nd4j.ZERO, temp);

        }

        Nd4j.getBlasWrapper().copy(temp, resultArray);


    } else {
        if (otherArray.columns() == 1) {
            Nd4j.getBlasWrapper().level2().gemv(BlasBufferUtil.getCharForTranspose(resultArray),
                            BlasBufferUtil.getCharForTranspose(this), Nd4j.UNIT, this, otherArray, Nd4j.ZERO,
                            resultArray);
        }


        else {
            Nd4j.getBlasWrapper().level3().gemm(BlasBufferUtil.getCharForTranspose(resultArray),
                            BlasBufferUtil.getCharForTranspose(this), BlasBufferUtil.getCharForTranspose(other),
                            Nd4j.UNIT, this, otherArray, Nd4j.ZERO, resultArray);
        }
    }
    return resultArray;


}
 
Example 16
Source File: NDArrayMath.java    From nd4j with Apache License 2.0 3 votes vote down vote up
/**
 * The number of vectors
 * in each slice of an ndarray.
 * @param arr the array to
 *            get the number
 *            of vectors per slice for
 * @return the number of vectors per slice
 */
public static long vectorsPerSlice(INDArray arr) {
    if (arr.rank() > 2) {
        return ArrayUtil.prodLong(new long[] {arr.size(-1), arr.size(-2)});
    }

    return arr.slices();
}
 
Example 17
Source File: NDArrayMath.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * The number of vectors
 * in each slice of an ndarray.
 * @param arr the array to
 *            get the number
 *            of vectors per slice for
 * @return the number of vectors per slice
 */
public static long vectorsPerSlice(INDArray arr) {
    if (arr.rank() > 2) {
        return ArrayUtil.prodLong(new long[] {arr.size(-1), arr.size(-2)});
    }

    return arr.slices();
}