# How to predict with an imported Keras model

**URL:** https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011
**Category:** DL4J
**Created:** [September 11, 2022, 8:31pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011 "2022-09-11T20:31:30Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 11, 2022, 8:31pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/1 "2022-09-11T20:31:30Z")

</div>

I have trained an exported a model in python using Keras.

```auto
vocab_size = 50000
embedding_dim = 100
model = Sequential()
model.add(Embedding(vocab_size, embedding_dim, input_length=train_padded.shape[1]))

model.add(Conv1D(48, 7, activation='relu', padding='valid'))
model.add(GlobalMaxPooling1D())
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dropout(0.5))

model.add(Dense(7, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

```

Then I imported:

```auto
val modelPath = File("C:/Users/Cherr/Desktop/Development/transaction_classification.h5")
    val model: MultiLayerNetwork = KerasModelImport.importKerasSequentialModelAndWeights(modelPath.absolutePath)

```

So I can predict in python using:

```auto
seq = tokenizer.texts_to_sequences('transfer to new customer')
padded = pad_sequences(seq, maxlen=max_length, padding=padding_type, truncating=trunc_type)
pred = model.predict(padded)

```

I have created a padded array with word\_index of each words but dunno how to transform that array into NDArray for prediction.  
Any idea what I am doing wrong??

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 12, 2022, 10:48pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/2 "2022-09-12T22:48:14Z")

</div>

@Cherrio-LLC I feel like I’m missing something here. Could you tell me what tokenizer you’re using? We have examples for NLP but it depends on what your input word vectors are.  
You can search around in here for word2vec and other ones: [GitHub - deeplearning4j/deeplearning4j-examples: Deeplearning4j Examples (DL4J, DL4J Spark, DataVec)](https://github.com/deeplearning4j/deeplearning4j-examples)

Once I know what your source of word vectors is I can give you more concrete advice.

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 13, 2022, 1:06am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/3 "2022-09-13T01:06:00Z")

</div>

So while training I converted each unique words to index then fed the indexes of each sentence to a sequence of shape (1,10) to the embedding layer to output an array of each word’s vector.

But I’ve been able to prepare data for prediction but I have been hit by this error:

```auto
Exception in thread "main" org.nd4j.linalg.exception.ND4JIllegalStateException: New shape length doesn't match original length: [288] vs [48]. Original shape: [1, 48] New Shape: [1, 288]
	at org.nd4j.linalg.api.ndarray.BaseNDArray.reshape(BaseNDArray.java:3804)
	at org.nd4j.linalg.api.ndarray.BaseNDArray.reshape(BaseNDArray.java:3749)
	at org.nd4j.linalg.api.ndarray.BaseNDArray.reshape(BaseNDArray.java:3872)
	at org.nd4j.linalg.api.ndarray.BaseNDArray.reshape(BaseNDArray.java:4099)
	at org.deeplearning4j.preprocessors.KerasFlattenRnnPreprocessor.preProcess(KerasFlattenRnnPreprocessor.java:49)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.outputOfLayerDetached(MultiLayerNetwork.java:1299)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2467)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2430)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2421)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.output(MultiLayerNetwork.java:2408)
	at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.predict(MultiLayerNetwork.java:2270)

```

So I had to print out my imported model to see what I missed:

```auto
===============================================================================================
LayerName (LayerType) nIn,nOut TotalParams ParamsShape           
===============================================================================================
embedding_12 (EmbeddingSequenceLayer) 50000,128 6,400,000 W:{50000,128}         
conv1d_12 (Convolution1DLayer) 128,48 30,768 b:{48}, W:{48,128,5,1}
global_max_pooling1d_12 (GlobalPoolingLayer) -,- 0 -                     
dropout_24 (DropoutLayer) -,- 0 -                     
dropout_25 (DropoutLayer) -,- 0 -                     
dense_12 (DenseLayer) 288,7 2,023 W:{288,7}, b:{7}      
dense_12_loss (LossLayer) -,- 0 -                     
-----------------------------------------------------------------------------------------------
            Total Parameters: 6,432,791
        Trainable Parameters: 6,432,791
           Frozen Parameters: 0
===============================================================================================

```

There’s a lot of differences to the model i trained in python:

