Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#slice()
The following examples show how to use
org.nd4j.linalg.api.ndarray.INDArray#slice() .
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: CudaFloatDataBufferTest.java From nd4j with Apache License 2.0 | 7 votes |
@Test public void testINDArrayOffsets2() throws Exception { INDArray array = Nd4j.linspace(0, 24, 25).reshape(5, 5); assertEquals(6.0f, array.getFloat(6), 0.01f); INDArray slice0 = array.slice(0); assertEquals(0f, slice0.getFloat(0), 0.01f); INDArray slice1 = array.slice(1); assertEquals(5f, slice1.getFloat(0), 0.01f); INDArray slice2 = array.slice(2); assertEquals(10f, slice2.getFloat(0), 0.01f); INDArray slice3 = array.slice(3); assertEquals(15f, slice3.getFloat(0), 0.01f); assertEquals(array.data().getTrackingPoint(), slice0.data().getTrackingPoint()); assertEquals(array.data().getTrackingPoint(), slice1.data().getTrackingPoint()); assertEquals(array.data().getTrackingPoint(), slice2.data().getTrackingPoint()); assertEquals(array.data().getTrackingPoint(), slice3.data().getTrackingPoint()); }
Example 2
Source File: Binning.java From ml-models with Apache License 2.0 | 6 votes |
public void logBins(INDArray indArray) { for (int column = 0; column < indArray.size(1); column++) { int remaining = indArray.size(0); int binNumber = 0; INDArray slice = indArray.slice(column, 1); INDArray[] indArrays = Nd4j.sortWithIndices(slice, 0, true); INDArray indices = indArrays[0]; for (int node = 0; node < indArray.size(0); node++) { if (node + remaining == indArray.size(0)) { remaining /= 2; binNumber++; } indArray.putScalar((int) indices.getDouble(node), column, binNumber - 1); } } }
Example 3
Source File: HistoryMergeTransform.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public INDArray transform(INDArray input) { INDArray element; if(isFirstDimensionBatch) { element = input.slice(0, 0); } else { element = input; } if(shouldStoreCopy) { element = element.dup(); } historyMergeElementStore.add(element); if(!historyMergeElementStore.isReady()) { return null; } INDArray result = historyMergeAssembler.assemble(historyMergeElementStore.get()); return INDArrayHelper.forceCorrectShape(result); }
Example 4
Source File: IndexingTestsC.java From deeplearning4j with Apache License 2.0 | 6 votes |
@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 5
Source File: CudaFloatDataBufferTest.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testDup3() throws Exception { INDArray array = Nd4j.linspace(0,99, 100).reshape(10, 10); INDArray slice1 = array.slice(2); DataBuffer duplicate = slice1.data().dup(); assertEquals(10, duplicate.length()); assertEquals(20f, duplicate.getFloat(0), 0.0001f); assertEquals(21f, duplicate.getFloat(1), 0.0001f); assertEquals(22f, duplicate.getFloat(2), 0.0001f); assertEquals(23f, duplicate.getFloat(3), 0.0001f); assertEquals(24f, duplicate.getFloat(4), 0.0001f); assertEquals(25f, duplicate.getFloat(5), 0.0001f); assertEquals(26f, duplicate.getFloat(6), 0.0001f); assertEquals(27f, duplicate.getFloat(7), 0.0001f); assertEquals(28f, duplicate.getFloat(8), 0.0001f); assertEquals(29f, duplicate.getFloat(9), 0.0001f); }
Example 6
Source File: IndexingTestsC.java From nd4j with Apache License 2.0 | 6 votes |
@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 7
Source File: BaseNDArrayFactory.java From deeplearning4j with Apache License 2.0 | 6 votes |
/** * 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 8
Source File: NDArrayTestsFortran.java From nd4j with Apache License 2.0 | 6 votes |
@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 9
Source File: SlicingTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testSlice() { INDArray arr = Nd4j.linspace(1, 24, 24, DataType.DOUBLE).reshape(4, 3, 2); INDArray assertion = Nd4j.create(new double[][] {{1, 13}, {5, 17}, {9, 21}}); INDArray firstSlice = arr.slice(0); INDArray slice1Assertion = Nd4j.create(new double[][] {{2, 14}, {6, 18}, {10, 22}, }); INDArray secondSlice = arr.slice(1); assertEquals(assertion, firstSlice); assertEquals(slice1Assertion, secondSlice); }
Example 10
Source File: Binning.java From ml-models with Apache License 2.0 | 5 votes |
public void linearBins(double[][] embedding, int numBins) { INDArray indArray = Nd4j.create(embedding); for (int column = 0; column < embedding[0].length; column++) { INDArray slice = indArray.slice(column, 1); INDArray[] indArrays = Nd4j.sortWithIndices(slice, 0, true); INDArray indices = indArrays[0]; int maxRank = embedding.length; for (int rank = 0; rank < indices.size(0); rank++) { embedding[(int) indices.getDouble(rank)][column] = (int) (((double) rank / maxRank) * numBins); } } }
Example 11
Source File: NDArrayMathTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testVectorPerSlice() { INDArray arr = Nd4j.create(2, 2, 2, 2); assertEquals(4, NDArrayMath.vectorsPerSlice(arr)); INDArray matrix = Nd4j.create(2, 2); assertEquals(2, NDArrayMath.vectorsPerSlice(matrix)); INDArray arrSliceZero = arr.slice(0); assertEquals(4, NDArrayMath.vectorsPerSlice(arrSliceZero)); }
Example 12
Source File: LeadingAndTrailingOnesC.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testMatrix() { INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2); INDArray slice1 = arr.slice(1); System.out.println(arr.slice(1)); INDArray oneInMiddle = Nd4j.linspace(1, 4, 4).reshape(2, 1, 2); INDArray otherSlice = oneInMiddle.slice(1); assertEquals(2, otherSlice.offset()); System.out.println(otherSlice); INDArray twoOnesInMiddle = Nd4j.linspace(1, 4, 4).reshape(2, 1, 1, 2); INDArray sub = twoOnesInMiddle.get(NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all()); assertEquals(2, sub.offset()); }
Example 13
Source File: NDArrayMathTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testVectorPerSlice() { INDArray arr = Nd4j.create(2, 2, 2, 2); assertEquals(4, NDArrayMath.vectorsPerSlice(arr)); INDArray matrix = Nd4j.create(2, 2); assertEquals(2, NDArrayMath.vectorsPerSlice(matrix)); INDArray arrSliceZero = arr.slice(0); assertEquals(4, NDArrayMath.vectorsPerSlice(arrSliceZero)); }
Example 14
Source File: OpExecutionerTestsC.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testStridedLog() { OpExecutioner opExecutioner = Nd4j.getExecutioner(); INDArray arr = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape(2, 3); INDArray slice = arr.slice(0); Log exp = new Log(slice); opExecutioner.exec(exp); INDArray assertion = Nd4j.create(new double[] {0.0, 0.6931471824645996, 1.0986123085021973}); assertEquals(getFailureMessage(), assertion, slice); }
Example 15
Source File: OpExecutionerTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testStridedExp() { OpExecutioner opExecutioner = Nd4j.getExecutioner(); INDArray arr = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape(2, 3); INDArray slice = arr.slice(0); val expected = new double[(int) slice.length()]; for (int i = 0; i < slice.length(); i++) expected[i] = (float) Math.exp(slice.getDouble(i)); Exp exp = new Exp(slice); opExecutioner.exec(exp); assertEquals(getFailureMessage(), Nd4j.create(expected), slice); }
Example 16
Source File: BarnesHutTsneTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testSpTree() { double[] input = new double[]{0.2999816948164936, 0.26252049735806526, 0.2673853427498767, 0.8604464129156685, 0.4802652829902563, 0.10959096539488711, 0.7950242948008909, 0.5917848948003486, 0.2738285999345498, 0.9519684328285567, 0.9690024759209738, 0.8585615547624705, 0.8087760944312002, 0.5337951589543348, 0.5960876109129123, 0.7187130179825856, 0.4629777327445964, 0.08665909175584818, 0.7748005397731237, 0.48020186965468536, 0.24927351841378798, 0.32272599988270445, 0.306414968984427, 0.6980212149215657, 0.7977183964212472, 0.7673513094629704, 0.1679681724796478, 0.3107359484804584, 0.021701726051792103, 0.13797462786662518, 0.8618953518813538, 0.841333838365635, 0.5284957375170422, 0.9703367685039823, 0.677388096913733, 0.2624474979832243, 0.43740966353106536, 0.15685545957858893, 0.11072929134449871, 0.06007395961283357, 0.4093918718557811, 0.9563909195720572, 0.5994144944480242, 0.8278927844215804, 0.38586830957105667, 0.6201844716257464, 0.7603829079070265, 0.07875691596842949, 0.08651136699915507, 0.7445210640026082, 0.6547649514127559, 0.3384719042666908, 0.05816723105860, 0.6248951423054205, 0.7431868493349041}; INDArray ndinput = Nd4j.createFromArray(input).reshape(11, 5); int[] rows = {0, 10, 20, 30, 40, 50, 60, 69, 78, 88, 98, 108}; INDArray indRows = Nd4j.createFromArray(rows); int[] cols = {4, 3, 10, 8, 6, 7, 1, 5, 9, 2, 0, 4, 9, 8, 10, 2, 6, 7, 3, 5, 1, 6, 8, 3, 9, 10, 4, 0, 5, 7, 0, 1, 2, 10, 4, 6, 8, 9, 5, 7, 0, 1, 2, 3, 10, 8, 9, 6, 7, 5, 0, 2, 3, 7, 9, 10, 4, 8, 1, 6, 0, 1, 2, 3, 4, 8, 10, 9, 5, 0, 1, 3, 4, 5, 9, 10, 8, 2, 0, 1, 2, 3, 4, 5, 6, 7, 10, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; INDArray indCols = Nd4j.createFromArray(cols); double[] vals = {0.6806, 0.1978, 0.1349, 0.0403, 0.0087, 0.0369, 0.0081, 0.0172, 0.0014, 0.0046, 0.0081, 0.3375, 0.2274, 0.0556, 0.0098, 0.0175, 0.0027, 0.0077, 0.0014, 0.0023, 0.0175, 0.6569, 0.1762, 0.0254, 0.0200, 0.0118, 0.0074, 0.0046, 0.0124, 0.0012, 0.1978, 0.0014, 0.0254, 0.7198, 0.0712, 0.0850, 0.0389, 0.0555, 0.0418, 0.0286, 0.6806, 0.3375, 0.0074, 0.0712, 0.2290, 0.0224, 0.0189, 0.0080, 0.0187, 0.0097, 0.0172, 0.0124, 0.0418, 0.7799, 0.0521, 0.0395, 0.0097, 0.0030, 0.0023, 1.706e-5, 0.0087, 0.0027, 0.6569, 0.0850, 0.0080, 0.5562, 0.0173, 0.0015, 1.706e-5, 0.0369, 0.0077, 0.0286, 0.0187, 0.7799, 0.0711, 0.0200, 0.0084, 0.0012, 0.0403, 0.0556, 0.1762, 0.0389, 0.0224, 0.0030, 0.5562, 0.0084, 0.0060, 0.0028, 0.0014, 0.2274, 0.0200, 0.0555, 0.0189, 0.0521, 0.0015, 0.0711, 0.0028, 0.3911, 0.1349, 0.0098, 0.0118, 0.7198, 0.2290, 0.0395, 0.0173, 0.0200, 0.0060, 0.3911}; INDArray indVals = Nd4j.createFromArray(vals); final int N = 11; INDArray posF = Nd4j.create(DataType.DOUBLE, ndinput.shape()); SpTree tree = new SpTree(ndinput); tree.computeEdgeForces(indRows, indCols, indVals, N, posF); double[]expectedPosF = {-0.0818453583761987, -0.10231102631753211, 0.016809473355579547, 0.16176252194290375, -0.20703464777007444, -0.1263832139293613, 0.10996898963389254, 0.13983782727968627, -0.09164547825742625, 0.09219041827159041, 0.14252277104691244, 0.014676985587529433, 0.19786703075718223, -0.25244374832212546, -0.018387062879777892, 0.13652061663449183, 0.07639155593531936, -0.07616591260449279, 0.12919565310762643, -0.19229222179037395, -0.11250575155166542, -0.09598877143033444, 0.014899570740339653, 0.018867923701997365, 0.19996253097190828, 0.30233811684856743, -0.18830455752593392, 0.10223346521208224, -0.09703007177169608, -0.003280966942428477, 0.15213078827243462, -0.02397414389327494, -0.1390550777479942, 0.30088735606726813, 0.17456236098186903, -0.31560012032960044, 0.142309945794784, -0.08988089476622348, 0.011236280978163357, -0.10732740266565795, -0.24928551644245478, 0.10762735102220329, 0.03434270193250408, 2.831838829882295E-4, -0.17494982967210068, 0.07114328804840916, 0.15171552834583996, -0.08888924450773618, -0.20576831397087963, 0.027662749212463134, 0.08096437977846523, -0.19211185715249313, -0.11199893965092741, 0.024654692641180212, 0.20889407228258244}; assertArrayEquals(expectedPosF, posF.reshape(1,55).toDoubleVector(), 1e-5); final double theta = 0.5; AtomicDouble sumQ = new AtomicDouble(0.0); INDArray negF = Nd4j.create(DataType.DOUBLE, ndinput.shape()); for (int n = 0; n < N; n++) { INDArray prev = ((n == 0) ? negF.slice(n ): negF.slice(n-1)); tree.computeNonEdgeForces(n, theta, negF.slice(0), sumQ); } double[] expectedNegF = {-0.15349944039348173, -0.9608688924710804, -1.7099994806905086, 2.6604989787415203, 1.2677709150619332, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; double expectedSum = 88.60715062760883; assertArrayEquals(expectedNegF, negF.reshape(1,55).toDoubleVector(), 1e-5); assertEquals(expectedSum, sumQ.get(), 1e-5); }
Example 17
Source File: SlicingTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSlice() { INDArray arr = Nd4j.linspace(1, 24, 24).reshape(4, 3, 2); INDArray assertion = Nd4j.create(new double[][] {{1, 13}, {5, 17}, {9, 21}}); INDArray firstSlice = arr.slice(0); INDArray slice1Assertion = Nd4j.create(new double[][] {{2, 14}, {6, 18}, {10, 22}, }); INDArray secondSlice = arr.slice(1); assertEquals(assertion, firstSlice); assertEquals(slice1Assertion, secondSlice); }
Example 18
Source File: LeadingAndTrailingOnesC.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testMatrix() { INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2); INDArray slice1 = arr.slice(1); // System.out.println(arr.slice(1)); INDArray oneInMiddle = Nd4j.linspace(1, 4, 4).reshape(2, 1, 2); INDArray otherSlice = oneInMiddle.slice(1); assertEquals(2, otherSlice.offset()); // System.out.println(otherSlice); INDArray twoOnesInMiddle = Nd4j.linspace(1, 4, 4).reshape(2, 1, 1, 2); INDArray sub = twoOnesInMiddle.get(NDArrayIndex.point(1), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all()); assertEquals(2, sub.offset()); }
Example 19
Source File: LeadingAndTrailingOnesC.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testMultipleOnesInMiddle() { INDArray tensor = Nd4j.linspace(1, 144, 144).reshape(2, 2, 1, 1, 6, 6); INDArray tensorSlice1 = tensor.slice(1); INDArray tensorSlice1Slice1 = tensorSlice1.slice(1); // System.out.println(tensor); }
Example 20
Source File: CudaTransformsTests.java From nd4j with Apache License 2.0 | 4 votes |
@Test public void testClassificationSoftmax() throws Exception { INDArray input = Nd4j.zeros(256, 3000); System.out.println("A0: --------------------------------"); for (int i = 0; i < 256; i++) { input.putScalar(3000 * i, (i * 2) + 0.5); } System.out.println("AF: --------------------------------"); // AtomicAllocator.getInstance().getPointer(input); // AtomicAllocator.getInstance().getPointer(input.shapeInfoDataBuffer()); System.out.println("AX: --------------------------------"); // AtomicAllocator.getInstance().getPointer(input); // AtomicAllocator.getInstance().getPointer(input.shapeInfoDataBuffer()); System.out.println("AA: --------------------------------"); float sumAll = input.sumNumber().floatValue(); System.out.println("A1: --------------------------------"); System.out.println("Data:" + input.data().length() + " Sum: " + sumAll); assertEquals(65408.0f, sumAll, 0.01f); System.out.println("A2: --------------------------------"); OldSoftMax softMax = new OldSoftMax(input); long time1 = System.currentTimeMillis(); Nd4j.getExecutioner().exec(softMax); long time2 = System.currentTimeMillis(); System.out.println("A3: --------------------------------"); System.out.println("Execution time: " + (time2 - time1)); /* assertEquals(0.036710344f,input.getFloat(0), 0.01f); assertEquals(0.023549506f,input.getFloat(152), 0.01f); assertEquals(0.005180763f,input.getFloat(310), 0.01f); assertEquals(4.5634616E-7f,input.getFloat(879), 0.01f); */ for (int i = 0; i < 256; i++) { INDArray slice = input.slice(i); System.out.println("Position [0]: " + input.getDouble(3000 * i) + ", [1]: " + input.getDouble(3000 * i + 1)); assertTrue(input.getDouble(3000 * i) > input.getDouble(3000 * i + 1)); float sum = slice.sumNumber().floatValue(); assertEquals("Failed on iteration ["+i+"]", 1.0f, sum, 0.01f); } }