【发布时间】:2017-04-11 09:51:28
【问题描述】:
我的问题是,如果一个操作的参数是常量,TF会缓存结果:
a = tf.constant(np.random.randn(*(100, 101)))
b = tf.constant(np.random.randn(*(100, 102)))
c = tf.constant(np.random.randn(*(101, 102)))
# Some expensive operation.
res = tf.einsum('si,sj,ij->s', a, b, c)
%timeit sess.run(res)
最慢的运行时间是最快的运行时间的 577.76 倍。这可能意味着正在缓存中间结果。 10000 次循环,3 次中的最佳:每个循环 137 µs
如果我在每次运行时从头开始生成张量,那么我也会计算张量生成的开销:
a = tf.random_normal((100, 101))
b = tf.random_normal((100, 102))
c = tf.random_normal((101, 102))
res = tf.einsum('si,sj,ij->s', a, b, c)
%timeit sess.run(res)
最慢的运行时间是最快的运行时间的 4.07 倍。这可能意味着正在缓存中间结果。 10 个循环,3 个循环中的最佳值:每个循环 28 毫秒
也许在这个特定的例子中,开销并不大,但对于更便宜的操作来说,它可能很重要。
有什么方法可以冻结参数,这样它们就不会在每个 sess.run() 上重新计算,而是抑制所有其他缓存?
【问题讨论】:
标签: python caching tensorflow profiling timeit