无法设置属性"trainable_weights",可能是因为它与现有的只读冲突 [英] Can't set the attribute "trainable_weights", likely because it conflicts with an existing read-only

查看:326
本文介绍了无法设置属性"trainable_weights",可能是因为它与现有的只读冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在colab中运行完美.但是今天它没有运行.它说无法设置属性"trainable_weights",可能是因为它与对象的现有只读属性冲突.请选择其他名称.

My code was running perfectly in colab. But today it's not running. It says Can't set the attribute "trainable_weights", likely because it conflicts with an existing read-only @property of the object. Please choose a different name.

我正在将LSTM与关注层配合使用.

I am using LSTM with the attention layer.

班级注意力(层):

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])

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

I am not sure what happened suddenly. Anyone encounter similar problem?

推荐答案

更改

self.trainable_weights = [self.W]

self._trainable_weights = [self.W]

这篇关于无法设置属性"trainable_weights",可能是因为它与现有的只读冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