remove createSparse methods. (#92)

Signed-off-by: Robert Altena <Rob@Ra-ai.com>
master
Robert Altena 2019-07-30 19:59:21 +09:00 committed by AlexDBlack
parent 386a9f057b
commit fbe120031d
6 changed files with 145 additions and 126 deletions

View File

@ -470,16 +470,6 @@ public class Nd4j {
return DISTRIBUTION_FACTORY;
}
// TODO: unused, uncommented method.
public static void setNdArrayFactoryClazz(Class<? extends NDArrayFactory> clazz) {
ndArrayFactoryClazz = clazz;
}
// TODO: unused, uncommented method.
public static void setSparseNDArrayClazz(Class<? extends NDArrayFactory> clazz) {
sparseNDArrayClazz = clazz;
}
/**
* Get the current random generator
*
@ -4980,57 +4970,6 @@ public class Nd4j {
return INSTANCE.createUninitializedDetached(shape, order());
}
// Todo: This method has zero documentation.
/**
* @param data
* @param columns
* @param pointerB
* @param pointerE
* @param shape
* @return a INDArray
* */
public static INDArray createSparseCSR(double[] data, int[] columns, int[] pointerB, int[] pointerE, long[] shape) {
return SPARSE_INSTANCE.createSparseCSR(data, columns, pointerB, pointerE, shape);
}
/**
* @param data
* @param indices
* @param shape
* @return a INDArray
* */
public static INDArray createSparseCOO(double[] data, int[][] indices, long[] shape) {
return SPARSE_INSTANCE.createSparseCOO(data, indices, shape);
}
/**
* @param data
* @param indices
* @param shape
* @return a INDArray
* */
public static INDArray createSparseCOO(double[] data, long[][] indices, long[] shape) {
return SPARSE_INSTANCE.createSparseCOO(data, indices, shape);
}
// TODO: unused method.
/**
* @param values a DataBuffer with the sparse non-null values
* @param indices a DataBuffer with the indexes of the values
* @param sparseOffsets the sparse
* @param flags an array that define the inactive dimension
* @param shape
* @param hiddenDimensions an array containing the position of the hidden dimensions
* @param underlyingRank the rank of the original ndarray
* @return a INDArray
* */
public static INDArray createSparseCOO(DataBuffer values, DataBuffer indices, long[] sparseOffsets, int[] flags,
long[] shape, int[] hiddenDimensions, int underlyingRank) {
INDArray matrix = SPARSE_INSTANCE.createSparseCOO(values, indices, sparseOffsets, flags, hiddenDimensions,
underlyingRank, shape);
return matrix;
}
////////////////////// OTHER ///////////////////////////////
/**

View File

@ -55,34 +55,43 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
@Test
public void shouldCreateSparseMatrix() {
INDArray sparse = Nd4j.createSparseCOO(data, indices, shape);
// Commented out on removal of Nd4j createSparse methods
/*INDArray sparse = Nd4j.createSparseCOO(data, indices, shape);
assertArrayEquals(shape, sparse.shape());
assertEquals(data.length, sparse.nnz());
*/
}
@Test
public void shouldPutScalar() {
// Commented out on removal of Nd4j createSparse methods
/*
INDArray sparse = Nd4j.createSparseCOO(new double[] {1, 2}, new int[][] {{0, 0}, {0, 2}}, new long[] {1, 3});
sparse.putScalar(1, 3);
*/
}
@Test
public void shouldntPutZero() {
// Commented out on removal of Nd4j createSparse methods
/*
INDArray sparse = Nd4j.createSparseCOO(new double[] {1, 2}, new int[][] {{0, 0}, {0, 2}}, new long[] {1, 3});
int oldNNZ = sparse.nnz();
sparse.putScalar(1, 0);
assertArrayEquals(new int[] {0, 2}, sparse.getVectorCoordinates().asInt());
assertTrue(sparse.isRowVector());
assertEquals(oldNNZ, sparse.nnz());
*/
}
@Test
public void shouldRemoveZero() {
// Commented out on removal of Nd4j createSparse methods
/*
INDArray sparse = Nd4j.createSparseCOO(new double[] {1, 2}, new int[][] {{0, 0}, {0, 2}}, new long[] {1, 3});
sparse.putScalar(0, 0);
assertArrayEquals(new int[] {2}, sparse.getVectorCoordinates().asInt());
*/
}
@Test
@ -95,6 +104,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
// test with sparse :
double[] values = {1, 2, 3, 4};
int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
// Commented out on removal of Nd4j createSparse methods
/*
INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
// subarray in the top right corner
@ -106,6 +117,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(ArrayUtil.flatten(indices), sparseView.getUnderlyingIndices().asInt());
assertEquals(0, sparseView.nnz());
System.out.println(sparseView.sparseInfoDataBuffer());
*/
}
@Test
@ -113,6 +125,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
double[] values = {1, 2, 3, 4};
int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
// Commented out on removal of Nd4j createSparse methods
/*
INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
BaseSparseNDArrayCOO sparseView = (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.interval(2, 5),
@ -122,6 +136,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new int[] {0, 1}, sparseView.getIncludedIndices().asInt());
System.out.println(sparseView.sparseInfoDataBuffer());
*/
}
@Test
@ -129,7 +144,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
double[] values = {1, 2, 3, 4};
int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
// Commented out on removal of Nd4j createSparse methods
/* INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
BaseSparseNDArrayCOO sparseView = (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.interval(0, 2),
NDArrayIndex.interval(2, 5));
assertEquals(2, sparseView.nnz());
@ -137,13 +153,15 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new int[] {0, 1, 1, 0}, sparseView.getIncludedIndices().asInt());
System.out.println(sparseView.sparseInfoDataBuffer());
*/
}
@Test
public void shouldTakeViewInTheMiddle() {
double[] values = {1, 2, 3, 4};
int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
// Commented out on removal of Nd4j createSparse methods
/* INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
BaseSparseNDArrayCOO sparseView = (BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.interval(1, 3),
NDArrayIndex.interval(1, 3));
assertEquals(2, sparseView.nnz());
@ -151,25 +169,29 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new int[] {0, 1, 1, 0}, sparseView.getIncludedIndices().asInt());
System.out.println(sparseView.sparseInfoDataBuffer());
*/
}
@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 long[] {5, 5});
// Commented out on removal of Nd4j createSparse methods
/* INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
BaseSparseNDArrayCOO sparseView =
(BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.all(), NDArrayIndex.point(0));
assertEquals(0, sparseView.nnz());
System.out.println(sparseView.sparseInfoDataBuffer());
*/
}
@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 long[] {5, 5});
// Commented out on removal of Nd4j createSparse methods
/* INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
BaseSparseNDArrayCOO sparseView =
(BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.point(2), NDArrayIndex.all());
assertEquals(1, sparseView.nnz());
@ -177,19 +199,22 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new double[] {3}, sparseView.getIncludedValues().asDouble(), 1e-1);
System.out.println(sparseView.sparseInfoDataBuffer());
*/
}
@Test
public void shouldGetScalar() {
double[] values = {1, 2, 3, 4};
int[][] indices = {{0, 3}, {1, 2}, {2, 1}, {3, 4}};
INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
// Commented out on removal of Nd4j createSparse methods
/* INDArray sparseNDArray = Nd4j.createSparseCOO(values, indices, new long[] {5, 5});
BaseSparseNDArrayCOO sparseView =
(BaseSparseNDArrayCOO) sparseNDArray.get(NDArrayIndex.point(2), NDArrayIndex.point(1));
assertEquals(1, sparseView.nnz());
assertArrayEquals(new int[] {0, 0}, sparseView.getIncludedIndices().asInt());
assertArrayEquals(new double[] {3}, sparseView.getIncludedValues().asDouble(), 1e-1);
assertTrue(sparseView.isScalar());
*/
}
@Test
@ -198,7 +223,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
double[] values = new double[] {2, 1, 4, 3};
int[][] indices = new int[][] {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}};
INDArray array = Nd4j.createSparseCOO(values, indices, shape);
// Commented out on removal of Nd4j createSparse methods
/* INDArray array = Nd4j.createSparseCOO(values, indices, shape);
BaseSparseNDArrayCOO view =
(BaseSparseNDArrayCOO) array.get(NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all());
assertEquals(2, view.nnz());
@ -207,6 +233,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new double[] {2, 1}, view.getIncludedValues().asDouble(), 1e-1);
System.out.println(view.sparseInfoDataBuffer());
*/
}
@Test
@ -215,7 +242,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
double[] values = new double[] {2, 1, 4, 3};
int[][] indices = new int[][] {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}};
INDArray array = Nd4j.createSparseCOO(values, indices, shape);
// Commented out on removal of Nd4j createSparse methods
/* INDArray array = Nd4j.createSparseCOO(values, indices, shape);
BaseSparseNDArrayCOO baseView =
(BaseSparseNDArrayCOO) array.get(NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all());
BaseSparseNDArrayCOO view = (BaseSparseNDArrayCOO) baseView.get(NDArrayIndex.point(1), NDArrayIndex.all());
@ -223,6 +251,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new long[] {1, 2}, view.shape());
assertArrayEquals(new int[] {0, 1}, view.getIncludedIndices().asInt());
assertArrayEquals(new double[] {1}, view.getIncludedValues().asDouble(), 1e-1);
*/
}
@Test
@ -232,7 +261,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* 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));
@ -241,6 +271,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(new int[] {0, 0, 1, 0}, view.getIncludedIndices().asInt());
assertArrayEquals(new double[] {5, 7}, view.getIncludedValues().asDouble(), 1e-1);
assertTrue(view.isColumnVector());
*/
}
@Test
@ -249,12 +280,14 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/*INDArray array = Nd4j.createSparseCOO(values, indices, shape);
BaseSparseNDArrayCOO newArray = (BaseSparseNDArrayCOO) array.get(new SpecifiedIndex(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());
*/
}
@Test
@ -263,13 +296,15 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* INDArray array = Nd4j.createSparseCOO(values, indices, shape);
BaseSparseNDArrayCOO newArray = (BaseSparseNDArrayCOO) array.get(NDArrayIndex.interval(1, 4),
new SpecifiedIndex(0), new SpecifiedIndex(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());
*/
}
@Test
@ -289,10 +324,11 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* INDArray array = Nd4j.createSparseCOO(values, indices, shape);
INDArray v = array.get(NDArrayIndex.point(0), NDArrayIndex.newAxis());
System.out.println(v.shapeInfoDataBuffer());
*/
}
@Test
@ -301,7 +337,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* 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));
@ -331,7 +368,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
System.out.println("sparse offsets " + v1.sparseOffsets());
System.out.println("hidden dimensions " + v1.hiddenDimensions());
System.out.println("number of hidden dimensions " + ((BaseSparseNDArrayCOO) v1).getNumHiddenDimension());
*/
}
@ -367,13 +404,13 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/*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);
*/
}
@Test
@ -382,11 +419,13 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* INDArray original = Nd4j.createSparseCOO(values, indices, shape);
BaseSparseNDArrayCOO view = (BaseSparseNDArrayCOO) original.get(NDArrayIndex.all(), NDArrayIndex.newAxis(),
NDArrayIndex.point(1));
assertArrayEquals(new int[] {0, 1, 0}, view.translateToPhysical(new int[] {0, 0, 0}));
assertArrayEquals(new int[] {1, 1, 1}, view.translateToPhysical(new int[] {1, 0, 1}));
*/
}
@Test
@ -395,11 +434,13 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* 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}));
*/
}
@Test
@ -409,7 +450,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* INDArray original = Nd4j.createSparseCOO(values, indices, shape);
BaseSparseNDArrayCOO view = (BaseSparseNDArrayCOO) original.get(NDArrayIndex.newAxis(), NDArrayIndex.all(),
NDArrayIndex.point(1));
@ -421,6 +463,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
int[] originalIdx = view.translateToPhysical(new int[] {0, 1, 2});
int[] exceptedIdx = new int[] {1, 0, 2};
assertArrayEquals(exceptedIdx, originalIdx);
*/
}
@Test
@ -428,7 +471,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
long[] shape = new long[] {4, 3, 3};
double[] values = new double[] {1};
long[][] indices = new long[][] {{0, 0, 0}};
INDArray original = Nd4j.createSparseCOO(values, indices, shape);
// commented out on removal of createSparse methods from Nd4j
/* INDArray original = Nd4j.createSparseCOO(values, indices, shape);
original.putScalar(2, 2, 2, 3);
original.putScalar(1, 1, 1, 2);
@ -438,6 +482,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
assertArrayEquals(expectedIdx, view.getIncludedIndices().asInt());
assertArrayEquals(expectedValues, view.getIncludedValues().asDouble(), 1e-5);
assertTrue(view == original);
*/
}
@Test
@ -573,11 +618,13 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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}};
BaseSparseNDArrayCOO array = (BaseSparseNDArrayCOO) Nd4j.createSparseCOO(values, indices, shape);
// Commented out on removal of Nd4j createSparse methods
/* BaseSparseNDArrayCOO array = (BaseSparseNDArrayCOO) Nd4j.createSparseCOO(values, indices, shape);
assertEquals(0, array.reverseIndexes(0, 0, 2));
assertEquals(7, array.reverseIndexes(3, 0, 2));
assertEquals(8, array.reverseIndexes(3, 1, 0));
*/
}
@Test
@ -596,7 +643,8 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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);
// Commented out on removal of Nd4j createSparse methods
/* 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());
@ -605,6 +653,7 @@ public class SparseNDArrayCOOTest extends BaseNd4jTest {
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());
*/
}
}

View File

@ -59,12 +59,6 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
private long[] shape = {5, 5};
@Test
public void shouldCreateSparseMatrix() {
INDArray matrix = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
//TODO
}
@Test
public void shouldAddValueAtAGivenPosition() {
/*
@ -74,6 +68,8 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
* [-4 0 2 7 0 ]
* [ 0 8 0 0 -5]]
* */
// commented out on removal of createSparse methods from Nd4j
/*
INDArray sparseNDArray = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
if (sparseNDArray instanceof BaseSparseNDArrayCSR) {
BaseSparseNDArrayCSR sparseCSRArray = (BaseSparseNDArrayCSR) sparseNDArray;
@ -92,10 +88,13 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
assertArrayEquals(expectedPointerE, sparseCSRArray.getPointerEArray());
assertArrayEquals(expectedShape, shape);
}
*/
}
@Test
public void shouldReallocate() {
// commented out on removal of createSparse methods from Nd4j
/*
INDArray sparseNDArray = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
if (sparseNDArray instanceof BaseSparseNDArrayCSR) {
BaseSparseNDArrayCSR sparseCSRArray = (BaseSparseNDArrayCSR) sparseNDArray;
@ -109,7 +108,7 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
int finalSize = sparseCSRArray.getDoubleValues().length;
assertTrue(finalSize > initialSize);
}
*/
}
@Test
@ -121,6 +120,8 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
* [-4 0 2 7 0 ]
* [ 0 8 0 0 -5]]
* */
// commented out on removal of createSparse methods from Nd4j
/*
INDArray sparseNDArray = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
if (sparseNDArray instanceof BaseSparseNDArrayCSR) {
BaseSparseNDArrayCSR sparseCSRArray = (BaseSparseNDArrayCSR) sparseNDArray;
@ -138,6 +139,7 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
assertArrayEquals(expectedPointerE, sparseCSRArray.getPointerEArray());
assertArrayEquals(expectedShape, shape);
}
*/
}
@Test
@ -164,6 +166,8 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
INDArray denseView = array.get(NDArrayIndex.interval(1, 3), NDArrayIndex.interval(1, 3));
// test with sparse :
// commented out on removal of createSparse methods from Nd4j
/*
INDArray sparseNDArray = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
// subarray in the top right corner
@ -214,8 +218,7 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
assertArrayEquals(new int[] {0, 0}, sparseView.getVectorCoordinates().asInt());
assertArrayEquals(new int[] {0, 5, 9}, sparseView.getPointerBArray());
assertArrayEquals(new int[] {0, 6, 10}, sparseView.getPointerEArray());
*/
}
@Test
@ -224,12 +227,14 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
int[] columns = {0, 1, 3, 0, 1, 2, 3, 4, 0, 2, 3, 1, 4};
int[] pointerB = {0, 3, 5, 8, 11};
INDArray sparseNDArray = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
// commented out on removal of createSparse methods from Nd4j
//INDArray sparseNDArray = Nd4j.createSparseCSR(values, columns, pointerB, pointerE, shape);
/* [0, -3, 0]
* sparseView = [0, 0, 0] subview = [[0,0], [4,6]]
* [4, 6, 4]
*/
BaseSparseNDArrayCSR sparseView = (BaseSparseNDArrayCSR) sparseNDArray.get(NDArrayIndex.interval(0, 3),
/*BaseSparseNDArrayCSR sparseView = (BaseSparseNDArrayCSR) sparseNDArray.get(NDArrayIndex.interval(0, 3),
NDArrayIndex.interval(2, 5));
BaseSparseNDArrayCSR subview =
(BaseSparseNDArrayCSR) sparseView.get(NDArrayIndex.interval(1, 3), NDArrayIndex.interval(0, 2));
@ -260,5 +265,6 @@ public class SparseNDArrayCSRTest extends BaseNd4jTest {
assertArrayEquals(new int[] {}, subview.getVectorCoordinates().asInt());
assertArrayEquals(new int[] {0}, subview.getPointerBArray());
assertArrayEquals(new int[] {0}, subview.getPointerEArray());
*/
}
}

