|
| 1 | +package org.nd4j.examples; |
| 2 | + |
| 3 | +import org.nd4j.linalg.api.ndarray.INDArray; |
| 4 | +import org.nd4j.linalg.factory.Nd4j; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +/** |
| 11 | + * Nd4j provides serialization of INDArrays many formats. This example gives some examples for binary and text |
| 12 | + * serialization. |
| 13 | + */ |
| 14 | +public class Nd4jEx16_Serialization { |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + ClassLoader loader = Nd4jEx16_Serialization.class.getClassLoader(); // used to read files from resources. |
| 18 | + |
| 19 | + // 1. binary format from stream. |
| 20 | + INDArray arrWrite = Nd4j.linspace(1,25,25).reshape(5,5); |
| 21 | + String pathname = "tmp.bin"; |
| 22 | + |
| 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 | + |
| 32 | + // We now have our test matrix in arrRead |
| 33 | + System.out.println("Read from binary stream:" ); |
| 34 | + System.out.println(arrRead ); |
| 35 | + |
| 36 | + |
| 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()); |
| 49 | + |
| 50 | + Map<String, INDArray> arrayMap = Nd4j.createFromNpzFile(file); //We get a map reading an .npz file. |
| 51 | + System.out.println(); |
| 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")); |
| 55 | + |
| 56 | + |
| 57 | + // 4. binary format from file. |
| 58 | + file = new File(pathname); |
| 59 | + Nd4j.saveBinary(arrWrite, file); |
| 60 | + arrRead = Nd4j.readBinary(file ); |
| 61 | + System.out.println(); |
| 62 | + System.out.println("Read from binary format:" ); |
| 63 | + System.out.println(arrRead); |
| 64 | + |
| 65 | + |
| 66 | + // 5. read a csv file. |
| 67 | + file = new File( Objects.requireNonNull(loader.getResource("twentyfive.csv")).getFile()); |
| 68 | + String Filename = file.getAbsolutePath(); |
| 69 | + arrRead = Nd4j.readNumpy(Filename, ","); |
| 70 | + System.out.println(); |
| 71 | + System.out.println("Read from csv format:" ); |
| 72 | + System.out.println(arrRead); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments