I'm facing an issue and I need your help. I'm trying to benchmark execution time between eager and graph modes. I'm using a very simple model (only one dense layer).
Here is the graph version:
import tensorflow as tf
import numpy as np
import timeit
graph = tf.Graph()
with graph.as_default():
x = tf.placeholder(tf.float32, [10, 200, 200, 3])
f = tf.keras.layers.Flatten()(x)
c = tf.keras.layers.Dense(512, activation='relu')(f)
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
val = np.ones((10, 200, 200, 3)) * 3.5
# warmup
sess.run(c, {x: val})
print(timeit.timeit(lambda: sess.run(c, {x: val}), number=1000)/1000)
Here is the eager version
import tensorflow as tf
import numpy as np
import timeit
tf.enable_eager_execution()
flatten_layer = tf.keras.layers.Flatten()
dense_layer = tf.keras.layers.Dense(512, activation='relu')
def c(x):
f = flatten_layer(x)
return dense_layer(f)
val = tf.math.scalar_mul(3.5, tf.ones((10, 200, 200, 3)))
# warmup
c(val)
print(timeit.timeit(lambda: c(val), number=1000)/1000)
I'm using tf 1.14 and the issue i'm facing is (if it is one) that the graph version is slower than the eager version. Here are the output of the graph and eager versions (respectively):
0.003849512729793787
0.0008663320459891111
I was expected that the result would be inverted, so I really don't understand why the graph version is slower than the eager one. If I use the tf.function decorator in the eager version, the difference is even greater. Has someone an explanation ?
Thank you