Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Tensor' object has no attribute 'assign_add'

I encountered error 'Tensor' object has no attribute 'assign_add' when I try to use the assign_add or assign_sub function. The code is shown below:

I defined two tensor t1 and t2, with the same shape, and same data type.

>>> t1 = tf.Variable(tf.ones([2,3,4],tf.int32))
>>> t2 = tf.Variable(tf.zeros([2,3,4],tf.int32))
>>> t1
<tf.Variable 'Variable_4:0' shape=(2, 3, 4) dtype=int32_ref>
>>> t2
<tf.Variable 'Variable_5:0' shape=(2, 3, 4) dtype=int32_ref>

then I use the assign_add on t1 and t2 to create t3

>>> t3 = tf.assign_add(t1,t2)
>>> t3
<tf.Tensor 'AssignAdd_4:0' shape=(2, 3, 4) dtype=int32_ref>

then I try to create a new tensor t4 using t1[1] and t2[1], which are tensors with same shape and same data type.

>>> t1[1]   
<tf.Tensor 'strided_slice_23:0' shape=(3, 4) dtype=int32>
>>> t2[1]
<tf.Tensor 'strided_slice_24:0' shape=(3, 4) dtype=int32>
>>> t4 = tf.assign_add(t1[1],t2[1])

but got error,

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/admin/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 245, in assign_add
return ref.assign_add(value)
AttributeError: 'Tensor' object has no attribute 'assign_add'

same error when using assign_sub

>>> t4 = tf.assign_sub(t1[1],t2[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/admin/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 217, in assign_sub
return ref.assign_sub(value)
AttributeError: 'Tensor' object has no attribute 'assign_sub'

Any idea where is wrong? Thanks.

like image 713
X. L Avatar asked May 03 '18 18:05

X. L


Video Answer


1 Answers

The error is because t1 is a tf.Variable object , while t1[1] is a tf.Tensor.(you can see this in the outputs to your print statements.).Ditto for t2 and t[[2]]

As it happens, tf.Tensor can't be mutated(it's read only) whereas tf.Variable can be(read as well as write) see here. Since tf.scatter_add,does an inplace addtion, it doesn't work with t1[1] and t2[1] as inputs, while there's no such problem with t1 and t2 as inputs.

like image 123
Kanchan Kumar Avatar answered Sep 20 '22 04:09

Kanchan Kumar