Java Code Examples for org.nd4j.linalg.factory.Nd4j#createSparseCOO()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#createSparseCOO() . 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: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void tryToFindABugWithHiddenDim(){

    int[] shape = new int[] {1, 4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 0, 2}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 2}, {0, 2, 0, 1}, {0, 2, 1, 2},
            {0, 3, 0, 2}, {0, 3, 1, 0}};
    BaseSparseNDArrayCOO array = (BaseSparseNDArrayCOO) Nd4j.createSparseCOO(values, indices, shape);

    BaseSparseNDArrayCOO view1 = (BaseSparseNDArrayCOO) array.get( NDArrayIndex.point(0), NDArrayIndex.newAxis(), NDArrayIndex.newAxis(),  NDArrayIndex.point(0));
    System.out.println(view1.shapeInfoDataBuffer());
    System.out.println(view1.sparseInfoDataBuffer());

    BaseSparseNDArrayCOO view2 = (BaseSparseNDArrayCOO) view1.get( NDArrayIndex.point(0), NDArrayIndex.newAxis(),NDArrayIndex.newAxis(),  NDArrayIndex.point(0));
    System.out.println(view2.shapeInfoDataBuffer());
    System.out.println(view2.sparseInfoDataBuffer());
}
 
Example 2
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldTakeViewInLeftTopCorner() {
    // Test with dense ndarray
    double[] data = {0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0};
    INDArray array = Nd4j.create(data, new int[] {5, 5}, 0, 'c');
    INDArray denseView = array.get(NDArrayIndex.interval(0, 2), NDArrayIndex.interval(0, 2));

    // test with sparse :
    double[] values = {1, 2, 3, 4};
    int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
    INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new int[] {5, 5});

    // subarray in the top right corner
    BaseSparseNDArrayCOO sparseView = (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.interval(0, 2),
                    NDArrayIndex.interval(0, 2));
    assertArrayEquals(denseView.shape(), sparseView.shape());
    double[] currentValues = sparseView.data().asDouble();
    assertArrayEquals(values, currentValues, 1e-5);
    assertArrayEquals(ArrayUtil.flatten(indices), sparseView.getUnderlyingIndices().asInt());
    assertEquals(0, sparseView.nnz());
    System.out.println(sparseView.sparseInfoDataBuffer());
}
 
Example 3
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldTakeViewOfView2() {
    int[] shape = new int[] {4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 2}, {2, 0, 1}, {2, 1, 2},
                    {3, 0, 1}, {3, 1, 0}};

    INDArray array = Nd4j.createSparseCOO(values, indices, shape);
    BaseSparseNDArrayCOO baseView = (BaseSparseNDArrayCOO) array.get(NDArrayIndex.interval(1, 4),
                    NDArrayIndex.point(1), NDArrayIndex.all());
    BaseSparseNDArrayCOO view = (BaseSparseNDArrayCOO) baseView.get(NDArrayIndex.all(), NDArrayIndex.point(2));
    assertEquals(2, view.nnz());
    assertArrayEquals(new long[] {3, 1}, view.shape());
    assertArrayEquals(new int[] {0, 0, 1, 0}, view.getIncludedIndices().asInt());
    assertArrayEquals(new double[] {5, 7}, view.getIncludedValues().asDouble(), 1e-1);
    assertTrue(view.isColumnVector());
}
 
Example 4
Source File: SparseCOOLevel1Test.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldComputeDot() {
    INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
    //INDArray vec = Nd4j.create( new double[] {1 ,2, 3, 4});
    INDArray matrix = Nd4j.linspace(1, 4, 4).reshape(1, 4);
    INDArray vec = matrix.getRow(0);
    assertEquals(21, Nd4j.getBlasWrapper().dot(sparseVec, vec), 1e-1);
}
 
Example 5
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetRowInTheMiddle() {
    double[] values = {1, 2, 3, 4};
    int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
    INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new int[] {5, 5});
    BaseSparseNDArrayCOO sparseView =
                    (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.point(2), NDArrayIndex.all());
    assertEquals(1, sparseView.nnz());
    assertArrayEquals(new int[] {0, 1}, sparseView.getIncludedIndices().asInt());
    assertArrayEquals(new double[] {3}, sparseView.getIncludedValues().asDouble(), 1e-1);

    System.out.println(sparseView.sparseInfoDataBuffer());
}
 
Example 6
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetFirstColumn() {
    double[] values = {1, 2, 3, 4};
    int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
    INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new int[] {5, 5});
    BaseSparseNDArrayCOO sparseView =
                    (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.all(), NDArrayIndex.point(0));
    assertEquals(0, sparseView.nnz());

    System.out.println(sparseView.sparseInfoDataBuffer());
}
 