```auto
_________________________________________________________________
 Layer (type) Output Shape Param #   
=================================================================
 embedding_12 (Embedding) (None, 10, 128) 6400000   
                                                                 
 conv1d_12 (Conv1D) (None, 6, 48) 30768     
                                                                 
 global_max_pooling1d_12 (Gl (None, 48) 0         
 obalMaxPooling1D)                                               
                                                                 
 dropout_24 (Dropout) (None, 48) 0         
                                                                 
 flatten_12 (Flatten) (None, 48) 0         
                                                                 
 dropout_25 (Dropout) (None, 48) 0         
                                                                 
 dense_12 (Dense) (None, 7) 343       
                                                                 
=================================================================
Total params: 6,431,111
Trainable params: 6,431,111
Non-trainable params: 0

```

The model configuration:

```auto
model = Sequential()
model.add(Embedding(vocab_size, embedding_dim, input_length=train_padded.shape[1]))

model.add(Conv1D(48, 5, activation='relu', padding='valid'))
model.add(GlobalMaxPooling1D())
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

```

So I noticed the Flatten layer was missing in the summary from DL4J imported model. Anything I am doing wrong??

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 13, 2022, 1:53am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/4 "2022-09-13T01:53:35Z")

</div>

@Cherrio-LLC No flatten isn’t a “layer” in this case. If you’re ever curious about how something works the library is open source 🙂 you can look at what it’s doing. You can see what we do here:

> <https://github.com/deeplearning4j/deeplearning4j/blob/96790ea7ce13d72b918a2ce244468b7d4d2c711a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/core/KerasFlatten.java#L92>

Dl4j’s api uses pre processors for certain kinds of layers. So no you’re fine.

Could you show an end to end example of where you attempt to perform inference? Please also include some dummy data that represents the shape of your input. This part is important because I need to see what your interpretation of the inputs/outputs are first.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 13, 2022, 5:12am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/5 "2022-09-13T05:12:24Z")

</div>

@Cherrio-LLC your stackoverflow post gave me what I needed. This was actually a bug and has been fixed: [Fixes Global Pooling collapse dimension cases with rnns in keras import. by agibsonccc · Pull Request #9769 · deeplearning4j/deeplearning4j · GitHub](https://github.com/deeplearning4j/deeplearning4j/pull/9769)

Snapshots will go up soon so you can use this. Thanks!

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 13, 2022, 8:12am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/6 "2022-09-13T08:12:05Z")

</div>

Okay. Thanks. How will I know when it’s up??

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 13, 2022, 2:01pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/7 "2022-09-13T14:01:41Z")

</div>

What is the snapshot version? I saw 1.0.0-SNAPSHOT. Which I don’t believe is the latest.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 13, 2022, 2:55pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/8 "2022-09-13T14:55:02Z")

</div>

@Cherrio-LLC it actually is. We are working towards. 1.0 and currently on milestones. Please do let me know if anything else comes up.

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 13, 2022, 3:03pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/9 "2022-09-13T15:03:03Z")

</div>

@agibsonccc The latest snapshot build is 09/09/2022 is that correct?

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 13, 2022, 3:51pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/10 "2022-09-13T15:51:38Z")

</div>

I added the snapshot:

```auto
implementation "org.deeplearning4j:deeplearning4j-core:1.0.0-SNAPSHOT"

```

So when I run the code, I get the error:

```auto
Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet(Hdf5Archive.java:295)
	at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet(Hdf5Archive.java:109)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.importWeights(KerasModelUtils.java:335)
	at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:154)
	at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:55)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential(KerasModelBuilder.java:326)
	at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:218)
	at utils.nn.TestNNKt.main(TestNN.kt:449)
	at utils.nn.TestNNKt.main(TestNN.kt)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
	at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5208)
	at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5073)
	at org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:299)
	... 9 more
Caused by: java.lang.NullPointerException
	at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5164)
	... 11 more

```

I think the snapshot is not up yet.

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 14, 2022, 6:40am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/11 "2022-09-14T06:40:12Z")

</div>

@agibsonccc is the latest snapshot build up yet?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 14, 2022, 7:33am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/12 "2022-09-14T07:33:31Z")

</div>

@Cherrio-LLC here’s the latest build of the relevant code: [.github/workflows/build-deploy-cross-platform.yml · deeplearning4j/deeplearning4j@028559b · GitHub](https://github.com/deeplearning4j/deeplearning4j/actions/runs/3050836456) could you try again?

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 14, 2022, 1:15pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/13 "2022-09-14T13:15:20Z")

