TensorFlow性能剖析
深度学习模型训练,时常会遇到性能问题,为了提高训练速度,得分析哪些环节是耗时比较长的,再进行优化。TensorFlow提供了一个很好的Profiler工具:tf.estimator.ProfilerHook。
tf.estimator.ProfilerHook 可以定期(每隔N个步数或秒数)捕获CPU/GPU/TPU的分析信息。 其将捕获的信息保存名为“ timeline-.json”的文件,这些文件采用Chrome的跟踪格式,Chrome的打开界面为: chrome://tracing/ 。
TensorFlow Profiling使用
TensorFlow Profiling使用比较简单,在对应代码处添加hook钩子后,下载结果就可以进行分析。
- 添加钩子。
estimator = ... # 正常创建estimator# 创建钩子hook = tf.train.ProfilerHook(save_steps=10, output_dir=output_dir, show_memory=True)# 添加钩子train_spec = tf.estimator.TrainSpec(input_fn=read_dataset(...), max_steps=TRAIN_STEPS, hooks=[hook])eval_spec = ... # 定义 tf.estimator.EvalSpec...tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
- 下载结果。在output_dir目录中,下载名为“ timeline-.json