使用tf.print,我们收到错误:
In [1]: import tensorflow as tf
In [2]: # using print
In [3]: entcoeff = tf.Variable([0], dtype=tf.float32, trainable=False)
...: entcoeff = tf.Print(entcoeff,[entcoeff,"printing"])
In [4]: tf.assign(entcoeff, [-1.])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-dd57efca5923> in <module>()
----> 1 tf.assign(entcoeff, [-1.])
/nohome/jaan/abhishek/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py in assign(ref, value, validate_shape, use_locking, name)
270 ref, value, use_locking=use_locking, name=name,
271 validate_shape=validate_shape)
--> 272 return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
不使用tf.print。它看起来像预期的那样正常
In [5]: # not using print
In [6]: entcoeff = tf.Variable([0], dtype=tf.float32, trainable=False)
In [7]: tf.assign(entcoeff, [-1.])
Out[7]: <tf.Tensor 'Assign:0' shape=(1,) dtype=float32_ref>
tf.Print会将变量转换为常量吗? 试图调试:
In [8]: entcoeff = tf.Print(entcoeff,[entcoeff,"printing"])
In [9]: type(entcoeff)
Out[9]: tensorflow.python.framework.ops.Tensor
In [10]: dir(entcoeff)
Out[10]:
['OVERLOADABLE_OPERATORS',
'__abs__',
'__add__',
'__and__',
'__array_priority__',
'__bool__',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__div__',
'__doc__',
'__eq__',
'__floordiv__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__invert__',
'__iter__',
'__le__',
'__lt__',
'__matmul__',
'__mod__',
'__module__',
'__mul__',
'__ne__',
'__neg__',
'__new__',
'__nonzero__',
'__or__',
'__pow__',
'__radd__',
'__rand__',
'__rdiv__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rfloordiv__',
'__rmatmul__',
'__rmod__',
'__rmul__',
'__ror__',
'__rpow__',
'__rsub__',
'__rtruediv__',
'__rxor__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__truediv__',
'__weakref__',
'__xor__',
'_add_consumer',
'_as_node_def_input',
'_as_tf_output',
'_consumers',
'_dtype',
'_handle_dtype',
'_handle_shape',
'_op',
'_override_operator',
'_shape',
'_shape_as_list',
'_value_index',
'consumers',
'device',
'dtype',
'eval',
'get_shape',
'graph',
'name',
'op',
'set_shape',
'shape',
'value_index']
答案 0 :(得分:1)
问题是tf.Print
会返回张量而不是tf.Variable
。您可以通过将结果分配给一个新变量来解决这个问题,虽然这对我来说有点麻烦,并且可能有更优雅的方式。如果您运行以下内容:
import tensorflow as tf
entcoeff = tf.Variable([0], dtype=tf.float32, trainable=False)
print(entcoeff)
entcoeff = tf.Variable(tf.Print(entcoeff, [entcoeff], message="\n\nprinting"))
print(entcoeff)
entcoeff = tf.assign(entcoeff, [-1.]) # This returns an op which you need to run for the assignment to happen
print(entcoeff)
init_op = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_op) # This triggers the tf.Print
print(sess.run(entcoeff)) # This causes entcoeff to be assigned -1.
你应该得到
<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>
[ -1.]
2017-11-23 00:49:56.450723: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\kernels\logging_ops.cc:79]
printing[0]
请注意,tf.Print
的输出不会显示到结尾,因为tf.Print
通过写入标准错误来工作。另请注意我添加到输出消息中的\n\n
以使其更加明显。