Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is tensorflow lazy?

Tags:

tensorflow

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 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.

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

like image 570
yalis Avatar asked Dec 21 '15 18:12

yalis


People also ask

Is eager mode slower?

Eager execution is slower than graph execution! Since eager execution runs all operations one-by-one in Python, it cannot take advantage of potential acceleration opportunities .

Is PyTorch eager execution?

By default, PyTorch uses eager mode computation. You can run a neural net as you build it, line by line, which makes it easier to debug. It also makes it possible to construct neural nets with conditional execution. This dynamic execution is more intuitive for most Python programmers.

Is eager execution default in TensorFlow?

Eager execution is enabled by default and this API returns True in most of cases. However, this API might return False in the following use cases.

What is TF in TensorFlow?

class Tensor : A tf. Tensor represents a multidimensional array of elements. class TensorArray : Class wrapping dynamic-sized, per-time-step, write-once Tensor arrays.


1 Answers

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

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.

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.

like image 61
mrry Avatar answered Sep 27 '22 15:09

mrry