【发布时间】:2017-03-14 10:48:42
【问题描述】:
我探索了变量是如何在图中表示的。我创建一个变量,初始化 它并在每次操作后制作图形快照:
import tensorflow as tf
def dump_graph(g, filename):
with open(filename, 'w') as f:
print(g.as_graph_def(), file=f)
g = tf.get_default_graph()
var = tf.Variable(2)
dump_graph(g, 'data/after_var_creation.graph')
init = tf.global_variables_initializer()
dump_graph(g, 'data/after_initializer_creation.graph')
with tf.Session() as sess:
sess.run(init)
dump_graph(g, 'data/after_initializer_run.graph')
变量创建后的图表如下所示
node {
name: "Variable/initial_value"
op: "Const"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 2
}
}
}
}
node {
name: "Variable"
op: "VariableV2"
attr {
key: "container"
value {
s: ""
}
}
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "shape"
value {
shape {
}
}
}
attr {
key: "shared_name"
value {
s: ""
}
}
}
node {
name: "Variable/Assign"
op: "Assign"
input: "Variable"
input: "Variable/initial_value"
attr {
key: "T"
value {
type: DT_INT32
}
}
attr {
key: "_class"
value {
list {
s: "loc:@Variable"
}
}
}
attr {
key: "use_locking"
value {
b: true
}
}
attr {
key: "validate_shape"
value {
b: true
}
}
}
node {
name: "Variable/read"
op: "Identity"
input: "Variable"
attr {
key: "T"
value {
type: DT_INT32
}
}
attr {
key: "_class"
value {
list {
s: "loc:@Variable"
}
}
}
}
versions {
producer: 21
}
有几个节点:Variable/initial_value、Variable、
Variable/Assign, Variable/read.
运行init操作后,又添加了一个节点:
node {
name: "init"
op: "NoOp"
input: "^Variable/Assign"
}
我不知道这里发生了什么。
- 谁能解释一下这些节点的确切含义是什么?
- tensorflow Python中隐式变量初始化的目的是什么 应用程序接口?为什么我们不能在变量对象之后自动初始化一个变量 在 Session.run() 中创建或初始化未初始化的变量?
-
Variable/read节点内的“loc:@”语法是什么意思和^Variable/Assign在init节点内? - 如何检索变量值?我想价值是
存储在会话中,并且
session.run()替换某处 这个值,但不知道血淋淋的细节。
【问题讨论】:
标签: tensorflow