Skip to content

Commit

Permalink
PR review comments.
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Altena <Rob@Ra-ai.com>
  • Loading branch information
RobAltena committed Sep 12, 2019
1 parent 56d1a68 commit b12bb8f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Expand Up @@ -4,6 +4,7 @@
import org.nd4j.linalg.factory.Nd4j;

import java.io.*;
import java.util.Map;
import java.util.Objects;

/**
Expand All @@ -12,47 +13,64 @@
*/
public class Nd4jEx16_Serialization {

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
ClassLoader loader = Nd4jEx16_Serialization.class.getClassLoader(); // used to read files from resources.

// 1. binary format from stream.
INDArray arrWrite = Nd4j.linspace(1,25,25).reshape(5,5);
String pathname = "tmp.bin";
DataOutputStream sWrite = new DataOutputStream(new FileOutputStream(new File(pathname )));
Nd4j.write(arrWrite, sWrite);

DataInputStream sRead = new DataInputStream(new FileInputStream(new File(pathname )));
INDArray arrRead = Nd4j.read(sRead);
try(DataOutputStream sWrite = new DataOutputStream(new FileOutputStream(new File(pathname )))){
Nd4j.write(arrWrite, sWrite);
}

INDArray arrRead;
try(DataInputStream sRead = new DataInputStream(new FileInputStream(new File(pathname )))){
arrRead = Nd4j.read(sRead);
}

// We now have our test matrix in arrRead
System.out.println("Read from binary format:" );
System.out.println("Read from binary stream:" );
System.out.println(arrRead );


// 2. Read the numpy npy (and npz) formats:
File file = new File( Objects.requireNonNull(loader.getResource("twentyfive.npy")).getFile());
// 2. Write and read the numpy npy format:
File file = new File("nd4j.npy" );
Nd4j.writeAsNumpy(arrRead, file ); // Try to read this file from Python: y = np.load('nd4j.npy')

arrRead = Nd4j.createFromNpyFile(file); // We can read these files from nd4j.
System.out.println();
System.out.println("Read from Numpy .npy format:" );
System.out.println(arrRead);


// 3. Read the numpy npz format:
file = new File( Objects.requireNonNull(loader.getResource("numpyz.npz")).getFile());

INDArray x = Nd4j.createFromNpyFile(file); // Nd4j.createFromNpzFile for npz Numpy files.
Map<String, INDArray> arrayMap = Nd4j.createFromNpzFile(file); //We get a map reading an .npz file.
System.out.println();
System.out.println("Read from Numpy .npyformat:" );
System.out.println(x);
System.out.println("Read from Numpy .npz format:" );
System.out.println(arrayMap.get("arr_0")); //We know there are 2 arrays in the .npz file.
System.out.println(arrayMap.get("arr_1"));


// 3. binary format from file.
// 4. binary format from file.
file = new File(pathname);
Nd4j.saveBinary(arrWrite, file);
arrRead = Nd4j.readBinary(file );
System.out.println();
System.out.println("Read from Numpy .npyformat:" );
System.out.println("Read from binary format:" );
System.out.println(arrRead);


// 4. read a csv file.
// 5. read a csv file.
file = new File( Objects.requireNonNull(loader.getResource("twentyfive.csv")).getFile());
String Filename = file.getAbsolutePath();
arrRead = Nd4j.readNumpy(Filename, ",");
System.out.println();
System.out.println("Read from csv format:" );
System.out.println(arrRead);

}

}
Binary file added nd4j-examples/src/main/resources/numpyz.npz
Binary file not shown.
Binary file removed nd4j-examples/src/main/resources/twentyfive.npy
Binary file not shown.

0 comments on commit b12bb8f

Please sign in to comment.