Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: can't set attribute #7736

Closed
jetd1 opened this issue Aug 24, 2017 · 9 comments
Closed

AttributeError: can't set attribute #7736

jetd1 opened this issue Aug 24, 2017 · 9 comments

Comments

@jetd1
Copy link

jetd1 commented Aug 24, 2017

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

@jetd1
Copy link
Author

jetd1 commented Aug 24, 2017

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

@nicolewhite
Copy link
Contributor

This was fixed in 619259c, which came after the 2.0.7 release. You'll need to install the latest Keras from GitHub.

@stale
Copy link

stale bot commented Nov 23, 2017

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.

@stale stale bot added the stale label Nov 23, 2017
@thomas-neva
Copy link

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

@thomas-neva
Copy link

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

@nicolewhite
Copy link
Contributor

nicolewhite commented 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

@thomas-neva
Copy link

merci beaucoup! 🥇

@wt-huang
Copy link

Closing as this is resolved

@abhijeetknayak
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants