Blog的Github地址:github.com/liuyan731/b…python
最近上線了一個圖片分類的機器學習Python服務,可是在線上的性能並非特別好,就開始進行痛苦的性能分析。今天分享一個簡單可是很是實用的Tensorflow性能調優工具Timeline。git
Tensorflow的Timeline模塊是用於描述張量圖一個工具,能夠記錄在會話中每一個操做執行時間和資源分配及消耗的狀況。github
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)
複製代碼
打開Google Chrome,轉到該頁面 chrome://tracing並加載該timeline.json文件。在該頁面上能夠每一個操做的耗時,以及op的詳細信息。json
2018/4/22 done機器學習
此文章也同步至我的Github博客工具