When I wrote a custom class of Keras, I met this error.
Solution:
change from the snippet below
class custconv2d(keras.layers.Layer):
def __init__(self):
super(custconv2d, self).__init__()
self.k = self.add_weight(shape=(1, ), initializer="random_normal", trainable=True, name='k1')
To
#add **kwargs in the __init__()
class custconv2d(keras.layers.Layer):
def __init__(self, **kwargs):
super(custconv2d, self).__init__(**kwargs)
self.k = self.add_weight(shape=(1, ), initializer="random_normal", trainable=True, name='k1')
本文介绍了一种常见的Keras自定义层实现错误及解决方案。通过调整自定义层类的初始化方法,加入**kwargs参数,可以成功避免错误并使自定义层正常工作。
1017

被折叠的 条评论
为什么被折叠?



