# Which keras import version can be run perfect

**URL:** <https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859>\
**Category:** Uncategorized\
**Created:** [September 17, 2020, 3:03pm UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859 "2020-09-17T15:03:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![1329154840](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/1329154840/32/286_2.png) [@1329154840](https://community.konduit.ai/u/1329154840)\
**Post date:** [September 17, 2020, 3:03pm UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/1 "2020-09-17T15:03:33Z")

</div>

Which keras version can run perfect in 1.0.0-beta7.I try some of keras verision to import,but python keras predict is different from dl4j import predict

---

<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 18, 2020, 1:07am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/2 "2020-09-18T01:07:30Z")

</div>

@1329154840 could you be more specific? If you have a file you tried to import and it failed, could just tell us what it is directly? We have a ton of unit tests (hundreds of model files) that work on both keras 1 and 2, as well as tf keras.

We can help you with your specific problem if you tell us a bit more.

---

<div class="post-metadata">

**Author:** ![1329154840](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/1329154840/32/286_2.png) [@1329154840](https://community.konduit.ai/u/1329154840)\
**Post date:** [September 18, 2020, 2:35am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/3 "2020-09-18T02:35:19Z")

</div>

keras 2.2.4

 ![WechatIMG64](https://canada1.discourse-cdn.com/flex035/uploads/konduit/original/1X/e5fa21c442db17864f25a27cfef4cb58c0f9130d.png)

Exception in DL4J  
Exception in thread “main” org.deeplearning4j.exception.DL4JInvalidConfigException: Invalid configuration for layer (idx=-1, name=conv2d\_1, type=ConvolutionLayer) for height dimension: Invalid input configuration for kernel height. Require 0 \< kH \<= inHeight + 2\*padH; got (kH=5, inHeight=1, padH=0)  
Input type = InputTypeConvolutional(h=1,w=187,c=1), kernel = [5, 5], strides = [1, 1], padding = [0, 0], layer size (output channels) = 32, convolution mode = Same  
at org.deeplearning4j.nn.conf.layers.InputTypeUtil.getOutputTypeCnnLayers(InputTypeUtil.java:328)  
at org.deeplearning4j.nn.conf.layers.ConvolutionLayer.getOutputType(ConvolutionLayer.java:184)  
at org.deeplearning4j.nn.modelimport.keras.layers.convolutional.KerasConvolution2D.getOutputType(KerasConvolution2D.java:150)  
at org.deeplearning4j.nn.modelimport.keras.KerasModel.inferOutputTypes(KerasModel.java:304)  
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.(KerasSequentialModel.java:147)  
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.(KerasSequentialModel.java:58)  
at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential(KerasModelBuilder.java:322)  
at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:180)  
at h5.main(h5.java:40)

---

<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 18, 2020, 4:24am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/4 "2020-09-18T04:24:05Z")

</div>

Could you paste the text so I don’t have to use OCR to get the code out to run this model? 🙂 Thanks!

---

<div class="post-metadata">

