|
4 | 4 | import org.nd4j.linalg.factory.Nd4j;
|
5 | 5 |
|
6 | 6 | import java.io.*;
|
| 7 | +import java.util.Map; |
7 | 8 | import java.util.Objects;
|
8 | 9 |
|
9 | 10 | /**
|
|
12 | 13 | */
|
13 | 14 | public class Nd4jEx16_Serialization {
|
14 | 15 |
|
15 |
| - public static void main(String[] args) throws IOException { |
| 16 | + public static void main(String[] args) throws Exception { |
16 | 17 | ClassLoader loader = Nd4jEx16_Serialization.class.getClassLoader(); // used to read files from resources.
|
17 | 18 |
|
18 | 19 | // 1. binary format from stream.
|
19 | 20 | INDArray arrWrite = Nd4j.linspace(1,25,25).reshape(5,5);
|
20 | 21 | String pathname = "tmp.bin";
|
21 |
| - DataOutputStream sWrite = new DataOutputStream(new FileOutputStream(new File(pathname ))); |
22 |
| - Nd4j.write(arrWrite, sWrite); |
23 | 22 |
|
24 |
| - DataInputStream sRead = new DataInputStream(new FileInputStream(new File(pathname ))); |
25 |
| - INDArray arrRead = Nd4j.read(sRead); |
| 23 | + try(DataOutputStream sWrite = new DataOutputStream(new FileOutputStream(new File(pathname )))){ |
| 24 | + Nd4j.write(arrWrite, sWrite); |
| 25 | + } |
| 26 | + |
| 27 | + INDArray arrRead; |
| 28 | + try(DataInputStream sRead = new DataInputStream(new FileInputStream(new File(pathname )))){ |
| 29 | + arrRead = Nd4j.read(sRead); |
| 30 | + } |
| 31 | + |
26 | 32 | // We now have our test matrix in arrRead
|
27 |
| - System.out.println("Read from binary format:" ); |
| 33 | + System.out.println("Read from binary stream:" ); |
28 | 34 | System.out.println(arrRead );
|
29 | 35 |
|
30 | 36 |
|
31 |
| - // 2. Read the numpy npy (and npz) formats: |
32 |
| - File file = new File( Objects.requireNonNull(loader.getResource("twentyfive.npy")).getFile()); |
| 37 | + // 2. Write and read the numpy npy format: |
| 38 | + File file = new File("nd4j.npy" ); |
| 39 | + Nd4j.writeAsNumpy(arrRead, file ); // Try to read this file from Python: y = np.load('nd4j.npy') |
| 40 | + |
| 41 | + arrRead = Nd4j.createFromNpyFile(file); // We can read these files from nd4j. |
| 42 | + System.out.println(); |
| 43 | + System.out.println("Read from Numpy .npy format:" ); |
| 44 | + System.out.println(arrRead); |
| 45 | + |
| 46 | + |
| 47 | + // 3. Read the numpy npz format: |
| 48 | + file = new File( Objects.requireNonNull(loader.getResource("numpyz.npz")).getFile()); |
33 | 49 |
|
34 |
| - INDArray x = Nd4j.createFromNpyFile(file); // Nd4j.createFromNpzFile for npz Numpy files. |
| 50 | + Map<String, INDArray> arrayMap = Nd4j.createFromNpzFile(file); //We get a map reading an .npz file. |
35 | 51 | System.out.println();
|
36 |
| - System.out.println("Read from Numpy .npyformat:" ); |
37 |
| - System.out.println(x); |
| 52 | + System.out.println("Read from Numpy .npz format:" ); |
| 53 | + System.out.println(arrayMap.get("arr_0")); //We know there are 2 arrays in the .npz file. |
| 54 | + System.out.println(arrayMap.get("arr_1")); |
38 | 55 |
|
39 | 56 |
|
40 |
| - // 3. binary format from file. |
| 57 | + // 4. binary format from file. |
41 | 58 | file = new File(pathname);
|
42 | 59 | Nd4j.saveBinary(arrWrite, file);
|
43 | 60 | arrRead = Nd4j.readBinary(file );
|
44 | 61 | System.out.println();
|
45 |
| - System.out.println("Read from Numpy .npyformat:" ); |
| 62 | + System.out.println("Read from binary format:" ); |
46 | 63 | System.out.println(arrRead);
|
47 | 64 |
|
48 | 65 |
|
49 |
| - // 4. read a csv file. |
| 66 | + // 5. read a csv file. |
50 | 67 | file = new File( Objects.requireNonNull(loader.getResource("twentyfive.csv")).getFile());
|
51 | 68 | String Filename = file.getAbsolutePath();
|
52 | 69 | arrRead = Nd4j.readNumpy(Filename, ",");
|
53 | 70 | System.out.println();
|
54 | 71 | System.out.println("Read from csv format:" );
|
55 | 72 | System.out.println(arrRead);
|
| 73 | + |
56 | 74 | }
|
57 | 75 |
|
58 | 76 | }
|
0 commit comments