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: 'Tensor' object has no attribute 'assign' #1

Closed
petteriTeikari opened this issue May 19, 2017 · 11 comments
Closed

AttributeError: 'Tensor' object has no attribute 'assign' #1

petteriTeikari opened this issue May 19, 2017 · 11 comments

Comments

@petteriTeikari
Copy link

petteriTeikari commented May 19, 2017

Hi,

Thanks a lot for the repo, but I hit the following error when trying the python test_inference.py with the densenet169:

Traceback (most recent call last):
  File "test_inference.py", line 27, in <module>
    model = DenseNet(reduction=0.5, classes=1000, weights_path=weights_path)
  File "densenet169.py", line 78, in DenseNet
    model.load_weights(weights_path, by_name=True)
  File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2498, in load_weights
    load_weights_from_hdf5_group_by_name(f, self.layers)
  File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2976, in load_weights_from_hdf5_group_by_name
    K.batch_set_value(weight_value_tuples)
  File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2025, in batch_set_value
    assign_op = x.assign(assign_placeholder)
AttributeError: 'Tensor' object has no attribute 'assign'

So when printing the types of the variables in the saved weights I got the following:

<tf.Variable 'conv1/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref>
<tf.Variable 'conv1_bn/gamma:0' shape=(64,) dtype=float32_ref>
<tf.Variable 'conv1_bn/beta:0' shape=(64,) dtype=float32_ref>
<tf.Variable 'conv1_bn/moving_mean:0' shape=(64,) dtype=float32_ref>
<tf.Variable 'conv1_bn/moving_variance:0' shape=(64,) dtype=float32_ref>
Tensor("conv1_scale/Const:0", shape=(64,), dtype=float32)

And the batch_set_value(tuples) from the backend hit the Tensor problem with "conv1_scale", is there a fix for this to allow the assigning or are the weights somewhat weirdly saved?:

            if hasattr(x, '_assign_placeholder'):
                assign_placeholder = x._assign_placeholder
                assign_op = x._assign_op
            else:
                assign_placeholder = tf.placeholder(tf_dtype, shape=value.shape)
                assign_op = x.assign(assign_placeholder)
                x._assign_placeholder = assign_placeholder
                x._assign_op = assign_op
            assign_ops.append(assign_op)
            feed_dict[assign_placeholder] = value
@flyyufelix
Copy link
Owner

What version of tensorflow are you using? We currently only supports 0.12.0. Will add support to the latest 1.0 version soon.

@petteriTeikari
Copy link
Author

Aah, explaining factor then. Yes I switched to the latest 1.xx version already @flyyufelix

@lef-fan
Copy link

lef-fan commented Jun 22, 2017

Hello,
Check flyyufelix/cnn_finetune#2 or my repo here for an update to Keras 2.0.3 and Tensorflow 1.1.0

@flyyufelix
Copy link
Owner

Apologize for the delay @petteriTeikari . I've already made the necessary changes to get the code to work with the latest versions of Keras (2.0.5) and TensorFlow (1.2.1)

@KanuGT
Copy link

KanuGT commented Jul 5, 2017

hey @flyyufelix I am getting the same issue on tensorflow backend
On theano, it is
AttributeError: 'TensorConstant' object has no attribute 'set_value'

my keras version is 2.0.4
tf : 1.2.1
th: 0.9.0

@flyyufelix
Copy link
Owner

Oh yes, I forgot to erase a line in custom_layers.py which caused the error above. It should work now!

@chhayac
Copy link

chhayac commented May 10, 2018

Hey @flyyufelix, I am getting the below error:
AttributeError: 'Tensor' object has no attribute 'assign' for
generator.fit( noise_input, target_ohe, batch_size=batch_size, epochs = pre_generator_rounds, validation_split=0.1 ) line.
I am using tensorflow version '1.4.1'. Could you please help or provide your views for resolving the error.

@ubershmekel
Copy link

@chhayac I had this error when my data was accidentally empty :(

Traceback (most recent call last):
  File "script.py", line 187, in <module>
    callbacks=[])
  File "/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1415, in fit_generator
    initial_epoch=initial_epoch)
  File "/anaconda3/lib/python3.6/site-packages/keras/engine/training_generator.py", line 39, in fit_generator
    model._make_train_function()
  File "/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 498, in _make_train_function
    loss=self.total_loss)
  File "/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/keras/optimizers.py", line 197, in get_updates
    self.updates.append(K.update(m, v))
  File "/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 971, in update
    return tf.assign(x, new_x)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 217, in assign
    return ref.assign(value, name=name)
AttributeError: 'Tensor' object has no attribute 'assign'

@Colmmm
Copy link

Colmmm commented Aug 23, 2018

@ubershmekel did you manage to sort this out? I'm having the same problem : , (

@ubershmekel
Copy link

I didn't solve the bug in Tensorflow. But I did notice it was only occurring in a degenerate scenario. E.g. if the matrix had no rows, no columns, no shape. If I sent in an empty iterator. I didn't debug exactly to figure out what. Just make sure whatever you're feeding into keras or tf isn't empty.

@GuillaumeMougeot
Copy link

@ubershmekel Thanks! I had the same issue and you were right: this error occurs in a degenerate scenario, in my case I controlled the number of filter in a layer through a variable that was null at a moment.

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

8 participants