如何检索 fetch 的执行顺序?
How to retrieve the execution ordering of fetches?
给定一个提取数组,我如何检索将在对 session.run(fetches)
的单个调用中执行的提取顺序(可能不是唯一的)?
一个合理的解决方案是重新计算python中的拓扑排序。看起来 C++ 实现没有在 python API 中公开。如果在某些情况下这不起作用,请告诉我。
这是一个例子:
import tensorflow as tf
from toposort import toposort
sess = tf.InteractiveSession()
matrix1=tf.constant([[3., 3.]])
matrix2=tf.constant([[2.], [2.]])
sum = tf.add(matrix1, matrix2)
product = tf.matmul(matrix1, matrix2)
final = tf.mul(sum, product)
g = sess.graph
deps = {}
for op in g.get_operations():
# op node
op_inputs = set()
op_inputs.update([t.name for t in op.inputs])
deps[op.name] = op_inputs
# tensor output node
for t in op.outputs:
deps[t.name]={op.name}
deps
{u'Add': {u'Const:0', u'Const_1:0'},
u'Add:0': {u'Add'},
u'Const': set(),
u'Const:0': {u'Const'},
u'Const_1': set(),
u'Const_1:0': {u'Const_1'},
u'MatMul': {u'Const:0', u'Const_1:0'},
u'MatMul:0': {u'MatMul'},
u'Mul': {u'Add:0', u'MatMul:0'},
u'Mul:0': {u'Mul'}}
list(toposort(deps))
[{u'Const', u'Const_1'},
{u'Const:0', u'Const_1:0'},
{u'Add', u'MatMul'},
{u'Add:0', u'MatMul:0'},
{u'Mul'},
{u'Mul:0'}]
随后,我们可以手动逐步评估图中的每个节点 - 对 Session.run()
的后续调用涉及传递一个 feed_dict
,该 feed_dict
累积所有先前输入的结果。这非常慢,因为 C++ 和 numpy 数据之间的不断混洗,而且内存密集型,因为我们正在缓存所有内容的输出值。
给定一个提取数组,我如何检索将在对 session.run(fetches)
的单个调用中执行的提取顺序(可能不是唯一的)?
一个合理的解决方案是重新计算python中的拓扑排序。看起来 C++ 实现没有在 python API 中公开。如果在某些情况下这不起作用,请告诉我。
这是一个例子:
import tensorflow as tf
from toposort import toposort
sess = tf.InteractiveSession()
matrix1=tf.constant([[3., 3.]])
matrix2=tf.constant([[2.], [2.]])
sum = tf.add(matrix1, matrix2)
product = tf.matmul(matrix1, matrix2)
final = tf.mul(sum, product)
g = sess.graph
deps = {}
for op in g.get_operations():
# op node
op_inputs = set()
op_inputs.update([t.name for t in op.inputs])
deps[op.name] = op_inputs
# tensor output node
for t in op.outputs:
deps[t.name]={op.name}
deps
{u'Add': {u'Const:0', u'Const_1:0'},
u'Add:0': {u'Add'},
u'Const': set(),
u'Const:0': {u'Const'},
u'Const_1': set(),
u'Const_1:0': {u'Const_1'},
u'MatMul': {u'Const:0', u'Const_1:0'},
u'MatMul:0': {u'MatMul'},
u'Mul': {u'Add:0', u'MatMul:0'},
u'Mul:0': {u'Mul'}}
list(toposort(deps))
[{u'Const', u'Const_1'},
{u'Const:0', u'Const_1:0'},
{u'Add', u'MatMul'},
{u'Add:0', u'MatMul:0'},
{u'Mul'},
{u'Mul:0'}]
随后,我们可以手动逐步评估图中的每个节点 - 对 Session.run()
的后续调用涉及传递一个 feed_dict
,该 feed_dict
累积所有先前输入的结果。这非常慢,因为 C++ 和 numpy 数据之间的不断混洗,而且内存密集型,因为我们正在缓存所有内容的输出值。