RuntimeError: Unable to create link (name already exists) Keras

回眸只為那壹抹淺笑 提交于 2021-01-05 07:39:53

问题


When I save my model I get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-40-853303da8647> in <module>()
      7 
      8 
----> 9 model.save(outdir+'model.h5')
     10 
     11 
5 frames
/usr/local/lib/python3.6/dist-packages/h5py/_hl/group.py in __setitem__(self, name, obj)
    371 
    372             if isinstance(obj, HLObject):
--> 373                 h5o.link(obj.id, self.id, name, lcpl=lcpl, lapl=self._lapl)
    374 
    375             elif isinstance(obj, SoftLink):

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5o.pyx in h5py.h5o.link()

RuntimeError: Unable to create link (name already exists)

This does not happen when I use built-in layers to build my model or others user defined layers. This error arises only when I use this particular user defined layer:

class MergeTwo(keras.layers.Layer):

def __init__(self, nout, **kwargs):
    super(MergeTwo, self).__init__(**kwargs)
    self.nout = nout


    self.alpha = self.add_weight(shape=(self.nout,), initializer='zeros',
                             trainable=True)

    self.beta = self.add_weight(shape=(self.nout,), initializer='zeros',
                             trainable=True)

def call(self, inputs):
    A, B = inputs
    result = keras.layers.add([self.alpha*A ,self.beta*B])
    result = keras.activations.tanh(result)
    return result


def get_config(self):
    config = super(MergeTwo, self).get_config()
    config['nout'] = self.nout
    return config

I read the Docs but nothing worked, I cannot figure out why. I am using Google Colab and Tensorflow version 2.2.0


回答1:


I think the problem is that both of your weight variables have internally the same name, which should not happen, you can give them names with the name parameter to add_weight:

self.alpha = self.add_weight(shape=(self.nout,), initializer='zeros',
                         trainable=True, name="alpha")

self.beta = self.add_weight(shape=(self.nout,), initializer='zeros',
                         trainable=True, name="beta")

This should workaround the problem.



来源:https://stackoverflow.com/questions/62169315/runtimeerror-unable-to-create-link-name-already-exists-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!