Skip to content

Commit 14ef75b

Browse files
authored
Merge pull request #900 from RobAltena/master
add serialization example.
2 parents 3b2f709 + b12bb8f commit 14ef75b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
794 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00,4.000000000000000000e+00,5.000000000000000000e+00
2+
6.000000000000000000e+00,7.000000000000000000e+00,8.000000000000000000e+00,9.000000000000000000e+00,1.000000000000000000e+01
3+
1.100000000000000000e+01,1.200000000000000000e+01,1.300000000000000000e+01,1.400000000000000000e+01,1.500000000000000000e+01
4+
1.600000000000000000e+01,1.700000000000000000e+01,1.800000000000000000e+01,1.900000000000000000e+01,2.000000000000000000e+01
5+
2.100000000000000000e+01,2.200000000000000000e+01,2.300000000000000000e+01,2.400000000000000000e+01,2.500000000000000000e+01

0 commit comments

Comments
 (0)