tensorflow: Saving tf.keras.Sequential model fails with RNN containing more than one GRUCell

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes
  • OS Platform and Distribution: Linux Ubuntu 18.04
  • Mobile device if the issue happens on mobile device: -
  • TensorFlow installed from: binary (tf-nightly-2.0-preview)
  • TensorFlow version: GIT_VERSION = v1.12.1-7529-g3e0ad8a004, VERSION = 2.0.0-dev20190731
  • Python version: 3.6.9
  • Bazel version (if compiling from source): -
  • GCC/Compiler version (if compiling from source): -
  • CUDA/cuDNN version: CPU only
  • GPU model and memory: CPU only

Describe the current behavior Saving a tf.keras.Sequential model with tf.keras.layers.RNN fails when containing more than one tf.keras.layers.GRUCell with error message RuntimeError: Unable to create link (name already exists).

Describe the expected behavior Saving should succeed, not only when having one cell.

Code to reproduce the issue

import tensorflow as tf

# saving succeeds for number_of_cells = 1, but fails for number_of_cells > 1
number_of_cells = 2

model = tf.keras.Sequential()

model.add(tf.keras.layers.Input(shape=(1, 1,)))

cells = []

for i in range(number_of_cells):
    cells.append(tf.keras.layers.GRUCell(10))

model.add(tf.keras.layers.RNN(cells))

model.save("rnn.h5")

Other info / logs Behavior is the same when using SimpleRNNCell instead of GRUCell.

Traceback in case of failure:

Traceback (most recent call last):
  File "test_rnn_gru.py", line 17, in <module>
    model.save("rnn.h5")
  File "/home/test/dev/tensorflow/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 1157, in save
    saving.save_model(self, filepath, overwrite, include_optimizer, save_format)
  File "/home/test/dev/tensorflow/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/save.py", line 105, in save_model
    model, filepath, overwrite, include_optimizer)
  File "/home/test/dev/tensorflow/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 103, in save_model_to_hdf5
    save_weights_to_hdf5_group(model_weights_group, model_layers)
  File "/home/test/dev/tensorflow/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 625, in save_weights_to_hdf5_group
    param_dset = g.create_dataset(name, val.shape, dtype=val.dtype)
  File "/home/test/dev/tensorflow/tf2/lib/python3.6/site-packages/h5py/_hl/group.py", line 139, in create_dataset
    self[name] = dset
  File "/home/test/dev/tensorflow/tf2/lib/python3.6/site-packages/h5py/_hl/group.py", line 373, in __setitem__
    h5o.link(obj.id, self.id, name, lcpl=lcpl, lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 202, in h5py.h5o.link
RuntimeError: Unable to create link (name already exists)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (3 by maintainers)

Most upvoted comments

@jvishnuvardhan Thank you, that worked more or less, but after trying a full cleanup of my pip3 packages, because I got a warning due to some old tf packages (not in the virtualenv), something seems to be broken. So in the end I decided to use the nightly docker image, which works like a charm.

With the current nightly (2.2.0-dev20200202), loading the tf model works fine, which solves my problem, so I am closing the ticket. Thanks for the support!

@padoremu, Can you try saving the model in tf format? using model.save('rnn.tf',save_format='tf'). It worked fine for me without any issue’s. Kindly find the gist of colab for your reference.Thanks!

I tried the workaround model.save("rnn.h5", include_optimizer=False) according to #27688, but this didn’t help. I also tried to manually set different names for the cells with GRUCell(10, name="cell" + str(i)), which didn’t help either.