【问题标题】:Can't set the attribute "trainable_weights", likely because it conflicts with an existing read-only无法设置属性“trainable_weights”,可能是因为它与现有的只读冲突
【发布时间】: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


【解决方案1】:

改变

self.trainable_weights = [self.W]

self._trainable_weights = [self.W]

【讨论】:

    【解决方案2】:

    这是 colab 中 tf 的持续问题。我可以得到这个here的链接

    看起来问题已经解决了,也许是时候重新打开了。

    【讨论】:

      【解决方案3】:

      Please remove the build function and use this instead, It worked for me.

          def build(self,input_shape):
              self.W=self.add_weight(name="att_weight",shape=(input_shape[-1],1),initializer="normal", trainable = True)
              self.b=self.add_weight(name="att_bias",shape=(self.attention_dim,),initializer="normal", trainable = True)
              self.u=self.add_weight(name="u_bias",shape=(self.attention_dim,1),initializer="normal", trainable = True)        
              super(Attention, self).build(input_shape)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-24
        • 1970-01-01
        • 1970-01-01
        • 2020-11-17
        • 1970-01-01
        • 2018-12-07
        • 2021-01-22
        • 1970-01-01
        相关资源
        最近更新 更多