无法设置属性“trainable_weights”,可能是因为它与现有的只读属性冲突

Roh*_*dav 4 nlp lstm attention-model

我的代码在 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])
Run Code Online (Sandbox Code Playgroud)

我不确定突然发生了什么。有人遇到类似的问题吗?

Sha*_*mad 9

改变

self.trainable_weights = [self.W]
Run Code Online (Sandbox Code Playgroud)

self._trainable_weights = [self.W]
Run Code Online (Sandbox Code Playgroud)