See the code snippet:
import tensorflow as tf
x = tf.Variable(1)
op = tf.assign(x, x + 1)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run([x, op]))
There are two possible results:
They depend on the order of evaluation, for the first case, x
is evaluated before op
, and for the second case, x
is evaluated after op
.
I have run the code many times, but the result is always x=2 and op=2
. So I guess that tensorflow
can guarantee x
is evaluated after op
. Is it right? And how does tensorflow
guarantee the dependence?
For the case above, the result is determinate. But in the follow case, the result is not determinate.
import tensorflow as tf
x = tf.Variable(1)
op = tf.assign(x, x + 1)
x = x + 0 # add this line
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(5):
print(sess.run([x, op]))
In the first code, x
is Variable
and op
depends on x
, so x
is always evaluated after op
. But in the second case, x
becomes Tensor
, and op
depend on Variable x
(After x = x + 0
, x is overrided). So the op
doesn't depend on Tensor x
.
TensorFlow Session is a session object which encapsulates the environment in which Operation objects are executed, and data objects are evaluated. TensorFlow requires a session to execute an operation and retrieve its calculated value. A session may own several resources, for example, tf. QueueBase, tf.
Each operation you will do with TensorFlow involves the manipulation of a tensor. There are four main tensor type you can create: tf.
Method #1: Creating tensor using the constant() function. The most popular function for creating tensors in Tensorflow is the constant() function. We need to give values or list of values as argument for creating tensor. If the values given are of type integer, then int32 is the default data type.
The order in which tensors are evaluated is undefined. See the API docs (towards, the very bottom, in the "Returns" info on Session.run()
). As such, you should not rely on them being executed in a particular order. If you need to guarantee an order you should probably use separate run
calls for the different tensors/ops.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With