tensorflow: 1.指定变量申请的设备 2.使用timeline

Tensorflow 指定变量申请的设备

import tensorflow as tf

with tf.device('/CPU:0'):
    a = tf.constant(3, dtype=tf.int8)
with tf.device('/GPU:0'):
    b = tf.constant(5, dtype=tf.int8)
with tf.device('/cpu:0'):
    out = tf.multiply(a, b)
#配置Session config
config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
config.gpu_options.allow_growth = True
config = tf.ConfigProto(log_device_placement=True)

#配置timeline option
#配置运行时需要记录的信息
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
#运行时记录运行信息的proto
run_metadata = tf.RunMetadata()

with tf.Session(config=config) as sess:
    #将配置信息和记录运行信息的proto传入运行的过程,从而记录运行时每一个节点的时间、空间开销信息
    sess.run(out, options=run_options, run_metadata=run_metadata)


    # 创建timeline对象,将其写入json文件
    t1 = timeline.Timeline(run_metadata.step_stats)
    ctf = t1.generate_chrome_trace_format()
    with open('timeline.json', 'w') as f:
        f.write(ctf)
        print('ok')
'''
配置好config,输出每个变量和op的位置

/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1

2018-05-29 16:43:02.028559: I tensorflow/core/common_runtime/placer.cc:874] Mul: (Mul)/job:localhost/replica:0/task:0/device:CPU:0
2018-05-29 16:43:02.028574: I tensorflow/core/common_runtime/placer.cc:874] Const_1: (Const)/job:localhost/replica:0/task:0/device:GPU:0
2018-05-29 16:43:02.028582: I tensorflow/core/common_runtime/placer.cc:874] Const: (Const)/job:localhost/replica:0/task:0/device:CPU:0
'''

'''
配置好timeline 的option,
会生成一个timeline.json文件,然后,打开Goog​​le Chrome,转到该页面chrome://tracing并加载该timeline.json文件
如果出现无法打开usr/local/cuda/lib64/libcupti.so.8.0 的问题,将/usr/local/cuda-8.0/extras/CUPTI/lib64 加入LD_LIBRARY_PATH环境变量

猜你喜欢

转载自blog.csdn.net/ilikede/article/details/80498923
今日推荐