首页 > 解决方案 > 如何在高级 Estimator api 中使用 tf.print(不是 tf.Print)

问题描述

目前,我正在使用tf.Print在估计器中打印(调试)张量,但是这个 api 被标记为不推荐使用,并建议我使用 tf.print 代替。根据RFC,通过使用 tf.print,我需要控制正在运行的会话,但Estimator旨在对用户隐藏会话和图形。那么,如何tf.print在 Estimator 中使用呢?

标签: tensorflow

解决方案


为了tf.print在图形模式下使用,这就是tf.estimator工作原理,您可以直接tf.print用作 的替代品tf.Print,您只需在tf.print执行 model_fn 中的张量之前强制执行操作,因此,给定输入张量,input_你的model_fn,你可以:

    print_op = tf.print(tensor_to_log)
    with tf.control_dependencies([print_op]):
        first_layer_output = first_inpyt_layer(input_)

甚至

    print_op = tf.print(tensor_to_log)
    with tf.control_dependencies([print_op]):
        input_ = tf.identity(input_)
    # define your model using input_ as usual

推荐阅读