Java Code Examples for org.nd4j.linalg.indexing.NDArrayIndex#point()
The following examples show how to use
org.nd4j.linalg.indexing.NDArrayIndex#point() .
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: NDArrayIndexResolveTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testResolvePointVector() { INDArray arr = Nd4j.linspace(1, 4, 4); INDArrayIndex[] getPoint = {NDArrayIndex.point(1)}; INDArrayIndex[] resolved = NDArrayIndex.resolve(arr.shape(), getPoint); if (getPoint.length == resolved.length) assertArrayEquals(getPoint, resolved); else { assertEquals(2, resolved.length); assertTrue(resolved[0] instanceof PointIndex); assertEquals(0, resolved[0].current()); assertTrue(resolved[1] instanceof PointIndex); assertEquals(1, resolved[1].current()); } }
Example 2
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 6 votes |
@Test @Ignore public void testIndexPointInterval() { INDArray zeros = Nd4j.zeros(3, 3, 3); INDArrayIndex x = NDArrayIndex.point(1); INDArrayIndex y = NDArrayIndex.interval(1, 2, true); INDArrayIndex z = NDArrayIndex.point(1); INDArray value = Nd4j.ones(1, 2); zeros.put(new INDArrayIndex[] {x, y, z}, value); String f1 = "[[[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]\n" + " [[0,00,0,00,0,00]\n" + " [0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]]\n" + " [[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]]"; String f2 = "[[[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]\n" + " [[0.00,0.00,0.00]\n" + " [0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]]\n" + " [[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]]"; if (!zeros.toString().equals(f2) && !zeros.toString().equals(f1)) assertEquals(f2, zeros.toString()); }
Example 3
Source File: NDArrayIndexResolveTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testResolvePointVector() { INDArray arr = Nd4j.linspace(1, 4, 4); INDArrayIndex[] getPoint = {NDArrayIndex.point(1)}; INDArrayIndex[] resolved = NDArrayIndex.resolve(arr.shape(), getPoint); if (getPoint.length == resolved.length) assertArrayEquals(getPoint, resolved); else { assertEquals(2, resolved.length); assertTrue(resolved[0] instanceof PointIndex); assertEquals(0, resolved[0].offset()); assertTrue(resolved[1] instanceof PointIndex); assertEquals(1, resolved[1].offset()); } }
Example 4
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 6 votes |
@Test @Ignore public void testIndexPointAll() { INDArray zeros = Nd4j.zeros(3, 3, 3); INDArrayIndex x = NDArrayIndex.point(1); INDArrayIndex y = NDArrayIndex.all(); INDArrayIndex z = NDArrayIndex.point(1); INDArray value = Nd4j.ones(1, 3); zeros.put(new INDArrayIndex[] {x, y, z}, value); String f1 = "[[[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]\n" + " [[0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]]\n" + " [[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]]"; String f2 = "[[[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]\n" + " [[0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]]\n" + " [[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]]"; if (!zeros.toString().equals(f1) && !zeros.toString().equals(f2)) assertEquals(f2, zeros.toString()); }
Example 5
Source File: CpuLapack.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public void dgeqrf(int M, int N, INDArray A, INDArray R, INDArray INFO) { INDArray tau = Nd4j.create(DataType.DOUBLE, N ) ; int status = LAPACKE_dgeqrf(getColumnOrder(A), M, N, (DoublePointer)A.data().addressPointer(), getLda(A), (DoublePointer)tau.data().addressPointer() ); if( status != 0 ) { throw new BlasException( "Failed to execute dgeqrf", status ) ; } // Copy R ( upper part of Q ) into result if( R != null ) { R.assign( A.get( NDArrayIndex.interval( 0, A.columns() ), NDArrayIndex.all() ) ) ; INDArrayIndex ix[] = new INDArrayIndex[ 2 ] ; for( int i=1 ; i<Math.min( A.rows(), A.columns() ) ; i++ ) { ix[0] = NDArrayIndex.point( i ) ; ix[1] = NDArrayIndex.interval( 0, i ) ; R.put(ix, 0) ; } } status = LAPACKE_dorgqr( getColumnOrder(A), M, N, N, (DoublePointer)A.data().addressPointer(), getLda(A), (DoublePointer)tau.data().addressPointer() ); if( status != 0 ) { throw new BlasException( "Failed to execute dorgqr", status ) ; } }
Example 6
Source File: IndexShapeTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testSinglePoint() { /* Assumes all indexes are filled out. Test simple general point case */ int[] assertion = {2, 1, 4, 5, 1}; INDArrayIndex[] indexes = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all()}; int[] testShape = Indices.shape(shape, indexes); assertArrayEquals(assertion, testShape); int[] secondAssertion = {1, 2, 1, 5, 1}; INDArrayIndex[] otherCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.point(0) }; assertArrayEquals(secondAssertion, Indices.shape(shape, otherCase)); int[] thridAssertion = {1, 2, 1, 4, 5, 1}; INDArrayIndex[] thirdCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), }; assertArrayEquals(thridAssertion, Indices.shape(shape, thirdCase)); }
Example 7
Source File: NDArrayIndexResolveTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testResolvePoint() { INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2); INDArrayIndex[] test = NDArrayIndex.resolve(arr.shape(), NDArrayIndex.point(1)); INDArrayIndex[] assertion = {NDArrayIndex.point(1), NDArrayIndex.all()}; assertArrayEquals(assertion, test); INDArrayIndex[] allAssertion = {NDArrayIndex.all(), NDArrayIndex.all()}; assertArrayEquals(allAssertion, NDArrayIndex.resolve(arr.shape(), NDArrayIndex.all())); INDArrayIndex[] allAndOne = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(1)}; assertArrayEquals(allAndOne, NDArrayIndex.resolve(arr.shape(), allAndOne)); }
Example 8
Source File: EvaluationBinaryTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testEvaluationBinary4d() { INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 3, 10, 10); INDArray label = Nd4j.rand(DataType.FLOAT, 2, 3, 10, 10); List<INDArray> rowsP = new ArrayList<>(); List<INDArray> rowsL = new ArrayList<>(); NdIndexIterator iter = new NdIndexIterator(2, 10, 10); while (iter.hasNext()) { long[] idx = iter.next(); INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1]), NDArrayIndex.point(idx[2])}; rowsP.add(prediction.get(idxs)); rowsL.add(label.get(idxs)); } INDArray p2d = Nd4j.vstack(rowsP); INDArray l2d = Nd4j.vstack(rowsL); EvaluationBinary e4d = new EvaluationBinary(); EvaluationBinary e2d = new EvaluationBinary(); e4d.eval(label, prediction); e2d.eval(l2d, p2d); for (EvaluationBinary.Metric m : EvaluationBinary.Metric.values()) { for( int i=0; i<3; i++ ) { double d1 = e4d.scoreForMetric(m, i); double d2 = e2d.scoreForMetric(m, i); assertEquals(m.toString(), d2, d1, 1e-6); } } }
Example 9
Source File: ROCBinaryTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testROCBinary4d() { INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 3, 10, 10); INDArray label = Nd4j.rand(DataType.FLOAT, 2, 3, 10, 10); List<INDArray> rowsP = new ArrayList<>(); List<INDArray> rowsL = new ArrayList<>(); NdIndexIterator iter = new NdIndexIterator(2, 10, 10); while (iter.hasNext()) { long[] idx = iter.next(); INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1]), NDArrayIndex.point(idx[2])}; rowsP.add(prediction.get(idxs)); rowsL.add(label.get(idxs)); } INDArray p2d = Nd4j.vstack(rowsP); INDArray l2d = Nd4j.vstack(rowsL); ROCBinary e4d = new ROCBinary(); ROCBinary e2d = new ROCBinary(); e4d.eval(label, prediction); e2d.eval(l2d, p2d); for (ROCBinary.Metric m : ROCBinary.Metric.values()) { for( int i=0; i<3; i++ ) { double d1 = e4d.scoreForMetric(m, i); double d2 = e2d.scoreForMetric(m, i); assertEquals(m.toString(), d2, d1, 1e-6); } } }
Example 10
Source File: IndexShapeTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSinglePoint() { /* Assumes all indexes are filled out. Test simple general point case */ int[] assertion = {2, 1, 4, 5, 1}; INDArrayIndex[] indexes = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all()}; int[] testShape = Indices.shape(shape, indexes); assertArrayEquals(assertion, testShape); int[] secondAssertion = {1, 2, 1, 5, 1}; INDArrayIndex[] otherCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.point(0) }; assertArrayEquals(secondAssertion, Indices.shape(shape, otherCase)); int[] thridAssertion = {1, 2, 1, 4, 5, 1}; INDArrayIndex[] thirdCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(0), }; assertArrayEquals(thridAssertion, Indices.shape(shape, thirdCase)); }
Example 11
Source File: BaseNDArrayList.java From deeplearning4j with Apache License 2.0 | 5 votes |
private void moveBackward(int index) { int numMoved = size - index - 1; INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index ,index + numMoved)}; INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1 ,index + 1 + numMoved)}; INDArray get = container.get(getRange); container.put(first,get); }
Example 12
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testVectorIndexPointPointOutOfRange() { INDArray zeros = Nd4j.zeros(1, 4); INDArrayIndex x = NDArrayIndex.point(0); INDArrayIndex y = NDArrayIndex.point(4); INDArray value = Nd4j.ones(1, 1); try { zeros.put(new INDArrayIndex[] {x, y}, value); fail("Out of range index should throw an IllegalArgumentException"); } catch (IllegalArgumentException e) { //do nothing } }
Example 13
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testVectorIndexPointPoint() { INDArray zeros = Nd4j.zeros(1, 4); INDArrayIndex x = NDArrayIndex.point(0); INDArrayIndex y = NDArrayIndex.point(2); INDArray value = Nd4j.ones(1, 1); zeros.put(new INDArrayIndex[] {x, y}, value); INDArray assertion = Nd4j.create(new double[] {0.0, 0.0, 1.0, 0.0}); assertEquals(assertion, zeros); }
Example 14
Source File: ShapeResolutionTestsC.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testFlatIndexPointInterval() { INDArray zeros = Nd4j.zeros(1, 4); INDArrayIndex x = NDArrayIndex.point(0); INDArrayIndex y = NDArrayIndex.interval(1, 2, true); INDArray value = Nd4j.ones(1, 2); zeros.put(new INDArrayIndex[] {x, y}, value); INDArray assertion = Nd4j.create(new double[] {0.0, 1.0, 1.0, 0.0}); assertEquals(assertion, zeros); }
Example 15
Source File: EvaluationCalibrationTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testEvaluationCalibration3dMasking() { INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 3, 10); INDArray label = Nd4j.rand(DataType.FLOAT, 2, 3, 10); List<INDArray> rowsP = new ArrayList<>(); List<INDArray> rowsL = new ArrayList<>(); //Check "DL4J-style" 2d per timestep masking [minibatch, seqLength] mask shape INDArray mask2d = Nd4j.randomBernoulli(0.5, 2, 10); NdIndexIterator iter = new NdIndexIterator(2, 10); while (iter.hasNext()) { long[] idx = iter.next(); if(mask2d.getDouble(idx[0], idx[1]) != 0.0) { INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1])}; rowsP.add(prediction.get(idxs)); rowsL.add(label.get(idxs)); } } INDArray p2d = Nd4j.vstack(rowsP); INDArray l2d = Nd4j.vstack(rowsL); EvaluationCalibration e3d_m2d = new EvaluationCalibration(); EvaluationCalibration e2d_m2d = new EvaluationCalibration(); e3d_m2d.eval(label, prediction, mask2d); e2d_m2d.eval(l2d, p2d); assertEquals(e3d_m2d, e2d_m2d); }
Example 16
Source File: EvaluationBinaryTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testEvaluationBinary3d() { INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 5, 10); INDArray label = Nd4j.rand(DataType.FLOAT, 2, 5, 10); List<INDArray> rowsP = new ArrayList<>(); List<INDArray> rowsL = new ArrayList<>(); NdIndexIterator iter = new NdIndexIterator(2, 10); while (iter.hasNext()) { long[] idx = iter.next(); INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1])}; rowsP.add(prediction.get(idxs)); rowsL.add(label.get(idxs)); } INDArray p2d = Nd4j.vstack(rowsP); INDArray l2d = Nd4j.vstack(rowsL); EvaluationBinary e3d = new EvaluationBinary(); EvaluationBinary e2d = new EvaluationBinary(); e3d.eval(label, prediction); e2d.eval(l2d, p2d); for (EvaluationBinary.Metric m : EvaluationBinary.Metric.values()) { for( int i=0; i<5; i++ ) { double d1 = e3d.scoreForMetric(m, i); double d2 = e2d.scoreForMetric(m, i); assertEquals(m.toString(), d2, d1, 1e-6); } } }
Example 17
Source File: IndexingTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testGet() { // System.out.println("Testing sub-array put and get with a 3D array ..."); INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5); /* * Extract elements with the following indices: * * (2,1,1) (2,1,2) (2,1,3) * (2,2,1) (2,2,2) (2,2,3) * (2,3,1) (2,3,2) (2,3,3) */ int slice = 2; int iStart = 1; int jStart = 1; int iEnd = 4; int jEnd = 4; // Method A: Element-wise. INDArray subArr_A = Nd4j.create(new int[] {3, 3}); for (int i = iStart; i < iEnd; i++) { for (int j = jStart; j < jEnd; j++) { double val = arr.getDouble(slice, i, j); int[] sub = new int[] {i - iStart, j - jStart}; subArr_A.putScalar(sub, val); } } // Method B: Using NDArray get and put with index classes. INDArray subArr_B = Nd4j.create(new int[] {3, 3}); INDArrayIndex ndi_Slice = NDArrayIndex.point(slice); INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd); INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd); INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J}; INDArray whatToPut = arr.get(whereToGet); assertEquals(subArr_A, whatToPut); // System.out.println(whatToPut); INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()}; subArr_B.put(whereToPut, whatToPut); assertEquals(subArr_A, subArr_B); // System.out.println("... done"); }
Example 18
Source File: NDArrayList.java From nd4j with Apache License 2.0 | 4 votes |
private void moveBackward(int index) { int numMoved = size - index - 1; INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index ,index + numMoved)}; INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1 ,index + 1 + numMoved)}; container.put(first,container.get(getRange)); }
Example 19
Source File: IndexingTestsC.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testGet() { System.out.println("Testing sub-array put and get with a 3D array ..."); INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5); /* * Extract elements with the following indices: * * (2,1,1) (2,1,2) (2,1,3) * (2,2,1) (2,2,2) (2,2,3) * (2,3,1) (2,3,2) (2,3,3) */ int slice = 2; int iStart = 1; int jStart = 1; int iEnd = 4; int jEnd = 4; // Method A: Element-wise. INDArray subArr_A = Nd4j.create(new int[] {3, 3}); for (int i = iStart; i < iEnd; i++) { for (int j = jStart; j < jEnd; j++) { double val = arr.getDouble(slice, i, j); int[] sub = new int[] {i - iStart, j - jStart}; subArr_A.putScalar(sub, val); } } // Method B: Using NDArray get and put with index classes. INDArray subArr_B = Nd4j.create(new int[] {3, 3}); INDArrayIndex ndi_Slice = NDArrayIndex.point(slice); INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd); INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd); INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J}; INDArray whatToPut = arr.get(whereToGet); System.out.println(whatToPut); INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()}; subArr_B.put(whereToPut, whatToPut); assertEquals(subArr_A, subArr_B); System.out.println("... done"); }
Example 20
Source File: IndexingTestsC.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testGet() { // System.out.println("Testing sub-array put and get with a 3D array ..."); INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5); /* * Extract elements with the following indices: * * (2,1,1) (2,1,2) (2,1,3) * (2,2,1) (2,2,2) (2,2,3) * (2,3,1) (2,3,2) (2,3,3) */ int slice = 2; int iStart = 1; int jStart = 1; int iEnd = 4; int jEnd = 4; // Method A: Element-wise. INDArray subArr_A = Nd4j.create(new int[] {3, 3}); for (int i = iStart; i < iEnd; i++) { for (int j = jStart; j < jEnd; j++) { double val = arr.getDouble(slice, i, j); int[] sub = new int[] {i - iStart, j - jStart}; subArr_A.putScalar(sub, val); } } // Method B: Using NDArray get and put with index classes. INDArray subArr_B = Nd4j.create(new int[] {3, 3}); INDArrayIndex ndi_Slice = NDArrayIndex.point(slice); INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd); INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd); INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J}; INDArray whatToPut = arr.get(whereToGet); // System.out.println(whatToPut); INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()}; subArr_B.put(whereToPut, whatToPut); assertEquals(subArr_A, subArr_B); // System.out.println("... done"); }