org.nd4j.linalg.checkutil.NDArrayCreationUtil Java Examples
The following examples show how to use
org.nd4j.linalg.checkutil.NDArrayCreationUtil.
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: TestNdArrReadWriteTxt.java From nd4j with Apache License 2.0 | 6 votes |
public static void compareArrays(int rank, char ordering) { List<Pair<INDArray, String>> all = NDArrayCreationUtil.getTestMatricesWithVaryingShapes(rank,ordering); Iterator<Pair<INDArray,String>> iter = all.iterator(); while (iter.hasNext()) { Pair<INDArray,String> currentPair = iter.next(); INDArray origArray = currentPair.getFirst(); //adding elements outside the bounds where print switches to scientific notation origArray.tensorAlongDimension(0,0).muli(0).addi(100000); origArray.putScalar(0,10001.1234); log.info("\nChecking shape ..." + currentPair.getSecond()); log.info("\n"+ origArray.dup('c').toString()); Nd4j.writeTxt(origArray, "someArr.txt"); INDArray readBack = Nd4j.readTxt("someArr.txt"); assertEquals("\nNot equal on shape " + ArrayUtils.toString(origArray.shape()), origArray, readBack); try { Files.delete(Paths.get("someArr.txt")); } catch (IOException e) { e.printStackTrace(); } } }
Example #2
Source File: TestInvertMatrices.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testInverseComparison() { List<Pair<INDArray, String>> list = NDArrayCreationUtil.getAllTestMatricesWithShape(10, 10, 12345, DataType.DOUBLE); for (Pair<INDArray, String> p : list) { INDArray orig = p.getFirst(); orig.assign(Nd4j.rand(orig.shape())); INDArray inverse = InvertMatrix.invert(orig, false); RealMatrix rm = CheckUtil.convertToApacheMatrix(orig); RealMatrix rmInverse = new LUDecomposition(rm).getSolver().getInverse(); INDArray expected = CheckUtil.convertFromApacheMatrix(rmInverse, orig.dataType()); assertTrue(p.getSecond(), CheckUtil.checkEntries(expected, inverse, 1e-3, 1e-4)); } }
Example #3
Source File: TestNdArrReadWriteTxt.java From deeplearning4j with Apache License 2.0 | 6 votes |
public static void compareArrays(int rank, char ordering, TemporaryFolder testDir) throws Exception { List<Pair<INDArray, String>> all = NDArrayCreationUtil.getTestMatricesWithVaryingShapes(rank,ordering, Nd4j.defaultFloatingPointType()); Iterator<Pair<INDArray,String>> iter = all.iterator(); int cnt = 0; while (iter.hasNext()) { File dir = testDir.newFolder(); Pair<INDArray,String> currentPair = iter.next(); INDArray origArray = currentPair.getFirst(); //adding elements outside the bounds where print switches to scientific notation origArray.tensorAlongDimension(0,0).muli(0).addi(100000); origArray.putScalar(0,10001.1234); // log.info("\nChecking shape ..." + currentPair.getSecond()); //log.info("C:\n"+ origArray.dup('c').toString()); // log.info("F:\n"+ origArray.toString()); Nd4j.writeTxt(origArray, new File(dir, "someArr.txt").getAbsolutePath()); INDArray readBack = Nd4j.readTxt(new File(dir, "someArr.txt").getAbsolutePath()); assertEquals("\nNot equal on shape " + ArrayUtils.toString(origArray.shape()), origArray, readBack); cnt++; } }
Example #4
Source File: NDArrayTestsFortran.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testBroadcastingGenerated() { int[][] broadcastShape = NDArrayCreationUtil.getRandomBroadCastShape(7, 6, 10); List<List<Pair<INDArray, String>>> broadCastList = new ArrayList<>(broadcastShape.length); for (int[] shape : broadcastShape) { List<Pair<INDArray, String>> arrShape = NDArrayCreationUtil.get6dPermutedWithShape(7, shape); broadCastList.add(arrShape); broadCastList.add(NDArrayCreationUtil.get6dReshapedWithShape(7, shape)); broadCastList.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(7, shape)); } for (List<Pair<INDArray, String>> b : broadCastList) { for (Pair<INDArray, String> val : b) { INDArray inputArrBroadcast = val.getFirst(); val destShape = NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7); INDArray output = inputArrBroadcast .broadcast(NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7)); assertArrayEquals(destShape, output.shape()); } } }
Example #5
Source File: NDArrayTestsFortran.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testDupAndDupWithOrder() { List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(4, 5, 123); int count = 0; for (Pair<INDArray, String> pair : testInputs) { String msg = pair.getSecond(); INDArray in = pair.getFirst(); System.out.println("Count " + count); INDArray dup = in.dup(); INDArray dupc = in.dup('c'); INDArray dupf = in.dup('f'); assertEquals(msg, in, dup); assertEquals(msg, dup.ordering(), (char) Nd4j.order()); assertEquals(msg, dupc.ordering(), 'c'); assertEquals(msg, dupf.ordering(), 'f'); assertEquals(msg, in, dupc); assertEquals(msg, in, dupf); count++; } }
Example #6
Source File: Nd4jTestsComparisonFortran.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testAddSubtractWithOpsCommonsMath() { List<Pair<INDArray, String>> first = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED); List<Pair<INDArray, String>> second = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED); for (int i = 0; i < first.size(); i++) { for (int j = 0; j < second.size(); j++) { Pair<INDArray, String> p1 = first.get(i); Pair<INDArray, String> p2 = second.get(j); String errorMsg1 = getTestWithOpsErrorMsg(i, j, "add", p1, p2); String errorMsg2 = getTestWithOpsErrorMsg(i, j, "sub", p1, p2); boolean addFail = CheckUtil.checkAdd(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6); assertTrue(errorMsg1, addFail); boolean subFail = CheckUtil.checkSubtract(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6); assertTrue(errorMsg2, subFail); } } }
Example #7
Source File: Nd4jTest.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testSqueeze(){ final List<Pair<INDArray, String>> testMatricesC = NDArrayCreationUtil.getAllTestMatricesWithShape('c', 3, 1, 0xDEAD, DataType.DOUBLE); final List<Pair<INDArray, String>> testMatricesF = NDArrayCreationUtil.getAllTestMatricesWithShape('f', 7, 1, 0xBEEF, DataType.DOUBLE); final ArrayList<Pair<INDArray, String>> testMatrices = new ArrayList<>(testMatricesC); testMatrices.addAll(testMatricesF); for (Pair<INDArray, String> testMatrixPair : testMatrices) { final String recreation = testMatrixPair.getSecond(); final INDArray testMatrix = testMatrixPair.getFirst(); final char ordering = testMatrix.ordering(); val shape = testMatrix.shape(); final INDArray squeezed = Nd4j.squeeze(testMatrix, 1); final long[] expShape = ArrayUtil.removeIndex(shape, 1); final String message = "Squeezing in dimension 1; Shape before squeezing: " + Arrays.toString(shape) + " " + ordering + " Order; Shape after expanding: " + Arrays.toString(squeezed.shape()) + " "+squeezed.ordering()+"; Input Created via: " + recreation; assertArrayEquals(message, expShape, squeezed.shape()); assertEquals(message, ordering, squeezed.ordering()); assertEquals(message, testMatrix.ravel(), squeezed.ravel()); testMatrix.assign(Nd4j.rand(shape)); assertEquals(message, testMatrix.ravel(), squeezed.ravel()); } }
Example #8
Source File: ElementWiseStrideTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testEWS1() throws Exception { List<Pair<INDArray,String>> list = NDArrayCreationUtil.getAllTestMatricesWithShape(4,5,12345); list.addAll(NDArrayCreationUtil.getAll3dTestArraysWithShape(12345,4,5,6)); list.addAll(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345,4,5,6,7)); list.addAll(NDArrayCreationUtil.getAll5dTestArraysWithShape(12345,4,5,6,7,8)); list.addAll(NDArrayCreationUtil.getAll6dTestArraysWithShape(12345,4,5,6,7,8,9)); for(Pair<INDArray,String> p : list){ int ewsBefore = Shape.elementWiseStride(p.getFirst().shapeInfo()); INDArray reshapeAttempt = Shape.newShapeNoCopy(p.getFirst(),new int[]{1,p.getFirst().length()}, Nd4j.order() == 'f'); if (reshapeAttempt != null && ewsBefore == -1 && reshapeAttempt.elementWiseStride() != -1 ) { System.out.println("NDArrayCreationUtil." + p.getSecond()); System.out.println("ews before: " + ewsBefore); System.out.println(p.getFirst().shapeInfoToString()); System.out.println("ews returned by elementWiseStride(): " + p.getFirst().elementWiseStride()); System.out.println("ews returned by reshape(): " + reshapeAttempt.elementWiseStride()); System.out.println(); // assertTrue(false); } else { // System.out.println("FAILED: " + p.getFirst().shapeInfoToString()); } } }
Example #9
Source File: Nd4jTestsComparisonFortran.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testAddSubtractWithOpsCommonsMath() { List<Pair<INDArray, String>> first = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED, DataType.DOUBLE); List<Pair<INDArray, String>> second = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED, DataType.DOUBLE); for (int i = 0; i < first.size(); i++) { for (int j = 0; j < second.size(); j++) { Pair<INDArray, String> p1 = first.get(i); Pair<INDArray, String> p2 = second.get(j); String errorMsg1 = getTestWithOpsErrorMsg(i, j, "add", p1, p2); String errorMsg2 = getTestWithOpsErrorMsg(i, j, "sub", p1, p2); boolean addFail = CheckUtil.checkAdd(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6); assertTrue(errorMsg1, addFail); boolean subFail = CheckUtil.checkSubtract(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6); assertTrue(errorMsg2, subFail); } } }
Example #10
Source File: TestInvertMatrices.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testInverseComparison() { List<Pair<INDArray, String>> list = NDArrayCreationUtil.getAllTestMatricesWithShape(10, 10, 12345); for (Pair<INDArray, String> p : list) { INDArray orig = p.getFirst(); orig.assign(Nd4j.rand(orig.shape())); INDArray inverse = InvertMatrix.invert(orig, false); RealMatrix rm = CheckUtil.convertToApacheMatrix(orig); RealMatrix rmInverse = new LUDecomposition(rm).getSolver().getInverse(); INDArray expected = CheckUtil.convertFromApacheMatrix(rmInverse); assertTrue(p.getSecond(), CheckUtil.checkEntries(expected, inverse, 1e-3, 1e-4)); } }
Example #11
Source File: ReductionOpValidation.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testStdev() { List<String> errors = new ArrayList<>(); for (Pair<INDArray, String> p : NDArrayCreationUtil.getAllTestMatricesWithShape(3, 4, 12345, DataType.DOUBLE)) { for (boolean biasCorrected : new boolean[]{false, true}) { SameDiff sd = SameDiff.create(); SDVariable var = sd.var("in", p.getFirst()); SDVariable stdev = var.std(biasCorrected); INDArray expOut = p.getFirst().std(biasCorrected); TestCase tc = new TestCase(sd) .testName(p.getSecond() + " - biasCorrected=" + biasCorrected) .expected(stdev, expOut) .gradientCheck(false); String err = OpValidation.validate(tc); if (err != null) { errors.add(err); } } } assertEquals(errors.toString(), 0, errors.size()); }
Example #12
Source File: NDArrayTestsFortran.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testBroadcastingGenerated() { int[][] broadcastShape = NDArrayCreationUtil.getRandomBroadCastShape(7, 6, 10); List<List<Pair<INDArray, String>>> broadCastList = new ArrayList<>(broadcastShape.length); for (int[] shape : broadcastShape) { List<Pair<INDArray, String>> arrShape = NDArrayCreationUtil.get6dPermutedWithShape(7, shape, DataType.DOUBLE); broadCastList.add(arrShape); broadCastList.add(NDArrayCreationUtil.get6dReshapedWithShape(7, shape, DataType.DOUBLE)); broadCastList.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(7, shape, DataType.DOUBLE)); } for (List<Pair<INDArray, String>> b : broadCastList) { for (Pair<INDArray, String> val : b) { INDArray inputArrBroadcast = val.getFirst(); val destShape = NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7); INDArray output = inputArrBroadcast .broadcast(NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7)); assertArrayEquals(destShape, output.shape()); } } }
Example #13
Source File: GradCheckMisc.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testPermuteGradient() { int[] origShape = new int[]{3, 4, 5}; for (int[] perm : new int[][]{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}) { for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, origShape)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable permute = sd.f().permute(in, perm); //Using stdev here: mean/sum would backprop the same gradient for each input... SDVariable stdev = sd.standardDeviation("out", permute, true); INDArray out = sd.execAndEndResult(); INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE); assertEquals(expOut, out); String msg = "permute=" + Arrays.toString(perm) + ", source=" + p.getSecond(); boolean ok = GradCheckUtil.checkGradients(sd); assertTrue(msg, ok); } } }
Example #14
Source File: GradCheckMisc.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testReshapeGradient() { int[] origShape = new int[]{3, 4, 5}; for (int[] toShape : new int[][]{{3, 4 * 5}, {3 * 4, 5}, {1, 3 * 4 * 5}, {3 * 4 * 5, 1}}) { for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, origShape)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable reshape = sd.reshape(in, toShape); //Using stdev here: mean/sum would backprop the same gradient for each input... SDVariable stdev = sd.standardDeviation("out", reshape, true); INDArray out = sd.execAndEndResult(); INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE); assertEquals(expOut, out); String msg = "toShape=" + Arrays.toString(toShape) + ", source=" + p.getSecond(); boolean ok = GradCheckUtil.checkGradients(sd); assertTrue(msg, ok); } } }
Example #15
Source File: SameDiffTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testSqueezeExpandChain() { val origShape = new long[]{3, 4, 5}; for (int i = 0; i < 3; i++) { val shape = origShape.clone(); shape[i] = 1; for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, shape)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable squeeze = sd.squeeze(in, i); SDVariable expand = sd.expandDims(squeeze, i); INDArray out = sd.execAndEndResult(); String msg = "expand/Squeeze=" + i + ", source=" + p.getSecond(); assertEquals(msg, out, inArr); //squeeze -> expand: should be opposite ops } } }
Example #16
Source File: SameDiffTests.java From nd4j with Apache License 2.0 | 6 votes |
@Test public void testExpandSqueezeChain() { val origShape = new long[]{3, 4}; for (int i = 0; i < 3; i++) { for (Pair<INDArray, String> p : NDArrayCreationUtil.getAllTestMatricesWithShape(origShape[0], origShape[1], 12345)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable expand = sd.expandDims(in, i); SDVariable squeeze = sd.squeeze(expand, i); INDArray out = sd.execAndEndResult(); String msg = "expand/Squeeze=" + i + ", source=" + p.getSecond(); assertEquals(msg, out, inArr); //expand -> squeeze: should be opposite ops } } }
Example #17
Source File: NDArrayTestsFortran.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testDupAndDupWithOrder() { List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(4, 5, 123, DataType.DOUBLE); int count = 0; for (Pair<INDArray, String> pair : testInputs) { String msg = pair.getSecond(); INDArray in = pair.getFirst(); // System.out.println("Count " + count); INDArray dup = in.dup(); INDArray dupc = in.dup('c'); INDArray dupf = in.dup('f'); assertEquals(msg, in, dup); assertEquals(msg, dup.ordering(), (char) Nd4j.order()); assertEquals(msg, dupc.ordering(), 'c'); assertEquals(msg, dupf.ordering(), 'f'); assertEquals(msg, in, dupc); assertEquals(msg, in, dupf); count++; } }
Example #18
Source File: SameDiffTests.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Test public void testExpandSqueezeChain() { val origShape = new long[]{3, 4}; for (int i = 0; i < 3; i++) { for (Pair<INDArray, String> p : NDArrayCreationUtil .getAllTestMatricesWithShape(origShape[0], origShape[1], 12345, DataType.FLOAT)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable expand = sd.expandDims(in, i); SDVariable squeeze = sd.squeeze(expand, i); INDArray out = squeeze.eval(); String msg = "expand/Squeeze=" + i + ", source=" + p.getSecond(); assertEquals(msg, out, inArr); //expand -> squeeze: should be opposite ops } } }
Example #19
Source File: LoneTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@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 #20
Source File: ShapeOpValidation.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testPermuteGradient() { int[] origShape = new int[]{3, 4, 5}; List<String> failed = new ArrayList<>(); for (int[] perm : new int[][]{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}) { for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, origShape, DataType.DOUBLE)) { String msg = "permute=" + Arrays.toString(perm) + ", source=" + p.getSecond(); System.out.println(msg); INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable permute = sd.permute(in, perm); //Using stdev here: mean/sum would backprop the same gradient for each input... SDVariable stdev = sd.standardDeviation("out", permute, true); INDArray exp = inArr.permute(perm); INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE); TestCase tc = new TestCase(sd); tc.testName(msg) .expected("out", expOut) .expected(permute, exp); String error = OpValidation.validate(tc, true); if(error != null){ failed.add(msg); } } } assertEquals(failed.toString(), 0, failed.size()); }
Example #21
Source File: TestNdArrReadWriteTxt.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testNd4jReadWriteText() throws Exception { File dir = testDir.newFolder(); int count = 0; for(val testShape : new long[][]{{1,1}, {3,1}, {4,5}, {1,2,3}, {2,1,3}, {2,3,1}, {2,3,4}, {1,2,3,4}, {2,3,4,2}}){ List<Pair<INDArray, String>> l = null; switch (testShape.length){ case 2: l = NDArrayCreationUtil.getAllTestMatricesWithShape(testShape[0], testShape[1], 12345, Nd4j.defaultFloatingPointType()); break; case 3: l = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, testShape, Nd4j.defaultFloatingPointType()); break; case 4: l = NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, testShape, Nd4j.defaultFloatingPointType()); break; default: throw new RuntimeException(); } for (Pair<INDArray, String> p : l) { File f = new File(dir, (count++) + ".txt"); Nd4j.writeTxt(p.getFirst(), f.getAbsolutePath()); INDArray read = Nd4j.readTxt(f.getAbsolutePath()); String s = FileUtils.readFileToString(f, StandardCharsets.UTF_8); // System.out.println(s); assertEquals(p.getFirst(), read); } } }
Example #22
Source File: Nd4jTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testExpandDims(){ final List<Pair<INDArray, String>> testMatricesC = NDArrayCreationUtil.getAllTestMatricesWithShape('c', 3, 5, 0xDEAD, DataType.DOUBLE); final List<Pair<INDArray, String>> testMatricesF = NDArrayCreationUtil.getAllTestMatricesWithShape('f', 7, 11, 0xBEEF, DataType.DOUBLE); final ArrayList<Pair<INDArray, String>> testMatrices = new ArrayList<>(testMatricesC); testMatrices.addAll(testMatricesF); for (Pair<INDArray, String> testMatrixPair : testMatrices) { final String recreation = testMatrixPair.getSecond(); final INDArray testMatrix = testMatrixPair.getFirst(); final char ordering = testMatrix.ordering(); val shape = testMatrix.shape(); final int rank = testMatrix.rank(); for (int i = -rank; i <= rank; i++) { final INDArray expanded = Nd4j.expandDims(testMatrix, i); final String message = "Expanding in Dimension " + i + "; Shape before expanding: " + Arrays.toString(shape) + " "+ordering+" Order; Shape after expanding: " + Arrays.toString(expanded.shape()) + " "+expanded.ordering()+"; Input Created via: " + recreation; val tmR = testMatrix.ravel(); val expR = expanded.ravel(); assertEquals(message, 1, expanded.shape()[i < 0 ? i + rank : i]); assertEquals(message, tmR, expR); assertEquals(message, ordering, expanded.ordering()); testMatrix.assign(Nd4j.rand(DataType.DOUBLE, shape)); assertEquals(message, testMatrix.ravel(), expanded.ravel()); } } }
Example #23
Source File: SameDiffTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testExpandDims2d() { val origShape = new long[]{3, 4}; for (int i = 0; i < 3; i++) { for (Pair<INDArray, String> p : NDArrayCreationUtil .getAllTestMatricesWithShape(origShape[0], origShape[1], 12345, DataType.FLOAT)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable expand = sd.expandDims(in, i); INDArray out = expand.eval(); INDArray expOut; switch (i) { case 0: expOut = inArr.dup('c').reshape('c', 1, origShape[0], origShape[1]); break; case 1: expOut = inArr.dup('c').reshape('c', origShape[0], 1, origShape[1]); break; case 2: expOut = inArr.dup('c').reshape('c', origShape[0], origShape[1], 1); break; default: throw new RuntimeException(); } String msg = "expandDim=" + i + ", source=" + p.getSecond(); assertEquals(msg, out, expOut); } } }
Example #24
Source File: Nd4jTestsComparisonFortran.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testMmulWithOpsCommonsMath() { List<Pair<INDArray, String>> first = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED, DataType.DOUBLE); List<Pair<INDArray, String>> second = NDArrayCreationUtil.getAllTestMatricesWithShape(5, 4, SEED, DataType.DOUBLE); for (int i = 0; i < first.size(); i++) { for (int j = 0; j < second.size(); j++) { Pair<INDArray, String> p1 = first.get(i); Pair<INDArray, String> p2 = second.get(j); String errorMsg = getTestWithOpsErrorMsg(i, j, "mmul", p1, p2); assertTrue(errorMsg, CheckUtil.checkMmul(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6)); } } }
Example #25
Source File: Nd4jTestsComparisonFortran.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testMulDivOnCheckUtilMatrices() { List<Pair<INDArray, String>> first = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED, DataType.DOUBLE); List<Pair<INDArray, String>> second = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED, DataType.DOUBLE); for (int i = 0; i < first.size(); i++) { for (int j = 0; j < second.size(); j++) { Pair<INDArray, String> p1 = first.get(i); Pair<INDArray, String> p2 = second.get(j); String errorMsg1 = getTestWithOpsErrorMsg(i, j, "mul", p1, p2); String errorMsg2 = getTestWithOpsErrorMsg(i, j, "div", p1, p2); assertTrue(errorMsg1, CheckUtil.checkMulManually(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6)); assertTrue(errorMsg2, CheckUtil.checkDivManually(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6)); } } }
Example #26
Source File: NDArrayTestsFortran.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testToOffsetZeroCopy() { List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(4, 5, 123, DataType.DOUBLE); int cnt = 0; for (Pair<INDArray, String> pair : testInputs) { String msg = pair.getSecond(); INDArray in = pair.getFirst(); INDArray dup = Shape.toOffsetZeroCopy(in); INDArray dupc = Shape.toOffsetZeroCopy(in, 'c'); INDArray dupf = Shape.toOffsetZeroCopy(in, 'f'); INDArray dupany = Shape.toOffsetZeroCopyAnyOrder(in); assertEquals(msg + ": " + cnt, in, dup); assertEquals(msg, in, dupc); assertEquals(msg, in, dupf); assertEquals(msg, dupc.ordering(), 'c'); assertEquals(msg, dupf.ordering(), 'f'); assertEquals(msg, in, dupany); assertEquals(dup.offset(), 0); assertEquals(dupc.offset(), 0); assertEquals(dupf.offset(), 0); assertEquals(dupany.offset(), 0); assertEquals(dup.length(), dup.data().length()); assertEquals(dupc.length(), dupc.data().length()); assertEquals(dupf.length(), dupf.data().length()); assertEquals(dupany.length(), dupany.data().length()); cnt++; } }
Example #27
Source File: Nd4jTestsComparisonFortran.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testMmulWithOpsCommonsMath() { List<Pair<INDArray, String>> first = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 5, SEED); List<Pair<INDArray, String>> second = NDArrayCreationUtil.getAllTestMatricesWithShape(5, 4, SEED); for (int i = 0; i < first.size(); i++) { for (int j = 0; j < second.size(); j++) { Pair<INDArray, String> p1 = first.get(i); Pair<INDArray, String> p2 = second.get(j); String errorMsg = getTestWithOpsErrorMsg(i, j, "mmul", p1, p2); assertTrue(errorMsg, CheckUtil.checkMmul(p1.getFirst(), p2.getFirst(), 1e-4, 1e-6)); } } }
Example #28
Source File: SameDiffTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testExpandDims2d() { val origShape = new long[]{3, 4}; for (int i = 0; i < 3; i++) { for (Pair<INDArray, String> p : NDArrayCreationUtil.getAllTestMatricesWithShape(origShape[0], origShape[1], 12345)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable expand = sd.f().expandDims(in, i); INDArray out = sd.execAndEndResult(); INDArray expOut; switch (i) { case 0: expOut = inArr.dup('c').reshape('c', 1, origShape[0], origShape[1]); break; case 1: expOut = inArr.dup('c').reshape('c', origShape[0], 1, origShape[1]); break; case 2: expOut = inArr.dup('c').reshape('c', origShape[0], origShape[1], 1); break; default: throw new RuntimeException(); } String msg = "expandDim=" + i + ", source=" + p.getSecond(); assertEquals(msg, out, expOut); } } }
Example #29
Source File: SameDiffTests.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSqueezeDims() { val origShape = new long[]{3, 4, 5}; for (int i = 0; i < 3; i++) { val shape = origShape.clone(); shape[i] = 1; for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, shape)) { INDArray inArr = p.getFirst().muli(100); SameDiff sd = SameDiff.create(); SDVariable in = sd.var("in", inArr); SDVariable squeeze = sd.f().squeeze(in, i); INDArray out = sd.execAndEndResult(); INDArray expOut; switch (i) { case 0: expOut = inArr.dup('c').reshape('c', origShape[1], origShape[2]); break; case 1: expOut = inArr.dup('c').reshape('c', origShape[0], origShape[2]); break; case 2: expOut = inArr.dup('c').reshape('c', origShape[0], origShape[1]); break; default: throw new RuntimeException(); } String msg = "squeezeDim=" + i + ", source=" + p.getSecond(); assertEquals(msg, out, expOut); } } }
Example #30
Source File: Nd4jTest.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testExpandDims(){ final List<Pair<INDArray, String>> testMatricesC = NDArrayCreationUtil.getAllTestMatricesWithShape('c', 3, 5, 0xDEAD); final List<Pair<INDArray, String>> testMatricesF = NDArrayCreationUtil.getAllTestMatricesWithShape('f', 7, 11, 0xBEEF); final ArrayList<Pair<INDArray, String>> testMatrices = new ArrayList<>(testMatricesC); testMatrices.addAll(testMatricesF); for (Pair<INDArray, String> testMatrixPair : testMatrices) { final String recreation = testMatrixPair.getSecond(); final INDArray testMatrix = testMatrixPair.getFirst(); final char ordering = testMatrix.ordering(); val shape = testMatrix.shape(); final int rank = testMatrix.rank(); for (int i = -rank; i <= rank; i++) { final INDArray expanded = Nd4j.expandDims(testMatrix, i); final String message = "Expanding in Dimension " + i + "; Shape before expanding: " + Arrays.toString(shape) + " "+ordering+" Order; Shape after expanding: " + Arrays.toString(expanded.shape()) + " "+expanded.ordering()+"; Input Created via: " + recreation; assertEquals(message, 1, expanded.shape()[i < 0 ? i + rank : i]); assertEquals(message, testMatrix.ravel(), expanded.ravel()); assertEquals(message, ordering, expanded.ordering()); testMatrix.assign(Nd4j.rand(shape)); assertEquals(message, testMatrix.ravel(), expanded.ravel()); } } }