张量流是懒惰的吗? [英] Is tensorflow lazy?

查看:34
本文介绍了张量流是懒惰的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一段这样的代码

Let's say you have some piece of code like this

import tensorflow as tf
...
f = h*y + z*t  #Just some expression involving other tensors.
e = ... # some expression that does not involve f. 
result = tf.select(b, e, f)

sess.run(result)

b 是与 e 和 f 形状相同的布尔张量.如果 b 的所有元素评估为真,我们不需要f 并且结果将是(或等于)e.

b is a boolean tensor of the same shape as e and f. If all the elements of b evaluate to true, we don't need f and the result will just be (or be equal to) e.

问题:当 session 以 result 运行时,e 的元素都为真,f 是否求值?

The question: when the session is run with result, and the elements of e are all true, is f evaluated?

推荐答案

TL;DR: TensorFlow 是严格的,所以 ef 将在 tf.select() 节点执行之前进行评估.

TL;DR: TensorFlow is strict, so both e and f will be evaluated before the tf.select() node executes.

这引起了一些混乱.TensorFlow 首先根据静态需要哪些操作来生成获取的值(即 sess.run() 的参数)修剪数据流图.然而,一旦图被修剪,运行时就会使用严格执行,即在该操作可以执行之前,必须已经计算了操作的所有输入(例如 tf.select()).

This has caused some confusion. TensorFlow first prunes the dataflow graph based on which operations are statically required to produce the values that are fetched (i.e. the arguments to sess.run()). Once the graph has been pruned, however, the runtime uses strict execution, whereby all of the inputs to an operation (such as tf.select()) must have been computed before that operation can execute.

tf.control_flow_ops 中有对条件执行的实验性支持 模块,使用.control_flow_ops.cond() 函数,但目前很少记录.

There is experimental support for conditional execution in the tf.control_flow_ops module, using the tf.control_flow_ops.cond() function, but this is sparsely documented at present.

这篇关于张量流是懒惰的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