简体   繁体   中英

Does TensorFlow execute entire computation graph with sess.run()?

For example, when we compute a variable c as result = sess.run(c) , does TF only compute the inputs required for computing c or updates all the variables of the complete computational graph?

Also, I don't seem to be able to do this: c = c*a*b as I am stuck with uninitialized variable error even after initializing c as tf.Variable(tf.constant(1)) . Any suggestions?

Since Python code of TF only setups the graph, which is actually executed by native implementation of all ops , your variables need to be executed in this underlying environment. This happens by executing two ops - for local and global variables initialization:

session.run(tf.global_variables_initializer(), tf.local_variables_initializer())

On the original question - as far as I know - YES, it computes all the graph, and it requires you to feed placeholders, even if the executed op (in the session) is not dependent on them.

This question is fairly old but I'll answer it just in case anyone comes across this again.

To answer the first question, no. Tensorflow only executes what is necessary to execute the operation you passed it. From tensorflow.org:

tf.Session.run requires you to specify a list of fetches, which determine the return values, and may be a tf.Operation, a tf.Tensor, or a tensor-like type such as tf.Variable. These fetches determine what subgraph of the overall tf.Graph must be executed to produce the result

To answer the second part of the question, variables need to be initialized before you can use them (see link below). the easiest way to do this is to run tensorflow.global_variables_initializer()

https://www.tensorflow.org/guide/variables#initializing_variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM