In tensorflow, if we want to run multiple operations, are there any differences (eg in speed or results) if we pass the ops in a list to a single sess.run()
, versus we use multiple sess.run()
but only pass a single ops to each one?
Example:
#!/usr/env/bin python
import tensorflow as tf
if __name__ == '__main__':
v = tf.Variable(0)
c = tf.constant(3)
add = tf.add(v, c)
update = tf.assign(v, add)
mul = tf.multiply(add, update)
init = tf.compat.v1.global_variables_initializer()
# method 1
with tf.Session() as sess:
sess.run(init)
print(sess.run(add))
print(sess.run(mul))
# method 2
with tf.Session() as sess:
print(sess.run([init, add, mul]))
Edit: Initially I was also wondering if there are cases where multiple Session will cause different results, eg dependencies in computational graph causes execution order to be different etc. But I didn't make this clear in my question and only gives a rather dummy example instead...
Each time you run the init
, all the weights are initialised randomly, So no comparison can be made this way. If you really want to compare, I would run the following code:
#!/usr/env/bin python
import tensorflow as tf
if __name__ == '__main__':
v = tf.Variable(0)
c = tf.constant(3)
add = tf.add(v, c)
update = tf.assign(v, add)
mul = tf.multiply(add, update)
init = tf.compat.v1.global_variables_initializer()
# method 1
with tf.Session() as sess:
sess.run(init)
print(sess.run(add))
print(sess.run(mul))
print(sess.run([add, mul]))
There is no difference whatsoever in results:
import numpy as np
tf.reset_default_graph()
v = tf.Variable(0)
c = tf.constant(3)
add = tf.add(v, c)
update = tf.assign(v, add)
mul = tf.multiply(add, update)
init = tf.compat.v1.global_variables_initializer()
def single():
with tf.Session() as sess:
sess.run(init)
fetch_add = sess.run(add)
fetch_mul = sess.run(mul)
return fetch_add, fetch_mul
def multiple():
with tf.Session() as sess:
sess.run(init)
fetch_add, fetch_mul = sess.run([add, mul])
return fetch_add, fetch_mul
add_single, multiply_single = single()
add_multiple, multiply_multiple = multiple()
np.testing.assert_array_equal(add_single, add_multiple)
np.testing.assert_array_equal(multiply_single, multiply_multiple)
When it comes to speed, here are some metrics:
%timeit single()
# 7.44 ms ± 143 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit multiple()
# 5.56 ms ± 121 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
So it is definitely faster if you run multiple nodes together.
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.