给定一组提取,我如何检索将在一次调用中执行的提取的(可能是非唯一的)顺序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}
Run Code Online (Sandbox Code Playgroud)
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'}]
Run Code Online (Sandbox Code Playgroud)
随后,我们可以手动逐步评估图中的每个节点 - 后续调用Session.run()涉及传入feed_dict累积所有先前输入结果的a 。这很慢,因为 C++ 和 numpy 数据之间不断混洗,而且内存密集,因为我们正在缓存所有内容的输出值。
| 归档时间: |
|
| 查看次数: |
1231 次 |
| 最近记录: |