Java Code Examples for org.nd4j.linalg.api.buffer.DataType#fromNumpy()
The following examples show how to use
org.nd4j.linalg.api.buffer.DataType#fromNumpy() .
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: NumpyFormatTests.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test public void testNpzReading() throws Exception { val dir = testDir.newFolder(); new ClassPathResource("numpy_arrays/npz/").copyDirectory(dir); File[] files = dir.listFiles(); int cnt = 0; for(File f : files){ if(!f.getPath().endsWith(".npz")){ log.warn("Skipping: {}", f); continue; } String path = f.getAbsolutePath(); int lastDot = path.lastIndexOf('.'); int lastSlash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); String dtype = path.substring(lastSlash+1, lastDot); // System.out.println(path + " : " + dtype); DataType dt = DataType.fromNumpy(dtype); //System.out.println(dt); INDArray arr = Nd4j.arange(12).castTo(dt).reshape(3,4); INDArray arr2 = Nd4j.linspace(DataType.FLOAT, 0, 3, 10); Map<String,INDArray> m = Nd4j.createFromNpzFile(f); assertEquals(2, m.size()); assertTrue(m.containsKey("firstArr")); assertTrue(m.containsKey("secondArr")); assertEquals(arr, m.get("firstArr")); assertEquals(arr2, m.get("secondArr")); cnt++; } assertTrue(cnt > 0); }
Example 2
Source File: NumpyFormatTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testToNpyFormat() throws Exception { val dir = testDir.newFolder(); new ClassPathResource("numpy_arrays/").copyDirectory(dir); File[] files = dir.listFiles(); int cnt = 0; for(File f : files){ if(!f.getPath().endsWith(".npy")){ log.warn("Skipping: {}", f); continue; } String path = f.getAbsolutePath(); int lastDot = path.lastIndexOf('.'); int lastUnderscore = path.lastIndexOf('_'); String dtype = path.substring(lastUnderscore+1, lastDot); // System.out.println(path + " : " + dtype); DataType dt = DataType.fromNumpy(dtype); //System.out.println(dt); INDArray arr = Nd4j.arange(12).castTo(dt).reshape(3,4); byte[] bytes = Nd4j.toNpyByteArray(arr); byte[] expected = FileUtils.readFileToByteArray(f); /* log.info("E: {}", Arrays.toString(expected)); for( int i=0; i<expected.length; i++ ){ System.out.print((char)expected[i]); } System.out.println();System.out.println(); log.info("A: {}", Arrays.toString(bytes)); for( int i=0; i<bytes.length; i++ ){ System.out.print((char)bytes[i]); } System.out.println(); */ assertArrayEquals("Failed with file [" + f.getName() + "]", expected, bytes); cnt++; } assertTrue(cnt > 0); }
Example 3
Source File: NumpyFormatTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testToNpyFormatScalars() throws Exception { // File dir = new File("C:\\DL4J\\Git\\dl4j-test-resources\\src\\main\\resources\\numpy_arrays\\scalar"); val dir = testDir.newFolder(); new ClassPathResource("numpy_arrays/scalar/").copyDirectory(dir); File[] files = dir.listFiles(); int cnt = 0; for(File f : files){ if(!f.getPath().endsWith(".npy")){ log.warn("Skipping: {}", f); continue; } String path = f.getAbsolutePath(); int lastDot = path.lastIndexOf('.'); int lastUnderscore = path.lastIndexOf('_'); String dtype = path.substring(lastUnderscore+1, lastDot); // System.out.println(path + " : " + dtype); DataType dt = DataType.fromNumpy(dtype); //System.out.println(dt); INDArray arr = Nd4j.scalar(dt, 1); byte[] bytes = Nd4j.toNpyByteArray(arr); byte[] expected = FileUtils.readFileToByteArray(f); /* log.info("E: {}", Arrays.toString(expected)); for( int i=0; i<expected.length; i++ ){ System.out.print((char)expected[i]); } System.out.println();System.out.println(); log.info("A: {}", Arrays.toString(bytes)); for( int i=0; i<bytes.length; i++ ){ System.out.print((char)bytes[i]); } System.out.println(); */ assertArrayEquals("Failed with file [" + f.getName() + "]", expected, bytes); cnt++; System.out.println(); } assertTrue(cnt > 0); }
Example 4
Source File: NumpyFormatTests.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Test public void testNpy() throws Exception { for(boolean empty : new boolean[]{false, true}) { val dir = testDir.newFolder(); if(!empty) { new ClassPathResource("numpy_arrays/npy/3,4/").copyDirectory(dir); } else { new ClassPathResource("numpy_arrays/npy/0,3_empty/").copyDirectory(dir); } File[] files = dir.listFiles(); int cnt = 0; for (File f : files) { if (!f.getPath().endsWith(".npy")) { log.warn("Skipping: {}", f); continue; } String path = f.getAbsolutePath(); int lastDot = path.lastIndexOf('.'); int lastUnderscore = path.lastIndexOf('_'); String dtype = path.substring(lastUnderscore + 1, lastDot); // System.out.println(path + " : " + dtype); DataType dt = DataType.fromNumpy(dtype); //System.out.println(dt); INDArray exp; if(empty){ exp = Nd4j.create(dt, 0, 3); } else { exp = Nd4j.arange(12).castTo(dt).reshape(3, 4); } INDArray act = Nd4j.createFromNpyFile(f); assertEquals("Failed with file [" + f.getName() + "]", exp, act); cnt++; } assertTrue(cnt > 0); } }