Skip to content

Instantly share code, notes, and snippets.

@cawfree
Last active March 17, 2017 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cawfree/901c26f42233b7770935fe5bc8869d84 to your computer and use it in GitHub Desktop.
Save cawfree/901c26f42233b7770935fe5bc8869d84 to your computer and use it in GitHub Desktop.
`ModelSerializer` fails on Android. (0.8.0) invoke virtual method flush() on a null Object
/** GRADLE: (Any custom modifications I've made here were required to prevent duplicate file errors! */
// DL4J
provided ('org.deeplearning4j:deeplearning4j-core:0.8.0') {
}
provided ('org.nd4j:nd4j-native:0.8.0') {
//exclude group: 'org.bytedeco.javacpp-presets'
}
provided (group: 'org.nd4j', name: 'nd4j-native-platform', version: '0.8.0') {
//exclude group: 'org.bytedeco.javacpp-presets'
}
packagingOptions {
}
/** DALVIK: */
(new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
DenseLayer inputLayer = new DenseLayer.Builder()
.nIn(2)
.nOut(3)
.name("Input")
.build();
DenseLayer hiddenLayer = new DenseLayer.Builder()
.nIn(3)
.nOut(2)
.name("Hidden")
.build();
OutputLayer outputLayer = new OutputLayer.Builder()
.nIn(2)
.nOut(2)
.name("Output")
.activation(Activation.SOFTMAX)
.build();
NeuralNetConfiguration.Builder nncBuilder = new NeuralNetConfiguration.Builder();
nncBuilder.iterations(3);
nncBuilder.learningRate(0.1);
NeuralNetConfiguration.ListBuilder listBuilder = nncBuilder.list();
listBuilder.layer(0, inputLayer);
listBuilder.layer(1, hiddenLayer);
listBuilder.layer(2, outputLayer);
listBuilder.backprop(true);
MultiLayerNetwork myNetwork = new MultiLayerNetwork(listBuilder.build());
myNetwork.init();
final int NUM_SAMPLES = 4;
INDArray trainingInputs = Nd4j.zeros(NUM_SAMPLES, inputLayer.getNIn());
INDArray trainingOutputs = Nd4j.zeros(NUM_SAMPLES, outputLayer.getNOut());
// If 0,0 show 0
trainingInputs.putScalar(new int[]{0,0}, 0);
trainingInputs.putScalar(new int[]{0,1}, 0);
trainingOutputs.putScalar(new int[]{0,0}, 0);
trainingOutputs.putScalar(new int[]{0,1}, 1);
// If 0,1 show 1
trainingInputs.putScalar(new int[]{1,0}, 0);
trainingInputs.putScalar(new int[]{1,1}, 1);
trainingOutputs.putScalar(new int[]{1,0}, 1);
trainingOutputs.putScalar(new int[]{1,1}, 0);
// If 1,0 show 1
trainingInputs.putScalar(new int[]{2,0}, 1);
trainingInputs.putScalar(new int[]{2,1}, 0);
trainingOutputs.putScalar(new int[]{2,0}, 1);
trainingOutputs.putScalar(new int[]{2,1}, 0);
// If 1,1 show 0
trainingInputs.putScalar(new int[]{3,0}, 1);
trainingInputs.putScalar(new int[]{3,1}, 1);
trainingOutputs.putScalar(new int[]{3,0}, 0);
trainingOutputs.putScalar(new int[]{3,1}, 1);
DataSet myData = new DataSet(trainingInputs, trainingOutputs);
final ByteArrayOutputStream lByteArrayOutputStream = new ByteArrayOutputStream();
try {
// (Or path/file based calls with FileOutputStream; these throw the same Exception.
ModelSerializer.writeModel(myNetwork, lByteArrayOutputStream, false);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}).execute();
/* STACK TRACE: */
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.flush()' on a null object reference
at java.util.zip.DeflaterOutputStream.flush(DeflaterOutputStream.java:200)
at org.deeplearning4j.util.ModelSerializer.writeModel(ModelSerializer.java:126)
at io.github.cawfree.thoughtboy.fragment.SketchFragment$4.doInBackground(SketchFragment.java:318)
at io.github.cawfree.thoughtboy.fragment.SketchFragment$4.doInBackground(SketchFragment.java:236)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment