我的代码在 colab 中完美运行。但今天它没有运行。它说
无法设置属性“trainable_weights”,可能是因为它与对象的现有只读@property 冲突。请选择其他名称。
我正在将 LSTM 与注意层一起使用。
类注意力(层):
def __init__(self, **kwargs):
self.init = initializers.get('normal')
#self.input_spec = [InputSpec(ndim=3)]
super(Attention, self).__init__(**kwargs)
def build(self, input_shape):
assert len(input_shape)==3
#self.W = self.init((input_shape[-1],1))
self.W = self.init((input_shape[-1],))
#self.input_spec = [InputSpec(shape=input_shape)]
self.trainable_weights = [self.W]
super(Attention, self).build(input_shape) # be sure you call this somewhere!
def call(self, x, mask=None):
eij = K.tanh(K.dot(x, self.W))
ai = K.exp(eij)
weights = ai/K.sum(ai, axis=1).dimshuffle(0,'x')
weighted_input = x*weights.dimshuffle(0,1,'x')
return weighted_input.sum(axis=1)
def get_output_shape_for(self, input_shape):
return (input_shape[0], input_shape[-1])
我不确定突然发生了什么。有人遇到过类似的问题吗?
最佳答案
改变
self.trainable_weights = [self.W]
到self._trainable_weights = [self.W]
关于nlp - 无法设置属性 "trainable_weights",可能是因为它与现有的只读属性冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63343563/