Tensorflow, assign value to variable
I am new to tensorflow, I am learning
https://www.tensorflow.org/get_started/get_started
fixW = tf.assign(W, [-1.])
works fine but
fixb = tf.assign(b, [1.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
One more example
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
If I try to change zero_tsr
fixz = tf.assign(zero_tsr, [2.,2.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
Again, the same problem.
I haven't changed the shell, everything is the same. Why am I having a problem?
+3
source to share
1 answer
In the above example:
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
zero_tsr
is a constant, not a variable, so you cannot assign a value to it.
From the documentation :
assignee (out, cost, validate_shape = no, use_locking = no, name = None)
ref: modified tensor. Should be from the node variable. May be uninitialized.
For example, this will work fine:
import tensorflow as tf
zero_tsr = tf.Variable([0,0])
tf.assign(zero_tsr,[4,5])
while this code will throw an error
import tensorflow as tf
zero_tsr = tf.zeros([1,2])
tf.assign(zero_tsr,[4,5])
The error that comes up is exactly what you specified:
AttributeError: "Tensor" object has no attribute "assign"
+3
source to share