# Keras Model import error with TimeDistributed layer

**URL:** <https://community.konduit.ai/t/keras-model-import-error-with-timedistributed-layer/754>\
**Category:** DL4J\
**Created:** [July 28, 2020, 5:25am UTC](https://community.konduit.ai/t/keras-model-import-error-with-timedistributed-layer/754 "2020-07-28T05:25:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsharnag](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/rsharnag/32/277_2.png) [@rsharnag](https://community.konduit.ai/u/rsharnag)\
**Post date:** [July 28, 2020, 5:25am UTC](https://community.konduit.ai/t/keras-model-import-error-with-timedistributed-layer/754/1 "2020-07-28T05:25:50Z")

</div>

Is TimeDistributedLayer supported with 1.0.0-beta7 in DL4J ?

Model code :

```
def bilstm_model(input_length, output_size):
    input_vec = Input((input_length, 300,), name = "w2v_input")
    model = Bidirectional (LSTM (300,return_sequences=True, kernel_initializer="glorot_uniform"),merge_mode='concat')(input_vec)
    model = TimeDistributed(Dense(100,activation='relu'))(model)
    model = Flatten()(model)
    model = Dropout(0.3)(model)
    model = Dense(output_size,activation='sigmoid',kernel_initializer="glorot_uniform", use_bias=True, bias_initializer=tf.keras.initializers.Ones())(model)
    model = Model(input_vec,model, name="bilstm_model")
    return model

model = bilstm_model(
    input_length=10, 
    output_size = 5
)

model.compile(loss=BinaryCrossentropy(), optimizer='adam', metrics=["accuracy"])

```

While importing the model, Getting error :

> Exception in thread “main” org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException: Cannot assign arrays: arrays must both be scalars, both vectors, or shapes must be equal other than size 1 dimensions. Attempting to do x.assign(y) with x.shape=[100, 6299] and y.shape=[1000, 6299]  
> Tried to set weights for layer with name dense\_1, of class org.deeplearning4j.nn.conf.layers.DenseLayer.  
> Failed to set weights for parameter W  
> Expected shape for this parameter: Rank: 2, DataType: FLOAT, Offset: 0, Order: f, Shape: [100,6299], Stride: [1,100],  
> got: Rank: 2, DataType: FLOAT, Offset: 0, Order: c, Shape: [1000,6299], Stride: [6299,1]. For more information, see [https://deeplearning4j.konduit.ai/keras-import/overview](https://deeplearning4j.konduit.ai/keras-import/overview)  
> at org.deeplearning4j.nn.modelimport.keras.KerasLayer.copyWeightsToLayer(KerasLayer.java:334)  
> at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.copyWeightsToModel(KerasModelUtils.java:75)  
> at org.deeplearning4j.nn.modelimport.keras.KerasModel.getComputationGraph(KerasModel.java:418)  
> at org.deeplearning4j.nn.modelimport.keras.KerasModel.getComputationGraph(KerasModel.java:404)  
> at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasModelAndWeights(KerasModelImport.java:263)  
> at TFGraphRunner.(TFGraphRunner.java:45)  
> at TFGraphRunner.main(TFGraphRunner.java:151)

Please suggest if something can be corrected here. Same model without time distributed layer is importing in java.

---

<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:** [July 29, 2020, 10:16pm UTC](https://community.konduit.ai/t/keras-model-import-error-with-timedistributed-layer/754/2 "2020-07-29T22:16:54Z")

</div>

> [@rsharnag](#):
>
> ```auto
> def bilstm_model(input_length, output_size):
> input_vec = Input((input_length, 300,), name = "w2v_input")
> model = Bidirectional (LSTM (300,return_sequences=True, kernel_initializer="glorot_uniform"),merge_mode='concat')(input_vec)
> model = TimeDistributed(Dense(100,activation='relu'))(model)
> model = Flatten()(model)
> model = Dropout(0.3)(model)
> model = Dense(output_size,activation='sigmoid',kernel_initializer="glorot_uniform", use_bias=True, bias_initializer=tf.keras.initializers.Ones())(model)
> model = Model(input_vec,model, name="bilstm_model")
> return model
> 
> model = bilstm_model(
> input_length=10, 
> output_size = 5
> )
> 
> model.compile(loss=BinaryCrossentropy(), optimizer='adam', metrics=["accuracy"])
> 
> ```

Hi, which version of keras are you using? Is it tf.keras? How do you call model.save?  
For now if this doesn’t work, you can convert the model to tensorflow and use it like that via our samediff api. You can find more about that here:

> <https://github.com/deeplearning4j/deeplearning4j/issues/9043>
>
> \#### Issue Description
> 
> I am trying to import this keras model: https://github….com/antonior92/automatic-ecg-diagnosis/blob/cb3394da2af36f1199b03736da87478c6cc31dfc/model.py#L117-L137
> 
> and I get the error per title, more details below.
> 
> I noticed there was a ~similar issue back in 2018, but it was automatically closed with no solution.
> 
> \#### Version Information
> 
> Please indicate relevant versions, including, if relevant:
> 
> \* Deeplearning4j version 1.0.0-beta7
> 
> \#### Additional Information
> 
> Where applicable, please also provide:
> 
> \* Full log or exception stack trace 
> \`\`\`
> Exception in thread "main" java.lang.IllegalStateException: Invalid input type: Batch norm layer expected input of type CNN, CNN Flat or FF, got InputTypeRecurrent(64,timeSeriesLength=4096,format=NWC) for layer index -1, layer name = batch\_normalization\_1
> at org.deeplearning4j.nn.conf.layers.BatchNormalization.getOutputType(BatchNormalization.java:131)
> at org.deeplearning4j.nn.modelimport.keras.layers.normalization.KerasBatchNormalization.getOutputType(KerasBatchNormalization.java:165)
> at org.deeplearning4j.nn.modelimport.keras.KerasModel.inferOutputTypes(KerasModel.java:304)
> at org.deeplearning4j.nn.modelimport.keras.KerasModel.\<init\>(KerasModel.java:179)
> at org.deeplearning4j.nn.modelimport.keras.KerasModel.\<init\>(KerasModel.java:96)
> at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildModel(KerasModelBuilder.java:307)
> at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasModelAndWeights(KerasModelImport.java:172)
> at asd.App2.main(App2.java:10)
> \`\`\`
> \* pom.xml file or similar (also in a Gist)
> \`\`\`
> \<dependencies\>
> \<dependency\>
> \<groupId\>org.deeplearning4j\</groupId\>
> \<artifactId\>deeplearning4j-modelimport\</artifactId\>
> \<version\>1.0.0-beta7\</version\> // This version should match that of your other DL4J project dependencies.
> \</dependency\>
> ...
> \`\`\`
> 
> and reproducer app:
> \`\`\`
> import org.deeplearning4j.nn.graph.ComputationGraph;
> import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
> 
> public class App2 {
> public static void main(String\[\] args) throws Exception {
> ComputationGraph model = KerasModelImport.importKerasModelAndWeights(
> "../automatic-ecg-diagnosis/dnn\_predicts/model.hdf5");
> System.out.println(model);
> }
> }
> \`\`\`
> 
> 
> 
> \#### Contributing
> 
> Unfortunately I am terribly new to keras, and I'm just an average Java dev :) but if there is anything else I can support to resolved the issue let me know!
> 
> 
> Thanks!
