我玩了一点
import tensorflow as tf
x = tf.Variable([1.0, 2.0])
initializer = tf.global_variables_initializer()
session.run(initializer)
x
<tf.Variable 'Variable:0' shape=(2,) dtype=float32_ref>
y = 2 * x
y
<tf.Tensor 'mul:0' shape=(2,) dtype=float32>
z = y + 1
z
<tf.Tensor 'add:0' shape=(2,) dtype=float32>
v = session.run(x)
sess.run(initializer)
v = sess.run(x)
print (v)
[ 1. 2.]
v1 = sess.run(z)
print (v1)
[ 3. 5.]
v = sess.run(x)
Run Code Online (Sandbox Code Playgroud)
我有3个变量x,y,z.是否可以显示使用提示中的一个命令定义的所有变量?如果我尝试Jonas的建议
new = tf.trainable_variables()
print (new)
[<tf.Variable 'Variable:0' shape=(2,) dtype=float32_ref>]
Run Code Online (Sandbox Code Playgroud)
tf.trainable_variables()
打印出图表中的所有可训练变量,在您的情况下,只有x.当你这样做时y = 2 * x
,这实际上是隐式定义一个常量值mul/x
,并将原始变量作为一个Variable/read
如果您运行以下代码:
x = tf.Variable(1)
y = 2 * x
z = y + 1
for v in tf.get_default_graph().as_graph_def().node:
print v.name
Run Code Online (Sandbox Code Playgroud)
您将获得以下输出:
Variable/initial_value
Variable
Variable/Assign
Variable/read
mul/x
mul
add/y
add
Run Code Online (Sandbox Code Playgroud)
这些是图表中的所有节点.您可以使用它来过滤掉所需的所有相关信息.具体到你的情况,我不会打电话y
和z
变量.
请注意,这是从图表而不是会话中获取所有信息.如果您想从特定会话中获取它,您需要获得相关会话并致电sess.graph
.
作为最后的说明,上述的例子中使用v.name
,但每个图形节点实际上具有多个属性,例如name
,op
,input
,device
,attr
.有关更多信息,请参阅API.