对于tensorflow,我是新手,我正在学习
https://www.tensorflow.org/get_started/get_started fixW = tf.assign(W, [-1.])
工作正常,但是
fixb = tf.assign(b, [1.]) Traceback (most recent call last): File "", line 1, in 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'
另一个例子
zero_tsr = tf.zeros([1.,2.]) zero_tsr
如果我尝试更改zero_tsr
fixz = tf.assign(zero_tsr, [2.,2.]) Traceback (most recent call last): File "", line 1, in 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'
同样,同样的问题.
我没有改变shell,一切都是一样的.为什么我这里有问题?
在您发布的示例中:
zero_tsr = tf.zeros([1.,2.]) zero_tsr
zero_tsr
是常量而不是变量,因此您无法为其赋值.
从文档:
assign(ref,value,validate_shape = None,use_locking = None,name = None)
ref:一个可变的Tensor.应该来自一个Variable节点.可能是未初始化的.
例如,这将正常工作:
import tensorflow as tf zero_tsr = tf.Variable([0,0]) tf.assign(zero_tsr,[4,5])
而这段代码会引发错误
import tensorflow as tf zero_tsr = tf.zeros([1,2]) tf.assign(zero_tsr,[4,5])
引发的错误正是您发布的错误:
AttributeError:'Tensor'对象没有属性'assign'