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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ubershmekelcommented, Aug 23, 2018

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.

0reactions
GuillaumeMougeotcommented, Sep 20, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tensorflow,assign value to variable - Stack Overflow
The error that is raised is exactly the error you posted: AttributeError: 'Tensor' object has no attribute 'assign'.
Read more >
AttributeError: 'Tensor' object has no attribute 'assign' #2
This is strange that 'tf' has an object 'assign' but that does not seem working ! import tensorflow.compat.v1 as tf tf.disable_eager_execution()
Read more >
Fix Tensor' object has no attribute 'assign' Error in TensorFlow
In this tutorial, we will introduce how to fix Tensor' object has no attribute 'assign' error. You can do by following our steps....
Read more >
'tensor' object has no attribute 'assign' - Google Groups
I am using manual keras layer for attention. In model.fit() i am getting the attribute error as 'tensor' object has no attribute 'assign'....
Read more >
AttributeError: 'Tensor' object has no attribute 'assign'解决办法
AttributeError : 'Tensor' object has no attribute 'assign'a = tf.ones(shape=[1,2])tf.assign(a,-1)会报错:AttributeError: 'Tensor' object has no attribute ...
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