这个问题与低级 Tensorflow 1.x API 有关。给定 a Tensor,Session.run()我不清楚 Tensorflow 如何遍历计算图。
假设我有一些这样的代码:
a = tf.constant(1.0)
b = tf.subtract(a, 1.0)
c = tf.add(b, 2.0)
d = tf.multiply(c,3)
sess = tf.Session()
sess.run(d)
减法、加法和乘法运算并不都存储在 Tensord中,对吧?我知道Tensor对象有graph和op字段;这些字段是否有一些如何递归访问以获得计算所需的所有操作d?
编辑:添加输出
print(tf.get_default_graph().as_graph_def())
node {
name: "Const"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 1.0
}
}
}
}
node {
name: "Sub/y"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 1.0
}
}
}
}
node {
name: "Sub"
op: "Sub"
input: "Const"
input: "Sub/y"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
node {
name: "Add/y"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 2.0
}
}
}
}
node {
name: "Add"
op: "Add"
input: "Sub"
input: "Add/y"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
node {
name: "Mul/y"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 3.0
}
}
}
}
node {
name: "Mul"
op: "Mul"
input: "Add"
input: "Mul/y"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
versions {
producer: 38
}