tensorbaord可视化原理分析


这篇链接讲了TensorFlow的计算图(graph)的可视化,很简单一行代码搞定。当然要指定图的来源,

否则只会写入graph数据在events里面,在tensorboard里面看不到的。

tf.summary.FileWriter.它会返回一个FileWriter类。那返回那个FileWriter类还有何用????

============我们还想看看其他图形可视化,就要用返回那个FileWriter类========================

FileWriter类有很多方法。它可以帮我们生成更加复杂多元的图,比如loss,accurate图等,调用下面那个函数。

writer.add_summary

add_summary(
    summary,
    global_step=None
)

Adds a Summary protocol buffer to the event file.

This method wraps the provided summary in an Event protocol bufferand adds it to the event file.

You can pass the result of evaluating any summary op, usingtf.Session.run ortf.Tensor.eval, to thisfunction. Alternatively, you can pass a tf.Summary protocolbuffer that you populate with your own data. The latter iscommonly done to report evaluation results in event files.

Args:

  • summary: A Summary protocol buffer, optionally serialized as a string.
  • global_step: Number. Optional global step value to record with the summary.

它输入参数是各种图表的protocol 二进制数据,然后这些数据就会被写入到events文件里面。然后在IE里面打开tensorboard

就可以看到那些各种图表。

================以上是说调用FileWriter.add_summary函数就可以看到其他数据图====

但是,FileWriter.add_summary函数里面的summary参数怎么获得呢?看图下图


调用上面的任一函数都可以返回一个protocol 二进制数据,都可以作为add_summary的输入参数。

举个例子,看看为什么可以呢?

tf.summary.scalar

tf.summary.scalar(
    name,
    tensor,
    collections=None,
    family=None
)

Outputs a Summary protocol buffer containing a single scalar value.

The generated Summary has a Tensor.proto containing the input Tensor.

Args:

  • name: A name for the generated node. Will also serve as the series name in TensorBoard.
  • tensor: A real numeric Tensor containing a single value.
  • collections: Optional list of graph collections keys. The new summary op is added to these collections. Defaults to [GraphKeys.SUMMARIES].
  • family: Optional; if provided, used as the prefix of the summary tag name, which controls the tab name used for display on Tensorboard.

Returns:

A scalar Tensor of type string. Which contains a Summary protobuf.

Raises:

  • ValueError: If tensor has the wrong shape or type.

详情看这篇博客代码大笑
https://blog.csdn.net/fu6543210/article/details/80219223

==================总结=====================================

1.如果是只想得到一个简单的net网络图可视化,只需要调用tf.summary.FileWriter即可。同时它也返回一个writer类。

2.如果想得到loss等图表,就得在第一步的基础上,再调用tf.summary.scalar函数返回一个tensor_scalar,然后把tensor_scalar作为tf.summary.merge(tensor_scalar)的输入参数,tf.summary.merge(tensor_scalar)返回一个tensor_merge,最后调用writer.add_summary(tensor_merge,global_step),以上方法比较繁琐,通常用下面的方法较多。

3.还有一种简洁的方法得到loss图表:

调用tf.summary.FileWriter,返回一个writer类。

调用tf.summary.scalar函数,会生成张量数据,但我们不需要保存返回张量

调用tf.summary.merge_all(),会自动汇总上面的张量数据,返回一个tensor_merge,

最后调用writer.add_summary(tensor_merge,global_step)

猜你喜欢

转载自blog.csdn.net/fu6543210/article/details/80227731