**Author:** ![1329154840](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/1329154840/32/286_2.png) [@1329154840](https://community.konduit.ai/u/1329154840)\
**Post date:** [September 18, 2020, 4:56am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/6 "2020-09-18T04:56:36Z")

</div>

def CNN( X\_train, X\_val,X\_test, y\_train,y\_val,y\_test,maxlen):  
#th （dim\_ordering=‘th’）： [samples][channels][rows][cols]  
#tf （dim\_ordering=‘tf’）： [samples][rows][cols][channels]  
X\_train = X\_train.reshape(-1, 1, 1, maxlen)  
X\_val = X\_val.reshape(-1, 1, 1, maxlen)  
X\_test = X\_test.reshape(-1, 1,1, maxlen)  
model = Sequential()  
# Conv layer 1 output shape (787，32, 1, max\_len）  
model.add(Convolution2D(  
batch\_input\_shape=(None, 1, 1, maxlen),  
filters=32,  
kernel\_size=[1,5],  
strides=1,  
padding=‘same’, # Padding method  
data\_format=‘channels\_first’,  
))  
model.add(Activation(‘relu’))  
# Pooling layer 1 (max pooling) output shape (787，32, 1, max\_len)  
model.add(MaxPooling2D(  
pool\_size=(1,2),  
strides=1,  
padding=‘same’, # Padding method  
data\_format=‘channels\_first’,  
))  
# Conv layer 2 output shape (787，64, 1, max\_len)  
model.add(Convolution2D(64, 5, strides=1, padding=‘same’, data\_format=‘channels\_first’))  
model.add(Activation(‘relu’))  
# Pooling layer 2 (max pooling) output shape (787，64, 1, max\_len)  
model.add(MaxPooling2D((1,5), 1,padding= ‘same’, data\_format=‘channels\_first’))  
# Fully connected layer 1 input shape (64 \* 1 \* max\_len) , output shape (64_max\_len=64_187=11968)  
model.add(Flatten())  
model.add(Dense(512))  
model.add(Activation(‘relu’))  
model.add(Dense(1))  
adam = Adam(lr=1e-4)  
# We add metrics to get more results you want to see  
model.compile(optimizer=adam,loss=‘mean\_squared\_error’, metrics=[‘accuracy’])  
print(‘Training ------------’)  
early\_stopping = keras.callbacks.EarlyStopping(monitor=‘val\_loss’,patience=10,verbose=1,mode=‘auto’)  
model.fit(X\_train, y\_train, epochs=100, batch\_size=256, callbacks=[early\_stopping],validation\_data=(X\_val, y\_val))  
print(‘\nTesting ------------’)  
# Evaluate the model with the metrics we defined earlier  
loss= model.evaluate(X\_test, y\_test)  
print('\ntest loss: ', loss)  
model.save(‘C:/Users/86188/Desktop/cnn\_pwa\_model\_0.h5’)  
return model.predict(X\_test)

---

<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 18, 2020, 5:45am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/7 "2020-09-18T05:45:28Z")

</div>

Thanks! I have your model running locally. I’m not seeing that with the latest version.  
I fixed a lot of the ordering issues here:

> <https://github.com/KonduitAI/deeplearning4j/pull/523>
>
> \## What changes were proposed in this pull request?
> Fixes
> https://github.com/e…clipse/deeplearning4j/issues/8620
> https://github.com/eclipse/deeplearning4j/issues/6339
> https://github.com/eclipse/deeplearning4j/issues/8441
> https://github.com/eclipse/deeplearning4j/issues/9043
> https://github.com/eclipse/deeplearning4j/issues/2418
> (Please fill in changes proposed in this fix)
> 
> \## How was this patch tested?
> Adds a model import that ensures channels last/first works for sequential models. 
> (Please explain how this patch was tested. E.g. unit tests, integration tests, manual tests)
> Need to add support for computation graph as well.
> \## Quick checklist
> 
> The following checklist helps ensure your PR is complete:
> 
> \- \[X\] Eclipse Contributor Agreement signed, and signed commits - see \[IP Requirements\](https://deeplearning4j.org/eclipse-contributors) page for details
> \- \[X \] Reviewed the \[Contributing Guidelines\](https://github.com/eclipse/deeplearning4j/blob/master/CONTRIBUTING.md) and followed the steps within.
> \- \[X\] Created tests for any significant new code additions.
> \- \[X \] Relevant tests for your changes are passing.

Those will be in snapshots soon.

Could you tell me what length you used for your model?

---

<div class="post-metadata">

**Author:** ![1329154840](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/1329154840/32/286_2.png) [@1329154840](https://community.konduit.ai/u/1329154840)\
**Post date:** [September 20, 2020, 2:02am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/8 "2020-09-20T02:02:50Z")

</div>

The model of length is 187

---

<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 21, 2020, 3:27am UTC](https://community.konduit.ai/t/which-keras-import-version-can-be-run-perfect/859/9 "2020-09-21T03:27:37Z")

</div>

Just confirmed that this has already been fixed in the latest version. You’ll have to wait till we merge the latest changes master back to eclipse/deeplearning4j.  
(See: [GitHub - KonduitAI/deeplearning4j: Eclipse Deeplearning4j, ND4J, DataVec and more - deep learning & linear algebra for Java/Scala with GPUs + Spark](https://github.com/KonduitAI/deeplearning4j))

We’ll do that after a few more changes. Then the fix will be available via snapshots.
