如何修改LogginTensowrHook formatter?

时间:2018-02-12 22:29:54

标签: tensorflow hook formatter

https://www.tensorflow.org/tutorials/layers中给出cnn的标准实现,在训练结果显示时,张量钩定义如下

# Set up logging for predictions
  tensors_to_log = {"probabilities": "softmax_tensor"}
  logging_hook = tf.train.LoggingTensorHook(
      tensors=tensors_to_log, 
      every_n_iter=50)

以这种方式打印整个张量值。如何修改钩子以指定要打印的内容?

1 个答案:

答案 0 :(得分:0)

您只需要传递要在张量字典中打印的钩子即可。

最好, -托尼

last_step = 1000


def formatter_log(tensors):
   """
   Format the log output
   """

   logstring = "Step {} of {}: " \
               " training Dice loss = {:.4f}," \
               " training Dice = {:.4f}".format(tensors["step"],
               last_step,
               tensors["loss"], tensors["dice"])

   return logstring

hooks = [tf.train.StopAtStepHook(last_step=last_step),
        # Prints the loss and step every log_steps steps
        tf.train.LoggingTensorHook(tensors={"step": global_step,
                                   "loss": loss,
                                   "dice": metric_dice},
                                   every_n_iter=log_steps,
                                   formatter=formatter_log)]