Tensorflow Timeline介紹及簡單使用

Blog的Github地址:github.com/liuyan731/b…python


最近上線了一個圖片分類的機器學習Python服務,可是在線上的性能並非特別好,就開始進行痛苦的性能分析。今天分享一個簡單可是很是實用的Tensorflow性能調優工具Timeline。git

簡介

Tensorflow的Timeline模塊是用於描述張量圖一個工具,能夠記錄在會話中每一個操做執行時間和資源分配及消耗的狀況。github

使用方法

執行代碼sess.run()加入參數options和run_metadata

sess.run() 加入 option和run_metadata參數,而後建立timeline對象,並寫入到timeline.json文件中chrome

import tensorflow as tf
from tensorflow.python.client import timeline

run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()

predictions = use_sess.run(use_out, {'DecodeJpeg/contents:0': image_file.file.getvalue()}, options=run_options, run_metadata=run_metadata)

# Create the Timeline object, and write it to a json
tl = timeline.Timeline(run_metadata.step_stats)
ctf = tl.generate_chrome_trace_format()
with open('timeline.json', 'w') as f:
    f.write(ctf)

複製代碼

查看timeline對象

打開Google Chrome,轉到該頁面 chrome://tracing並加載該timeline.json文件。在該頁面上能夠每一個操做的耗時,以及op的詳細信息。json

timeline-tool
chrome 加載timeline.json文件後展現的運行圖


2018/4/22 done機器學習

此文章也同步至我的Github博客工具

相關文章
相關標籤/搜索