Java Code Examples for org.nd4j.linalg.api.buffer.util.DataTypeUtil#setDTypeForContext()
The following examples show how to use
org.nd4j.linalg.api.buffer.util.DataTypeUtil#setDTypeForContext() .
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: SporadicTests.java From nd4j with Apache License 2.0 | 6 votes |
/** * This is special test that checks for memory alignment * @throws Exception */ @Test @Ignore public void testDTypeSpam() throws Exception { Random rnd = new Random(); for(int i = 0; i < 100; i++) { DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT); float rand[] = new float[rnd.nextInt(10) + 1]; for (int x = 0; x < rand.length; x++) { rand[x] = rnd.nextFloat(); } Nd4j.getConstantHandler().getConstantBuffer(rand); int shape[] = new int[rnd.nextInt(3)+2]; for (int x = 0; x < shape.length; x++) { shape[x] = rnd.nextInt(100) + 2; } DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); INDArray array = Nd4j.rand(shape); BooleanIndexing.applyWhere(array, Conditions.lessThan(rnd.nextDouble()), rnd.nextDouble()); } }
Example 2
Source File: TestSerializationFloatToDouble.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSerializationFullArrayNd4jWriteRead() throws Exception { int length = 100; //WRITE OUT A FLOAT ARRAY //Hack before setting datatype - fix already in r119_various branch Nd4j.create(1); DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT); INDArray arr = Nd4j.linspace(1, length, length).reshape('c', 10, 10); arr.subi(50.0123456); //assures positive and negative numbers with decimal points ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputStream dos = new DataOutputStream(baos)) { Nd4j.write(arr, dos); } byte[] bytes = baos.toByteArray(); //SET DATA TYPE TO DOUBLE and initialize another array with the same contents //Nd4j.create(1); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); System.out.println("The data opType is " + Nd4j.dataType()); INDArray arr1 = Nd4j.linspace(1, length, length).reshape('c', 10, 10); arr1.subi(50.0123456); INDArray arr2; try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) { arr2 = Nd4j.read(dis); } //System.out.println(new NDArrayStrings(6).format(arr2.sub(arr1).mul(100).div(arr1))); assertTrue(Transforms.abs(arr1.sub(arr2).div(arr1)).maxNumber().doubleValue() < 0.01); }
Example 3
Source File: NativeOpExecutionerTest.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testDebugEdgeCase2(){ DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); INDArray l1 = Nd4j.create(new double[]{-0.2585039112684677,-0.005179485353710878,0.4348343401770497,0.020356532375728764,-0.1970793298488186}); INDArray l2 = Nd4j.create(2,l1.size(1)); INDArray p1 = Nd4j.create(new double[]{1.3979850406519119,0.6169451410155852,1.128993957530918,0.21000426084450596,0.3171215178932696}); INDArray p2 = Nd4j.create(2, p1.size(1)); for( int i=0; i<2; i++ ){ l2.putRow(i, l1); p2.putRow(i, p1); } INDArray norm2_1 = l1.norm2(1); INDArray temp1 = p1.mul(l1); INDArray out1 = temp1.diviColumnVector(norm2_1); INDArray norm2_2 = l2.norm2(1); INDArray temp2 = p2.mul(l2); INDArray out2 = temp2.diviColumnVector(norm2_2); System.out.println("norm2_1: " + Arrays.toString(norm2_1.data().asDouble())); System.out.println("norm2_2: " + Arrays.toString(norm2_2.data().asDouble())); System.out.println("temp1: " + Arrays.toString(temp1.data().asDouble())); System.out.println("temp2: " + Arrays.toString(temp2.data().asDouble())); //Outputs here should be identical: System.out.println(Arrays.toString(out1.data().asDouble())); System.out.println(Arrays.toString(out2.getRow(0).dup().data().asDouble())); }
Example 4
Source File: DoublesTests.java From nd4j with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { System.out.println("----------------------"); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); CudaEnvironment.getInstance().getConfiguration().enableDebug(true).setVerbose(true).allowMultiGPU(false); }
Example 5
Source File: TestSerializationFloatToDouble.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSerializationOnViewsJava() throws Exception { int length = 100; Nd4j.create(1); DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT); INDArray arr = Nd4j.linspace(1, length, length).reshape('c', 10, 10); INDArray sub = arr.get(NDArrayIndex.interval(5, 10), NDArrayIndex.interval(5, 10)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(sub); } byte[] bytes = baos.toByteArray(); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); System.out.println("The data opType is " + Nd4j.dataType()); INDArray arr1 = Nd4j.linspace(1, length, length).reshape('c', 10, 10); INDArray sub1 = arr1.get(NDArrayIndex.interval(5, 10), NDArrayIndex.interval(5, 10)); INDArray arr2; try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { arr2 = (INDArray) ois.readObject(); } //assertEquals(sub,arr2); assertTrue(Transforms.abs(sub1.sub(arr2).div(sub1)).maxNumber().doubleValue() < 0.01); }
Example 6
Source File: LossFunctionGradientChecks.java From nd4j with Apache License 2.0 | 5 votes |
@Before public void before() throws Exception { super.before(); Nd4j.zeros(1); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); Nd4j.getRandom().setSeed(123); }
Example 7
Source File: TestSerializationDoubleToFloat.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testSerializationFullArrayJava() throws Exception { int length = 100; Nd4j.create(1); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); INDArray arr = Nd4j.linspace(1, length, length).reshape('c', 10, 10); arr.subi(50.0123456); //assures positive and negative numbers with decimal points ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(arr); } byte[] bytes = baos.toByteArray(); //SET DATA TYPE TO FLOAT and initialize another array with the same contents //Nd4j.create(1); DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT); System.out.println("The data opType is " + Nd4j.dataType()); INDArray arr1 = Nd4j.linspace(1, length, length).reshape('c', 10, 10); arr1.subi(50.0123456); INDArray arr2; try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { arr2 = (INDArray) ois.readObject(); } assertTrue(Transforms.abs(arr1.sub(arr2).div(arr1)).maxNumber().doubleValue() < 0.01); }
Example 8
Source File: ShapeTestsC.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testColumnSumDouble() { DataType initialType = Nd4j.dataType(); DataTypeUtil.setDTypeForContext(DataType.DOUBLE); INDArray twoByThree = Nd4j.linspace(1, 600, 600, DataType.DOUBLE).reshape(150, 4); INDArray columnVar = twoByThree.sum(0); INDArray assertion = Nd4j.create(new double[] {44850.0f, 45000.0f, 45150.0f, 45300.0f}); assertEquals(getFailureMessage(), assertion, columnVar); DataTypeUtil.setDTypeForContext(initialType); }
Example 9
Source File: Nd4jTestsComparisonC.java From nd4j with Apache License 2.0 | 4 votes |
@Before public void before() throws Exception { super.before(); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); }
Example 10
Source File: AveragingTests.java From nd4j with Apache License 2.0 | 4 votes |
@Before public void setUp() { DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); }
Example 11
Source File: ConvolutionTests.java From nd4j with Apache License 2.0 | 4 votes |
@Test @Ignore public void testCompareIm2ColImpl() { int[] miniBatches = {1, 3, 5}; int[] depths = {1, 3, 5}; int[] inHeights = {5, 21}; int[] inWidths = {5, 21}; int[] strideH = {1, 2}; int[] strideW = {1, 2}; int[] sizeW = {1, 2, 3}; int[] sizeH = {1, 2, 3}; int[] padH = {0, 1, 2}; int[] padW = {0, 1, 2}; boolean[] coverall = {false, true}; DataBuffer.Type[] types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE, DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE}; DataBuffer.AllocationMode[] modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT}; String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase(); if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) { //Only test direct for CUDA; test all for CPU types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE}; modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT}; } DataBuffer.Type initialType = Nd4j.dataType(); for (int i = 0; i < types.length; i++) { DataBuffer.Type type = types[i]; DataBuffer.AllocationMode mode = modes[i]; DataTypeUtil.setDTypeForContext(type); Nd4j.alloc = mode; AllocUtil.setAllocationModeForContext(mode); for (int m : miniBatches) { for (int d : depths) { for (int h : inHeights) { for (int w : inWidths) { for (int sh : strideH) { for (int sw : strideW) { for (int kh : sizeH) { for (int kw : sizeW) { for (int ph : padH) { for (int pw : padW) { if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0) continue; //(w-kp+2*pw)/sw + 1 is not an integer, i.e., number of outputs doesn't fit System.out.println("Running " + m + " " + d + " " + h + " " + w); for (boolean cAll : coverall) { INDArray in = Nd4j.rand(new int[] {m, d, h, w}); //assertEquals(in.data().allocationMode(), mode); //assertEquals(in.data().dataType(), opType); INDArray outOrig = OldConvolution.im2col(in, kh, kw, sh, sw, ph, pw, -1, cAll); //Old implementation INDArray outNew = Convolution.im2col(in, kh, kw, sh, sw, ph, pw, cAll); //Current implementation assertArrayEquals(outOrig.data().asFloat(), outNew.data().asFloat(), 0.01f); assertEquals(outOrig, outNew); } } } } } } } } } } } } DataTypeUtil.setDTypeForContext(initialType); }
Example 12
Source File: DoubleDataBufferTest.java From nd4j with Apache License 2.0 | 4 votes |
@After public void after() { DataTypeUtil.setDTypeForContext(initialType); }
Example 13
Source File: Nd4jTestsComparisonC.java From nd4j with Apache License 2.0 | 4 votes |
@After public void after() throws Exception { super.after(); DataTypeUtil.setDTypeForContext(initialType); }
Example 14
Source File: Nd4jTestsComparisonC.java From deeplearning4j with Apache License 2.0 | 4 votes |
@After public void after() throws Exception { DataTypeUtil.setDTypeForContext(initialType); }
Example 15
Source File: ConvolutionTestsC.java From nd4j with Apache License 2.0 | 4 votes |
@Test @Ignore public void testCompareIm2ColImpl() { int[] miniBatches = {1, 3, 5}; int[] depths = {1, 3, 5}; int[] inHeights = {5, 21}; int[] inWidths = {5, 21}; int[] strideH = {1, 2}; int[] strideW = {1, 2}; int[] sizeW = {1, 2, 3}; int[] sizeH = {1, 2, 3}; int[] padH = {0, 1, 2}; int[] padW = {0, 1, 2}; boolean[] coverall = {false, true}; DataBuffer.Type[] types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE, DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE}; DataBuffer.AllocationMode[] modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT}; String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase(); if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) { //Only test direct for CUDA; test all for CPU types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE}; modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT}; } DataBuffer.Type initialType = Nd4j.dataType(); for (int i = 0; i < types.length; i++) { DataBuffer.Type type = types[i]; DataBuffer.AllocationMode mode = modes[i]; DataTypeUtil.setDTypeForContext(type); Nd4j.alloc = mode; AllocUtil.setAllocationModeForContext(mode); for (int m : miniBatches) { for (int d : depths) { for (int h : inHeights) { for (int w : inWidths) { for (int sh : strideH) { for (int sw : strideW) { for (int kh : sizeH) { for (int kw : sizeW) { for (int ph : padH) { for (int pw : padW) { if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0) continue; //(w-kp+2*pw)/sw + 1 is not an integer, i.e., number of outputs doesn't fit System.out.println("Running " + m + " " + d + " " + h + " " + w); for (boolean cAll : coverall) { INDArray in = Nd4j.rand(new int[] {m, d, h, w}); //assertEquals(in.data().allocationMode(), mode); //assertEquals(in.data().dataType(), opType); INDArray outOrig = OldConvolution.im2col(in, kh, kw, sh, sw, ph, pw, -1, cAll); //Old implementation INDArray outNew = Convolution.im2col(in, kh, kw, sh, sw, ph, pw, cAll); //Current implementation assertEquals(outOrig, outNew); } } } } } } } } } } } } DataTypeUtil.setDTypeForContext(initialType); }
Example 16
Source File: FloatDataBufferTest.java From nd4j with Apache License 2.0 | 4 votes |
@After public void after() { DataTypeUtil.setDTypeForContext(initialType); }
Example 17
Source File: RandomTests.java From nd4j with Apache License 2.0 | 4 votes |
@Before public void setUp() throws Exception { initialType = Nd4j.dataType(); DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE); }
Example 18
Source File: Nd4jTestsComparisonC.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Before public void before() throws Exception { DataTypeUtil.setDTypeForContext(DataType.DOUBLE); }
Example 19
Source File: FloatDataBufferTest.java From nd4j with Apache License 2.0 | 4 votes |
@Before public void before() { DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT); System.out.println("DATATYPE HERE: " + Nd4j.dataType()); }
Example 20
Source File: Nd4jTestsComparisonFortran.java From deeplearning4j with Apache License 2.0 | 4 votes |
@After public void after() throws Exception { DataTypeUtil.setDTypeForContext(initialType); }