【发布时间】:2020-08-10 15:50:14
【问题描述】:
我的代码在 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])
我不确定突然发生了什么。有人遇到过类似的问题吗?
【问题讨论】:
-
我遇到了同样的问题。你搞清楚了吗?
-
否,但我删除了构建功能并用另一个代码替换了它。它奏效了。您可以检查下面的代码。我已将其发布在答案中。
标签: nlp lstm attention-model