Skip to content

Cannot load KERAS LSTM model if they contain KERAS Masking Layer #10166

@mzattera

Description

@mzattera

Issue Description

I am using KNIME to train KERAS models that I then import into deeplearning4j. Recently, I have been using a simple LSTM network (64 LSTM units connected to a softmax fully connected layer). As in KNIME KERAS nodes the only way of training the model with sequence of different length is to add a KERAS masking layer, I added it to my model, but as soon as I did it, it stopped loading in deeplearning4j with java.lang.ClassCastException (see below). The model loads if I do not use the masking layer.

Version Information

I have the below pom.xml configuration for deeplearnign4j. I am using KNIME 4.7.8 to generate the KERAS NN

  <!-- Core -->
  <dependency>
      <groupId>org.deeplearning4j</groupId>
      <artifactId>deeplearning4j-core</artifactId>
      <version>1.0.0-M2.1</version>
  </dependency>
    
	<!-- CUDA Engine (I have CUDA 11.8 and cudNN 9.6 -->
	<dependency>
	    <groupId>org.nd4j</groupId>
	    <artifactId>nd4j-cuda-11.6</artifactId>
	    <version>1.0.0-M2.1</version>
	</dependency>
	<dependency>
	    <groupId>org.nd4j</groupId>
	    <artifactId>nd4j-cuda-11.6</artifactId>
	    <version>1.0.0-M2.1</version>
	    <classifier>windows-x86_64-cudnn</classifier>
	</dependency>  
	<dependency>
	    <groupId>org.bytedeco</groupId>
	    <artifactId>cuda-platform-redist</artifactId>
	    <version>11.6-8.3-1.5.7</version>
	</dependency>

Additional Information

Exception Stacktrace:

Exception in thread "main" java.lang.ClassCastException: class org.deeplearning4j.nn.conf.layers.recurrent.LastTimeStep cannot be cast to class org.deeplearning4j.nn.conf.layers.FeedForwardLayer (org.deeplearning4j.nn.conf.layers.recurrent.LastTimeStep and org.deeplearning4j.nn.conf.layers.FeedForwardLayer are in unnamed module of loader 'app')
	at org.deeplearning4j.nn.modelimport.keras.layers.recurrent.KerasLSTM.setWeights(KerasLSTM.java:452)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.importWeights(KerasModelUtils.java:352)
	at org.deeplearning4j.nn.modelimport.keras.KerasModel.<init>(KerasModel.java:188)
	at org.deeplearning4j.nn.modelimport.keras.KerasModel.<init>(KerasModel.java:97)
	at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildModel(KerasModelBuilder.java:311)
	at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasModelAndWeights(KerasModelImport.java:150)

Code I use to load the model:

ComputationGraph kerasModel = KerasModelImport.importKerasModelAndWeights("kerasModel.h5", false);

The .h5 file I use is attached: kerasModel.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions