RuntimeError:无法创建链接(名称已存在)Keras [英] RuntimeError: Unable to create link (name already exists) Keras
问题描述
保存模型时,出现以下错误:
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
我阅读了文档,但是没有任何效果,我无法弄清楚解释为什么. 我正在使用Google Colab和Tensorflow版本2.2.0
I read the Docs but nothing worked, I cannot figure out why. I am using Google Colab and Tensorflow version 2.2.0
推荐答案
我认为问题在于两个权重变量在内部具有相同的名称,这种情况不应该发生,您可以使用name
参数给它们命名到add_weight
:
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.
这篇关于RuntimeError:无法创建链接(名称已存在)Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!