question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: can't set attribute

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
nicolewhitecommented, Jan 4, 2018

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
3reactions
abhijeetknayakcommented, Jun 26, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - AttributeError: can't set attribute - Stack Overflow
This specific exception "AttributeError: can't set attribute" is raised (see source) when the attribute you're attempting to change is ...
Read more >
[Solved] AttributeError: can't set attribute in python - Finxter
The easiest way to fix the AttributeError:can't set attribute is to create a new namedtuple object with the namedtuple._replace() method.
Read more >
Python error saying \"AttributeError: can\'t set attribute\". How is ...
The explanation you are getting this error is that you are naming the setter method mistakenly. You have named the setter method as...
Read more >
Python error saying "AttributeError: can't set attribute" - Intellipaat
The reason you are getting this error is because you are naming the setter method incorrectly. You have named the setter method as...
Read more >
AttributeError: can't set attribute in example/basic.py #256
Code copied verbatim from example/basic.py; python version is 3.9.6; oddly I don't see the error in my actual work, but I needed to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found