Tensorflow 2.0 中的“UnreadVariable”是什么?

Phy*_*ade 5 tensorflow tensorflow2.0

在Tensorflow 2.0中,一些变量可以描述为'UnreadVariable'

例如:

b = tf.Variable([4,5], name="test")
print(b.assign([7, 9]))
# Will print
# <tf.Variable 'UnreadVariable' shape=(2,) dtype=int32, numpy=array([7, 9], dtype=int32)>
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

小智 1

UnreadVariable基本上Represents a future for a read of a variable. Pretends to be the tensor if anyone looks.

这是因为在分配操作期间启用了急切执行。

TensorFlow 的 eager execution 是一种命令式编程环境,可以立即评估操作,而无需构建图:操作返回具体值,而不是构建稍后运行的计算图。

在 Tensorflow 2.0 中,默认启用即时执行。因此,您正在执行的操作无需构建图表即可完成。

无论如何,如果您愿意,可以通过禁用急切执行来更改类型,AssignVariableOp如下所示。

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

b = tf.Variable([4,5], name="test")
print(b.assign([7, 9]))

Returns:  

<tf.Variable 'AssignVariableOp' shape=(2,) dtype=int32>
Run Code Online (Sandbox Code Playgroud)