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

标签 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])
我不确定突然发生了什么。有人遇到过类似的问题吗?

最佳答案

改变

self.trainable_weights = [self.W]
self._trainable_weights = [self.W]

关于nlp - 无法设置属性 "trainable_weights",可能是因为它与现有的只读属性冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63343563/

相关文章:

python - 动态时间扭曲中避免堆栈溢出的技术

r - 对于R中的文本挖掘,如何将DocumentTermMatrix与原始数据框结合起来?

python - Keras LSTM 不同的输入输出形状

tensorflow - 如何理解transformer中的masked multi-head attention

python - 从英文文本中提取产品名称

python - 您将如何按上下文对文章进行分组? - 自然语言

python - 如何为 python 的 keras LSTM 塑造大型 DataFrame?

tensorflow - 使用 TensorFlow 进行端到端语音识别的 RNN

tensorflow - 如何将 LSTM 的先前输出和隐藏状态用于注意力机制?

python - MultiheadAttention 的可学习参数数量