</div>

@agibsonccc Been getting the following error since i switched to the snapshot:

```auto
Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet(Hdf5Archive.java:295)
	at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet(Hdf5Archive.java:109)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.importWeights(KerasModelUtils.java:335)
	at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:154)
	at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:55)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential(KerasModelBuilder.java:326)
	at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:218)

```

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 14, 2022, 9:27pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/14 "2022-09-14T21:27:46Z")

</div>

@Cherrio-LLC hmm…could you post the full stack trace? There should be something at the bottom that reveals the actual problem.

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 14, 2022, 11:21pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/15 "2022-09-14T23:21:03Z")

</div>

@agibsonccc this is the full stacktrace

```auto
Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet(Hdf5Archive.java:295)
	at org.deeplearning4j.nn.modelimport.keras.Hdf5Archive.readDataSet(Hdf5Archive.java:109)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.importWeights(KerasModelUtils.java:335)
	at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:154)
	at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:55)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential(KerasModelBuilder.java:326)
	at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:218)
	at utils.nn.TestNNKt.main(TestNN.kt:449)
	at utils.nn.TestNNKt.main(TestNN.kt)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
	at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5208)
	at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5073)
	at org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:299)
	... 9 more
Caused by: java.lang.NullPointerException
	at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5164)
	... 11 more

```

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 14, 2022, 11:22pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/16 "2022-09-14T23:22:47Z")

</div>

@agibsonccc noticed if I down grade from the snapshot version, it doesn’t throw that error.

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 15, 2022, 3:57pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/17 "2022-09-15T15:57:45Z")

</div>

@agibsonccc Any update on the error??

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [September 15, 2022, 11:30pm UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/18 "2022-09-15T23:30:38Z")

</div>

> [@Cherrio-LLC](#):
>
> `Nd4j`

@Cherrio-LLC sorry I wasn’t able to prioritize this because it’s hard to act on. I"m not really sure what your specific local issue is. This feels like a fairly standard snapshots repository issue specific to your system. Could you show how you have snapshots configured? Maybe you’re hooking in to the old repository.

It’s been like this for a few releases not but we migrated from [oss.sonatype.org](http://oss.sonatype.org) to the newer [s01.oss.sonatype.org](http://s01.oss.sonatype.org). Could you clarify you’re even getting snapshots from the right location first?

BLASDelegator is a few weeks old now and shouldn’t have any issues. The only thing I can do is try re kicking the snapshots build off again and seeing what that does. I’ve done that just now and hope that resolves it.

Sorry this is just a hard to reproduce/troubleshoot issue since I don’t know enough about your local system to know why certain modules are missing.

You can always clone the source and just run mvn clean install -rf :nd4j which is a pure java only build not involving any c++ bits so that shouldn’t be hard to deal with as well.

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 16, 2022, 7:36am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/19 "2022-09-16T07:36:24Z")

</div>

Thank you for your help so far. I’m trying to update the backend to the snapshot version. But i’m getting the following error. Using gradle by the way.

```auto
Could not find nd4j-native-1.0.0-SNAPSHOT-windows-x86_64.jar (org.nd4j:nd4j-native:1.0.0-SNAPSHOT:20220916.020818-2347).
Searched in the following locations:
    https://s01.oss.sonatype.org/content/repositories/snapshots/org/nd4j/nd4j-native/1.0.0-SNAPSHOT/nd4j-native-1.0.0-20220916.020818-2347-windows-x86_64.jar

```

---

<div class="post-metadata">

### Author: ![Cherrio-LLC](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/cherrio-llc/32/904_2.png) [@Cherrio-LLC](https://community.konduit.ai/u/Cherrio-LLC)
#### Post date: [September 16, 2022, 7:46am UTC](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011/20 "2022-09-16T07:46:42Z")

</div>

My gradle config:

```auto
    implementation 'org.deeplearning4j:deeplearning4j-nn:1.0.0-SNAPSHOT'
    implementation 'org.nd4j:nd4j-native:1.0.0-SNAPSHOT'
    implementation 'org.nd4j:nd4j-native:1.0.0-SNAPSHOT:windows-x86_64'

```

[Next page](https://community.konduit.ai/t/how-to-predict-with-an-imported-keras-model/2011.md?page=2)
