tf中ProfilerHook的使用

1. ProfilerHook

在深度学习训练过程中,经常会遇到性能的问题,为了提高训练过程的效率,各个训练环节的耗时分析和性能优化是必须的。
而如果通过手动打印日志和耗时情况,不仅繁琐,而且准确度不高。那么我们可以通过tf提供的Profiler工具进行相关分析。

tf里的tf.estimator.ProfilerHook就是一个可以定期收集分析信息的接口,通过该接口收集到的信息会被保存到"timeline-${step}.json"文件中,这些文件可以直接通过chrome://tracing/导入可视化,方便具体分析每个流程的耗时情况。

2. 使用方法

def train_and_eval(model):
    """
    :param model: 声明的estimator实例
    :return: None
    :usage: 进行模型训练,并在指定步长的时候进行结果评估
    """
    timeline_hook = tf.train.ProfilerHook(save_steps=100, output_dir=os.path.join(
            os.getcwd(), './timeline_track'
        ))

    hook = tf.contrib.estimator.stop_if_no_increase_hook(
        model,
        metric_name='ctcvr_cvr_auc_esmm',
        max_steps_without_increase=configuration_params['max_steps_without_increase'],
        # maximum number of training steps with no decrease in the given metric.
        min_steps=configuration_params['min_steps'],  # stop is never requested if global step is less than this value
        run_every_steps=configuration_params['run_every_steps'],
        run_every_secs=None
    )

    train_spec = tf.estimator.TrainSpec(
        input_fn=lambda: input_fn(os.path.join(os.getcwd(),
                                               CONFIG_TRAIN['train_data']),
                                  'train', CONFIG_TRAIN['batch_size']),
        hooks=[hook, timeline_hook]
    )

    eval_spec = tf.estimator.EvalSpec(
        input_fn=lambda: input_fn(os.path.join(os.getcwd(),
                                               CONFIG_TRAIN['test_data']),
                                  'eval', 128),
        steps=CONFIG.evalconfig['steps'],
        throttle_secs=30
        )

    tf.estimator.train_and_evaluate(model, train_spec, eval_spec)

2.1 参数说明

参数说明
save_steps间隔多少步保存一次profiler trace
save_secs间隔多少秒保存一次profiler trace,save_steps和save_secs只设置一个
output_dirprofiler trace文件的存储路径

2.2 输出信息

profiler-output

  • timeline-${step}.json:每个报错步长输出的profiler trace文件

3. web分析

  1. 在浏览器中打开chrome://tracing/页面
    tracing-page
  2. 通过load将profiler trace文件导入浏览器
  3. 分析具体耗时情况
    time-cost
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值