Keras: AttributeError: can't set attribute

Created on 25 Aug 2017  路  9Comments  路  Source: keras-team/keras

I updated my keras from 2.0.6 to 2.0.7. Then the following code (works fine with 2.0.6) produces strange errors:

>>> import keras as K
Using TensorFlow backend.
>>> model = K.models.Sequential()
>>> l1 = K.layers.Conv2D(1, 1, input_shape=(2, 2, 2))
>>> model.add(l1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jet/anaconda3/lib/python3.6/site-packages/keras/models.py", line 442, in add
    layer(x)
  File "/home/jet/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 575, in __call__
    self.build(input_shapes[0])
  File "/home/jet/anaconda3/lib/python3.6/site-packages/keras/layers/convolutional.py", line 134, in build
    constraint=self.kernel_constraint)
  File "/home/jet/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/home/jet/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 399, in add_weight
    constraint=constraint)
  File "/home/jet/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 323, in variable
    v.constraint = constraint
AttributeError: can't set attribute

The backend is tensorflow 1.3 compiled with CUDA (it works fine with keras-2.0.6).

stale

Most helpful comment

Don't override self.weights; call yours self.attention_weights.

```
class Attention(Layer):
def __init__(self, kwargs):
super(Attention, self).__init__(
kwargs)

def build(self, input_shape):
    self.attention_weights = self.add_weight(name='attention_weights',
        shape=(input_shape[1], input_shape[1]),
        initializer='uniform',
        trainable=True)
    super(Attention, self).build(input_shape)

def call(self, x):
    return multiply([x, K.softmax(K.dot(x, self.attention_weights))])

def compute_output_shape(self, input_shape):
    return input_shape

```

All 9 comments

When I went back to keras-2.0.6, everything works fine. Or when I switch to tensorflow-1.3.0 binaries from pip, everything works fine...

This was fixed in https://github.com/fchollet/keras/commit/619259c1f067e342f3003865c39631f84eafc2a9, which came after the 2.0.7 release. You'll need to install the latest Keras from GitHub.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

I'm having the same problem with keras-2.1.2 using both Tensorflow and Theano backends

Here's the code that generates the error:

#! /usr/bin/env python3
#-*- coding: utf-8
from __future__ import print_function

from keras import backend as K
from keras.engine.topology import Layer
from keras.layers import multiply

class Attention(Layer):
    """
    Probably the simplest attention mechanism. Dots the attention weights
    with the input, does a softmax on the resulting vector, then elementwise
    multiplies the softmax with the input to create the output.
    """

    def __init__(self, **kwargs):
        super(Attention, self).__init__(**kwargs)

    def build(self, input_shape):
        self.weights = self.add_weight(name='attention_weights',
            shape=(input_shape[1], input_shape[1]),
            initializer='uniform',
            trainable=True)
        super(Attention, self).build(input_shape)

    def call(self, x):
        return multiply([x, K.softmax(K.dot(x, self.weights))])

    def compute_output_shape(self, input_shape):
        return input_shape


def test_attention():
    from keras.models import Sequential
    from keras.layers import GRU, Bidirectional

    kmodel = Sequential()
    kmodel.add(GRU(512,input_shape=(200,300)))
    kmodel.add(Attention())

if __name__ == "__main__":
    test_attention()

and here's the error:

Using Theano backend.
Traceback (most recent call last):
  File "attention_layer.py", line 39, in <module>
    test_attention()
  File "attention_layer.py", line 36, in test_attention
    kmodel.add(Attention())
  File "/usr/local/lib/python3.6/site-packages/keras/models.py", line 489, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 576, in __call__
    self.build(input_shapes[0])
  File "attention_layer.py", line 20, in build
    trainable=True)
AttributeError: can't set attribute

Don't override self.weights; call yours self.attention_weights.

```
class Attention(Layer):
def __init__(self, kwargs):
super(Attention, self).__init__(
kwargs)

def build(self, input_shape):
    self.attention_weights = self.add_weight(name='attention_weights',
        shape=(input_shape[1], input_shape[1]),
        initializer='uniform',
        trainable=True)
    super(Attention, self).build(input_shape)

def call(self, x):
    return multiply([x, K.softmax(K.dot(x, self.attention_weights))])

def compute_output_shape(self, input_shape):
    return input_shape

```

merci beaucoup! 馃

Closing as this is resolved

I get the same error from the following code:
model_1 = Sequential([Dense(units=hidden_1_num_units, input_dim=g_input_shape,
activation='relu', kernel_regularizer=L1L2(l1=1e-5, l2=1e-5)),
Dense(units=hidden_2_num_units,
activation='relu', kernel_regularizer=L1L2(l1=1e-5, l2=1e-5)),
Dense(units=g_output_num_units,
activation='sigmoid', kernel_regularizer=L1L2(l1=1e-5, l2=1e-5)),
Reshape(d_input_shape)])
model_2 = Sequential([InputLayer(input_shape=d_input_shape),
Flatten(),
Dense(units=hidden_1_num_units, activation='relu',
kernel_regularizer=L1L2(l1=1e-5, l2=1e-5)),
Dense(units=hidden_2_num_units, activation='relu',
kernel_regularizer=L1L2(l1=1e-5, l2=1e-5)),
Dense(units=d_output_num_units, activation='sigmoid',
kernel_regularizer=L1L2(l1=1e-5, l2=1e-5))])

model_1.summary()
model_2.summary()

ganModel = simple_gan(model_1, model_2, normal_latent_sampling((100,)))
model = AdversarialModel(base_model=ganModel, player_params=[model_1.trainable_weights, model_2.trainable_weights])

Traceback (most recent call last):
File "C:/Users/NAYAKAB/Desktop/Personal/PycharmProjects/KF/gan.py", line 60, in
model = AdversarialModel(base_model=ganModel, player_params=[model_1.trainable_weights, model_2.trainable_weights])
File "C:\Softwares\Python3\lib\site-packages\keras_adversarial\adversarial_model.py", line 44, in __init__
self.layers = []
File "C:\Softwares\Python3\lib\site-packages\keras\engine\network.py", line 316, in __setattr__
super(Network, self).__setattr__(name, value)
AttributeError: can't set attribute

Any Help?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylemcdonald picture kylemcdonald  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

farizrahman4u picture farizrahman4u  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments

LuCeHe picture LuCeHe  路  3Comments