Issue
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
Solution
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.
Answered By - Dr. Snoopy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.