View File

@ -47,44 +47,52 @@ public class SparseCOOLevel1Test extends BaseNd4jTest {
@Test
public void shouldComputeDot() {
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
// Commented out on removal of Nd4j createSparse methods
/*INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
//INDArray vec = Nd4j.create( new double[] {1 ,2, 3, 4});
INDArray matrix = Nd4j.linspace(1, 4, 4, DataType.FLOAT).reshape(1, 4);
INDArray vec = matrix.getRow(0);
assertEquals(21, Nd4j.getBlasWrapper().dot(sparseVec, vec), 1e-1);
*/
}
@Test
public void shouldComputeNrm2() {
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
assertEquals(Math.sqrt(21), Nd4j.getBlasWrapper().nrm2(sparseVec), 1e-1);
// Commented out on removal of Nd4j createSparse methods
//INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
//assertEquals(Math.sqrt(21), Nd4j.getBlasWrapper().nrm2(sparseVec), 1e-1);
}
@Test
public void shouldComputeAsum() {
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
assertEquals(7, Nd4j.getBlasWrapper().asum(sparseVec), 1e-1);
// Commented out on removal of Nd4j createSparse methods
//INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
//assertEquals(7, Nd4j.getBlasWrapper().asum(sparseVec), 1e-1);
}
@Test
public void shouldComputeIamax() {
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
assertEquals(2, Nd4j.getBlasWrapper().iamax(sparseVec), 1e-1);
// Commented out on removal of Nd4j createSparse methods
// INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
// assertEquals(2, Nd4j.getBlasWrapper().iamax(sparseVec), 1e-1);
}
@Test
public void shouldComputeIamin() {
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
assertEquals(0, Nd4j.getBlasWrapper().level1().iamin(sparseVec), 1e-1);
// Commented out on removal of Nd4j createSparse methods
// INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
// assertEquals(0, Nd4j.getBlasWrapper().level1().iamin(sparseVec), 1e-1);
}
@Test
public void shouldComputeAxpy() {
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
// Commented out on removal of Nd4j createSparse methods
/* 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);
*/
}
@Test
@ -101,18 +109,21 @@ public class SparseCOOLevel1Test extends BaseNd4jTest {
//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);
// Commented out on removal of Nd4j createSparse methods
/*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 long[] {1, 4});
INDArray expectedVec = Nd4j.create(new double[] {-1, -2, 3, -4});
assertEquals(getFailureMessage(), expectedSparseVec.data(), sparseVec.data());
assertEquals(getFailureMessage(), expectedVec, vec);
*/
// TODO FIXME
}
@ -132,10 +143,10 @@ public class SparseCOOLevel1Test extends BaseNd4jTest {
int[] cols = {0, 1, 2, 3};
double[] values = {1, 2, 3, 4};
INDArray sparseVec = Nd4j.createSparseCOO(data, indexes, shape);
// commented out on removal of createSparse methods from Nd4j
/* 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);
INDArray expectedSparseVec = Nd4j.createSparseCSR(new double[] {3, 6, 9, 12}, new int[] {0, 1, 2, 3},
new int[] {0}, new int[] {4}, new long[] {1, 4});
INDArray expectedVec = Nd4j.create(new double[] {-1, -2, -3, -4});
@ -146,6 +157,7 @@ public class SparseCOOLevel1Test extends BaseNd4jTest {
BaseSparseNDArray vecSparse2 = ((BaseSparseNDArray) sparseVec);
assertEquals(getFailureMessage(), vec2.getVectorCoordinates(), vecSparse2);
}
*/
}
@Override

View File

@ -46,13 +46,15 @@ public class SparseCOOLevel2Test extends BaseNd4jTest {
@Test
public void testGemv() {
INDArray array1 = Nd4j.createSparseCOO(data, indexes, shape);
// commented out on removal of createSparse methods from Nd4j
/* INDArray array1 = Nd4j.createSparseCOO(data, indexes, shape);
INDArray array2 = Nd4j.linspace(1, 2, 2).reshape(2, 1);
INDArray array3 = array1.mmul(array2); // should be [5, 0]
assertEquals(2, array3.length());
assertEquals(5, array3.getFloat(0), 1e-5);
assertEquals(0, array3.getFloat(1), 1e-5);
*/
}

View File

@ -48,44 +48,51 @@ public class SparseCSRLevel1Test extends BaseNd4jTest {
@Test
public void shouldComputeDot() {
INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
// commented out on removal of createSparse methods from Nd4j
//INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, 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);
//assertEquals(21, Nd4j.getBlasWrapper().dot(sparseVec, vec), 1e-1);
}
@Test
public void shouldComputeNrm2() {
INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
assertEquals(Math.sqrt(21), Nd4j.getBlasWrapper().nrm2(sparseVec), 1e-1);
// commented out on removal of createSparse methods from Nd4j
//INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
//assertEquals(Math.sqrt(21), Nd4j.getBlasWrapper().nrm2(sparseVec), 1e-1);
}
@Test
public void shouldComputeAsum() {
INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
assertEquals(7, Nd4j.getBlasWrapper().asum(sparseVec), 1e-1);
// commented out on removal of createSparse methods from Nd4j
//INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
//assertEquals(7, Nd4j.getBlasWrapper().asum(sparseVec), 1e-1);
}
@Test
public void shouldComputeIamax() {
INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
assertEquals(2, Nd4j.getBlasWrapper().iamax(sparseVec), 1e-1);
// commented out on removal of createSparse methods from Nd4j
//INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
//assertEquals(2, Nd4j.getBlasWrapper().iamax(sparseVec), 1e-1);
}
@Test
public void shouldComputeIamin() {
INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
assertEquals(0, Nd4j.getBlasWrapper().level1().iamin(sparseVec), 1e-1);
// commented out on removal of createSparse methods from Nd4j
//INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
//assertEquals(0, Nd4j.getBlasWrapper().level1().iamin(sparseVec), 1e-1);
}
@Test
public void shouldComputeAxpy() {
INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, shape);
// commented out on removal of createSparse methods from Nd4j
/* INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, 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);
*/
}
@Test
@ -102,7 +109,8 @@ public class SparseCSRLevel1Test extends BaseNd4jTest {
//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.createSparseCSR(data, col, pointerB, pointerE, shape);
// commented out on removal of createSparse methods from Nd4j
/* INDArray sparseVec = Nd4j.createSparseCSR(data, col, pointerB, pointerE, 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());
@ -114,6 +122,7 @@ public class SparseCSRLevel1Test extends BaseNd4jTest {
assertEquals(getFailureMessage(), expectedSparseVec.data(), sparseVec.data());
assertEquals(getFailureMessage(), expectedVec, vec);
*/
// TODO fix it
}
@ -133,7 +142,8 @@ public class SparseCSRLevel1Test extends BaseNd4jTest {
int[] cols = {0, 1, 2, 3};
double[] values = {1, 2, 3, 4};
INDArray sparseVec = Nd4j.createSparseCSR(values, cols, pointerB, pointerE, shape);
// commented out on removal of createSparse methods from Nd4j
/* INDArray sparseVec = Nd4j.createSparseCSR(values, cols, pointerB, pointerE, shape);
INDArray vec = Nd4j.create(new double[] {1, 2, 3, 4});
Nd4j.getBlasWrapper().level1().rot(vec.length(), sparseVec, vec, 1, 2);
@ -147,6 +157,7 @@ public class SparseCSRLevel1Test extends BaseNd4jTest {
BaseSparseNDArray vecSparse2 = ((BaseSparseNDArray) sparseVec);
assertEquals(getFailureMessage(), vec2.getVectorCoordinates(), vecSparse2);
}
*/
}