{"action":"created","issue":{"url":"https://api.github.com/repos/deeplearning4j/nd4j/issues/1246","repository_url":"https://api.github.com/repos/deeplearning4j/nd4j","labels_url":"https://api.github.com/repos/deeplearning4j/nd4j/issues/1246/labels{/name}","comments_url":"https://api.github.com/repos/deeplearning4j/nd4j/issues/1246/comments","events_url":"https://api.github.com/repos/deeplearning4j/nd4j/issues/1246/events","html_url":"https://github.com/deeplearning4j/nd4j/issues/1246","id":173896286,"number":1246,"title":"Read and Write to file problem","user":{"login":"mbabadi","id":15305869,"avatar_url":"https://avatars.githubusercontent.com/u/15305869?v=3","gravatar_id":"","url":"https://api.github.com/users/mbabadi","html_url":"https://github.com/mbabadi","followers_url":"https://api.github.com/users/mbabadi/followers","following_url":"https://api.github.com/users/mbabadi/following{/other_user}","gists_url":"https://api.github.com/users/mbabadi/gists{/gist_id}","starred_url":"https://api.github.com/users/mbabadi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbabadi/subscriptions","organizations_url":"https://api.github.com/users/mbabadi/orgs","repos_url":"https://api.github.com/users/mbabadi/repos","events_url":"https://api.github.com/users/mbabadi/events{/privacy}","received_events_url":"https://api.github.com/users/mbabadi/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-08-29T23:28:03Z","updated_at":"2016-08-30T20:46:53Z","closed_at":"2016-08-30T20:46:53Z","body":"Here's a simple class I use for reading and writing NDArray to file:\r\n\r\n```\r\npackage org.broadinstitute.hellbender.tools.coveragemodel.nd4jutils;\r\n\r\nimport org.apache.commons.io.input.ReaderInputStream;\r\nimport org.apache.commons.io.output.WriterOutputStream;\r\nimport org.broadinstitute.hellbender.exceptions.UserException;\r\nimport org.nd4j.linalg.api.buffer.DataBuffer;\r\nimport org.nd4j.linalg.api.buffer.util.DataTypeUtil;\r\nimport org.nd4j.linalg.api.ndarray.INDArray;\r\nimport org.nd4j.linalg.api.shape.Shape;\r\nimport org.nd4j.linalg.factory.Nd4j;\r\n\r\nimport javax.annotation.Nonnull;\r\nimport java.io.*;\r\nimport java.nio.charset.Charset;\r\n\r\nimport static org.nd4j.linalg.factory.Nd4j.createArrayFromShapeBuffer;\r\n\r\n/**\r\n * @author Mehrtash Babadi &lt;mehrtash@broadinstitute.org&gt;\r\n */\r\npublic class Nd4jIOUtils {\r\n\r\n    /**\r\n     * Write NDArray to a writer.\r\n     *\r\n     * @param arr\r\n     * @param writer\r\n     * @param writerName\r\n     */\r\n    public static void writeNDArrayToWriter(@Nonnull final INDArray arr,\r\n                                            @Nonnull final Writer writer,\r\n                                            @Nonnull final String writerName) {\r\n        try (final DataOutputStream dos = new DataOutputStream(new WriterOutputStream(writer))) {\r\n            Nd4j.write(arr, dos);\r\n        } catch (final IOException ex) {\r\n            throw new UserException.CouldNotCreateOutputFile(writerName, \"Could not write the NDArray\", ex);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Write NDArray to a file.\r\n     *\r\n     * @param arr\r\n     * @param outputFile\r\n     */\r\n    public static void writeNDArrayToFile(@Nonnull final INDArray arr,\r\n                                          @Nonnull final File outputFile) {\r\n        try (final FileWriter writer = new FileWriter(outputFile)) {\r\n            writeNDArrayToWriter(arr, writer, outputFile.getAbsolutePath());\r\n        } catch (final IOException ex) {\r\n            throw new UserException.CouldNotCreateOutputFile(outputFile, \"Could not write the NDArray\");\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Read NDArray from a reader.\r\n     *\r\n     * @param reader\r\n     * @param readerName\r\n     * @return\r\n     */\r\n    public static INDArray readNDArrayFromReader(@Nonnull final Reader reader, @Nonnull final String readerName) {\r\n        try (final DataInputStream dis = new DataInputStream(new ReaderInputStream(reader))) {\r\n            return Nd4j.read(dis);\r\n        } catch (final IOException ex) {\r\n            throw new UserException.CouldNotReadInputFile(readerName, \"Could not read INDArray\");\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Read NDArray from a file.\r\n     *\r\n     * @param inputFile\r\n     * @return\r\n     */\r\n    public static INDArray readNDArrayFromFile(@Nonnull final File inputFile) {\r\n        try (final FileReader reader = new FileReader(inputFile)) {\r\n            return readNDArrayFromReader(reader, inputFile.getAbsolutePath());\r\n        } catch (final IOException ex) {\r\n            throw new UserException.CouldNotReadInputFile(inputFile, \"Could not read the input file\");\r\n        }\r\n    }\r\n\r\n}\r\n```\r\n\r\nThe following test fails:\r\n\r\n```\r\n    @Test\r\n    public void basicTest() {\r\n        final INDArray arr = Nd4j.rand(1000, 10);\r\n        final File outFile = createTempFile(\"Nd4j-IO-test\", \".nd4j\");\r\n        Nd4jIOUtils.writeNDArrayToFile(arr, outFile);\r\n        final INDArray arr2 = Nd4jIOUtils.readNDArrayFromFile(outFile); /* EOFException */\r\n    }\r\n```\r\n\r\nwith exception:\r\n\r\n```\r\njava.lang.RuntimeException: java.io.EOFException\r\n\r\n\tat org.nd4j.linalg.api.buffer.BaseDataBuffer.read(BaseDataBuffer.java:1297)\r\n\tat org.nd4j.linalg.factory.Nd4j.read(Nd4j.java:2223)\r\n\tat org.broadinstitute.hellbender.tools.coveragemodel.nd4jutils.Nd4jIOUtils.readNDArrayFromReader(Nd4jIOUtils.java:64)\r\n\tat org.broadinstitute.hellbender.tools.coveragemodel.nd4jutils.Nd4jIOUtils.readNDArrayFromFile(Nd4jIOUtils.java:78)\r\n....\r\nCaused by: java.io.EOFException\r\n\tat java.io.DataInputStream.readInt(DataInputStream.java:392)\r\n\tat java.io.DataInputStream.readFloat(DataInputStream.java:446)\r\n\tat org.nd4j.linalg.api.buffer.BaseDataBuffer.read(BaseDataBuffer.java:1287)\r\n```\r\n\r\nI am using nd4j-0.5.0."},"comment":{"url":"https://api.github.com/repos/deeplearning4j/nd4j/issues/comments/243575278","html_url":"https://github.com/deeplearning4j/nd4j/issues/1246#issuecomment-243575278","issue_url":"https://api.github.com/repos/deeplearning4j/nd4j/issues/1246","id":243575278,"user":{"login":"eraly","id":8340500,"avatar_url":"https://avatars.githubusercontent.com/u/8340500?v=3","gravatar_id":"","url":"https://api.github.com/users/eraly","html_url":"https://github.com/eraly","followers_url":"https://api.github.com/users/eraly/followers","following_url":"https://api.github.com/users/eraly/following{/other_user}","gists_url":"https://api.github.com/users/eraly/gists{/gist_id}","starred_url":"https://api.github.com/users/eraly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eraly/subscriptions","organizations_url":"https://api.github.com/users/eraly/orgs","repos_url":"https://api.github.com/users/eraly/repos","events_url":"https://api.github.com/users/eraly/events{/privacy}","received_events_url":"https://api.github.com/users/eraly/received_events","type":"User","site_admin":false},"created_at":"2016-08-30T20:46:53Z","updated_at":"2016-08-30T20:46:53Z","body":"@mbabadi Closing."}}
{"actor":{"display_login":"eraly"}}