Example 7
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetWithSpecifiedIndexes() {
    int[] shape = new int[] {4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 2}, {2, 0, 1}, {2, 1, 2},
                    {3, 0, 1}, {3, 1, 0}};
    INDArray array = Nd4j.createSparseCOO(values, indices, shape);
    BaseSparseNDArrayCOO newArray = (BaseSparseNDArrayCOO) array.get(new SpecifiedIndex(new int[] {0, 3}),
                    NDArrayIndex.all(), NDArrayIndex.all());
    assertEquals(4, newArray.nnz());
    assertArrayEquals(new double[] {1, 2, 8, 9}, newArray.getIncludedValues().asDouble(), 1e-1);
    assertArrayEquals(new int[] {0, 0, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0}, newArray.getIncludedIndices().asInt());
}
 
Example 8
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldTakeViewInRightTopCorner() {

    double[] values = {1, 2, 3, 4};
    int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
    INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new int[] {5, 5});
    BaseSparseNDArrayCOO sparseView = (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.interval(0, 2),
                    NDArrayIndex.interval(2, 5));
    assertEquals(2, sparseView.nnz());
    assertArrayEquals(new double[] {1, 2}, sparseView.getIncludedValues().asDouble(), 1e-1);
    assertArrayEquals(new int[] {0, 1, 1, 0}, sparseView.getIncludedIndices().asInt());

    System.out.println(sparseView.sparseInfoDataBuffer());
}
 
Example 9
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void nestedSparseViewWithNewAxis() {
    int[] shape = new int[] {4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 2}, {2, 0, 1}, {2, 1, 2},
                    {3, 0, 2}, {3, 1, 0}};
    INDArray array = Nd4j.createSparseCOO(values, indices, shape);

    System.out.println("\nTaking view (all, point(1), all");
    INDArray v = array.get(NDArrayIndex.all(), NDArrayIndex.point(1));
    System.out.println(v.toString());
    System.out.println(v.shapeInfoDataBuffer());
    System.out.println("Fixed dimension " + v.flags());
    System.out.println("sparse offsets " + v.sparseOffsets());
    System.out.println("hidden dimensions " + v.hiddenDimensions());
    System.out.println("number of hidden dimensions " + ((BaseSparseNDArrayCOO) v).getNumHiddenDimension());
    // shape 4 x 3

    System.out.println("\nTaking view (all new axis");
    INDArray v1 = v.get(NDArrayIndex.all(), NDArrayIndex.newAxis());
    System.out.println(v1.toString());
    System.out.println(v1.shapeInfoDataBuffer());
    System.out.println("Fixed dimension " + v1.flags());
    System.out.println("sparse offsets " + v1.sparseOffsets());
    System.out.println("hidden dimensions " + v1.hiddenDimensions());
    System.out.println("number of hidden dimensions " + ((BaseSparseNDArrayCOO) v1).getNumHiddenDimension());
    // shape 4 x 1 x 3

    System.out.println("\nTaking view (all new axis");
    v1 = v.get(NDArrayIndex.newAxis(), NDArrayIndex.all(), NDArrayIndex.newAxis());
    System.out.println(v1.toString());
    System.out.println(v1.shapeInfoDataBuffer());
    System.out.println("Fixed dimension " + v1.flags());
    System.out.println("sparse offsets " + v1.sparseOffsets());
    System.out.println("hidden dimensions " + v1.hiddenDimensions());
    System.out.println("number of hidden dimensions " + ((BaseSparseNDArrayCOO) v1).getNumHiddenDimension());

}
 
Example 10
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateSparseMatrix() {
    INDArray sparse = Nd4j.createSparseCOO(data, indices, shape);
    assertArrayEquals(shape, sparse.shape());
    assertEquals(data.length, sparse.nnz());

}
 
Example 11
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldTranslateViewIndexesToOriginal3() {
    int[] shape = new int[] {4, 2, 3, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 1, 0}, {1, 1, 2, 1},
                    {2, 0, 1, 0}, {2, 1, 2, 0}, {3, 0, 2, 1}, {3, 1, 0, 1}};
    INDArray original = Nd4j.createSparseCOO(values, indices, shape);
    BaseSparseNDArrayCOO view = (BaseSparseNDArrayCOO) original.get(NDArrayIndex.all(), NDArrayIndex.newAxis(),
                    NDArrayIndex.point(1), NDArrayIndex.point(2));
    assertArrayEquals(new int[] {0, 1, 2, 0}, view.translateToPhysical(new int[] {0, 0, 0}));
    assertArrayEquals(new int[] {1, 1, 2, 1}, view.translateToPhysical(new int[] {1, 0, 1}));
}
 
