4

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.

I am using LSTM with the attention layer.

class 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?

3 Answers3

9

change

self.trainable_weights = [self.W]

to

self._trainable_weights = [self.W]
Shaik Ahmad
  • 449
  • 1
  • 3
  • 11
0

This is ongoing issue with tf in colab. I could get a link with this here

Looks the issue got closed , maybe time to reopen.

manjunath
  • 81
  • 1
  • 4
0

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)