Skip to content

WeightNormalization layer can't save in h5 #624

@hankcs

Description

@hankcs

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
    Ubuntu 16.04.6 LTS
  • TensorFlow version and how it was installed (source or binary):
    tensorflow-gpu 2.0.0, binary
  • TensorFlow-Addons version and how it was installed (source or binary):
    tensorflow-addons 0.6.0, binary
  • Python version:
    Python 3.6.9
  • Is GPU used? (yes/no):
    yes
    Describe the bug
    WeightNormalization layer can't save to h5

A clear and concise description of what the bug is.
WeightNormalization causing duplicate names of variables.

Code to reproduce the issue

import tensorflow as tf
import tensorflow_addons as tfa

conv = tf.keras.layers.Conv1D(1, 1, name='conv')
wn_conv = tfa.layers.WeightNormalization(conv)

model = tf.keras.Sequential(layers=[wn_conv])
model.build([1, 2, 3])
print([w.name for w in model.layers[0].weights])
model.save_weights('model.h5')

Provide a reproducible test case that is the bare minimum necessary to generate the problem.
See above.

Other info / logs

['weight_normalization/g:0', 'weight_normalization/kernel:0', 'weight_normalization/bias:0', 'weight_normalization/initialized:0', 'weight_normalization/kernel:0', 'weight_normalization/bias:0']
Traceback (most recent call last):
  File "/home/ubuntu/hankcs/laser/tests/playground/wn_bug.py", line 14, in <module>
    model.save_weights('model.h5')
  File "/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 1074, in save_weights
    saving.save_weights_to_hdf5_group(f, self.layers)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 631, in save_weights_to_hdf5_group
    param_dset = g.create_dataset(name, val.shape, dtype=val.dtype)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/h5py/_hl/group.py", line 139, in create_dataset
    self[name] = dset
  File "/home/ubuntu/.local/lib/python3.6/site-packages/h5py/_hl/group.py", line 371, 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)

There are 2 weight_normalization/kernel:0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedNeeds help as a contributionlayers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions