Import a Tensorflow Keras GRU network into Matlab

5 views (last 30 days)
I am trying to import a simple GRU tensorflow model into Matlab, but something is not working properly.
The tensorflow model in Python is given by the following function.
%Python function for GRU model
def create_model(nodes1, nodes2, history, BATCH_SIZE):
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.GRU(nodes1,
return_sequences=True,
stateful=True,
batch_input_shape=(BATCH_SIZE,history,len(features))))
model.add(tf.keras.layers.GRU(nodes2))
model.add(tf.keras.layers.Dense(horizon))
model.compile(
optimizer=tf.keras.optimizers.Adam(clipvalue=2.0),
loss='mse',
metrics=['mean_absolute_error', 'mean_squared_error'])
return model
Using
importKerasNetwork('model.h5')
does not work. The error I get is.
Warning: Unable to import layer. Keras layer 'GRU' with the specified settings is not supported. The problem was: Recurrent biases for GRU layers are not supported.
Error using nnet.internal.cnn.keras.importKerasNetwork (line 31)
Unable to import network because some network layers are not supported. To import layers and weights, call importKerasLayers with 'ImportWeights' set to true.
Error in importKerasNetwork (line 91)
Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
Using
importKerasLayers('model.h5','ImportWeights',true)
yields the following warnings:
Warning: Unable to import layer.
Keras layer 'GRU' with the specified settings is not supported. The problem was: Recurrent biases for GRU layers are not supported.
Warning: Unable to import layer.
Keras layer 'GRU' with the specified settings is not supported. The problem was: Recurrent biases for GRU layers are not supported.
Warning: Unable to import some Keras layers, because they are not supported by the Deep Learning Toolbox. They have been replaced by placeholder layers. To find these layers, call the function `findPlaceholderLayers` on the returned object.
As a result the two GRU layers are replaced by placeholders. I do not understand what recurrent bias is, as I have not specified anything like this anywhere nor does this setting exist in the model's configuration.
I then tried to import the configuration and weights seperately, but I get the following error when calling
importKerasNetwork('arch.json',"WeightFile",'weights.h5')
Error using nnet.internal.cnn.keras.importKerasNetwork (line 20)
Error reading Keras model_config from file 'arch.json'. The error message was: 'Dot indexing is not supported for variables of this type.''
Error in importKerasNetwork (line 91)
Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
So what exactly are reccurent biases and is there a way to solve the dot indexing error? Is there an easy way to directly import the Keras model?
Thanks in advance.
Using Tensorflow 2 and Matlab 2020a

Answers (1)

Divya Gaddipati
Divya Gaddipati on 1 Sep 2020
This is a known issue and the developers are looking into it.
As a workaround, it is possible to implement GRU using a custom layer but the the forward and backward passes for this layer need to be written manually.
Instead, you can also refer to the attached files for implementing a custom GRU layer and an example file for its usage. In this case, the layer needs to be defined and the weights and biases need to be assigned manually and replace the placeholder layer in the imported network.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!