为什么tensorflow的计算时间显着缩短

时间:2018-06-25 16:20:08

标签: tensorflow profiler

我发现使用tf.RunOptions.FULL_TRACE在Tensorboard中报告的“计算时间”明显小于使用time.time()记录的时间。有什么想法吗?

我尝试按tf.get_default_graph().get_operations()包括所有运算符。但是,张量板上的时间成本为数ms,而time.time()的数量级为0.1 seconds

下面提供了完整的脚本。

import os, math, time
import tensorflow as tf

def mat(theta):
    theta = tf.constant(theta, dtype='float32')

    mat = tf.stack([(tf.cos(theta), 0.0),
                    (0, tf.cos(theta))],
                   axis=0)
    return mat


def build_loop(dimension = 11, repeat = 2**10,):
    dev_name = '/cpu:0'

    with tf.device(dev_name):
        with tf.name_scope('loop'):
            i = tf.constant(1.0, shape=[2] * dimension)
            b = mat(theta=2 * math.pi / repeat)
            for _ in range(repeat):
                i = tf.tensordot(b,i, axes=(1,0))
        with tf.name_scope('initialisation'):
            ini_matmul = tf.tensordot(b,i, axes=(1,0))

    return ini_matmul, i

if __name__ == '__main__':

    repeat = 2**8
    dimension = 4
    logdir = os.path.expanduser('~/tmp/stackexchange__/')
    tag_name = 'why?'
    with tf.get_default_graph().as_default():
        ini_matmul, loop, = build_loop(dimension=dimension, repeat=repeat)

    summary_writer = tf.summary.FileWriter(logdir=logdir)
    summary_writer.add_graph(tf.get_default_graph())
    run_metadata = tf.RunMetadata()
    runoptions = {
        'options': tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE),
        'run_metadata' : run_metadata,
    }
    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
        sess.run(fetches=ini_matmul)
        t0 = time.time()
        sess.run(fetches=tf.get_default_graph().get_operations(), **runoptions)
        t1 = time.time()
        print('Time cost: %s (sec)' % str(t1-t0))
        summary_writer.add_run_metadata(run_metadata, tag=tag_name, global_step=None)
        summary_writer.flush()
    print('Logs written to %s.' % summary_writer.get_logdir())

0 个答案:

没有答案