Example 12
Source File: SparseCOOLevel1Test.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldComputeRot() {

    // try with dense vectors to get the expected result

    INDArray temp1 = Nd4j.create(new double[] {1, 2, 0, 4});
    INDArray temp2 = Nd4j.create(new double[] {1, 2, 3, 4});
    System.out.println("before: " + temp1.data() + " " + temp2.data());
    Nd4j.getBlasWrapper().level1().rot(temp1.length(), temp1, temp2, 1, 2);
    System.out.println("after: " + temp1.data() + " " + temp2.data());

    //before: [1.0,2.0,0.0,4.0]  [1.0,2.0,3.0,4.0]
    // after: [3.0,6.0,6.0,12.0] [-1.0,-2.0,3.0,-4.0]

    INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
    INDArray vec = Nd4j.create(new double[] {1, 2, 3, 4});
    Nd4j.getBlasWrapper().level1().rot(vec.length(), sparseVec, vec, 1, 2);
    System.out.println(sparseVec.data() + " " + vec.data());

    //System.out.println("indexes: " + ((BaseSparseNDArray) sparseVec).getVectorCoordinates().toString());
    INDArray expectedSparseVec = Nd4j.createSparseCSR(new double[] {3, 6, 6, 12}, new int[] {0, 1, 2, 3},
                    new int[] {0}, new int[] {4}, new int[] {1, 4});
    INDArray expectedVec = Nd4j.create(new double[] {-1, -2, 3, -4});
    assertEquals(getFailureMessage(), expectedSparseVec.data(), sparseVec.data());

    assertEquals(getFailureMessage(), expectedVec, vec);
    // TODO FIXME
}
 
Example 13
Source File: SparseCOOLevel1Test.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldComputeAxpy() {
    INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
    INDArray vec = Nd4j.create(new double[] {1, 2, 3, 4});
    INDArray expected = Nd4j.create(new double[] {2, 4, 3, 8});
    Nd4j.getBlasWrapper().level1().axpy(vec.length(), 1, sparseVec, vec);
    assertEquals(getFailureMessage(), expected, vec);
}
 
Example 14
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldTranslateViewIndexesToOriginal() {
    int[] shape = new int[] {4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 2}, {2, 0, 1}, {2, 1, 2},
                    {3, 0, 2}, {3, 1, 0}};
    INDArray original = Nd4j.createSparseCOO(values, indices, shape);
    BaseSparseNDArrayCOO view = (BaseSparseNDArrayCOO) original.get(NDArrayIndex.all(), NDArrayIndex.point(1));
    int[] originalIdx = view.translateToPhysical(new int[] {0, 0});
    int[] exceptedIdx = new int[] {0, 1, 0};
    assertArrayEquals(exceptedIdx, originalIdx);


}
 
Example 15
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void newAxisWithSparseArray() {
    int[] shape = new int[] {4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 2}, {2, 0, 1}, {2, 1, 2},
                    {3, 0, 2}, {3, 1, 0}};
    INDArray array = Nd4j.createSparseCOO(values, indices, shape);
    INDArray v = array.get(NDArrayIndex.point(0), NDArrayIndex.newAxis());
    System.out.println(v.shapeInfoDataBuffer());

}
 
Example 16
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetWithSpecifiedIndexes2() {
    int[] shape = new int[] {4, 2, 3};
    double[] values = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] indices = new int[][] {{0, 0, 2}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 2}, {2, 0, 1}, {2, 1, 2},
                    {3, 0, 2}, {3, 1, 0}};
    INDArray array = Nd4j.createSparseCOO(values, indices, shape);

    BaseSparseNDArrayCOO newArray = (BaseSparseNDArrayCOO) array.get(NDArrayIndex.interval(1, 4),
                    new SpecifiedIndex(new int[] {0}), new SpecifiedIndex(new int[] {0, 2}));
    assertEquals(2, newArray.nnz());
    assertArrayEquals(new double[] {3, 8}, newArray.getIncludedValues().asDouble(), 1e-1);
    assertArrayEquals(new int[] {0, 0, 2, 1}, newArray.getIncludedIndices().asInt());
}
 
Example 17
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldPutScalar() {
    INDArray sparse = Nd4j.createSparseCOO(new double[] {1, 2}, new int[][] {{0, 0}, {0, 2}}, new int[] {1, 3});
    sparse.putScalar(1, 3);

}
 
Example 18
Source File: SparseNDArrayCOOTest.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldRemoveZero() {
    INDArray sparse = Nd4j.createSparseCOO(new double[] {1, 2}, new int[][] {{0, 0}, {0, 2}}, new int[] {1, 3});
    sparse.putScalar(0, 0);
    assertArrayEquals(new int[] {2}, sparse.getVectorCoordinates().asInt());
}
 
Example 19
Source File: SparseCOOLevel1Test.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldComputeIamin() {
    INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
    assertEquals(0, Nd4j.getBlasWrapper().level1().iamin(sparseVec), 1e-1);
}
 
Example 20
Source File: SparseCOOLevel1Test.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldComputeIamax() {
    INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
    assertEquals(2, Nd4j.getBlasWrapper().iamax(sparseVec), 1e-1);
}