# 3.11 TensorBoard可视化初步

**3.11.1 TensorBoard简介**

![图3-14 TensorBoard](https://80031148-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYtTdBhaX9EsGjMVkHV%2F-L_XfVL7cAlMGek48m0d%2F-L_XftVIpnIF4eT_v5OR%2Fimage.png?alt=media\&token=1a5ff9ee-bf03-4787-8501-5010fe702fdc)

图14即为TensorFlow的可视化工具TensorBoard，它通过TensorFlow程序运行过程中产生的日志文件可视化TensorFlow的程序运行状态，它与TensorFlow跑在两个不同的进程中。

**3.11.2 在TensorBoard中查看图结构**

![图3-15 在TensorBoard中查看图结构](https://80031148-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYtTdBhaX9EsGjMVkHV%2F-L_XfVL7cAlMGek48m0d%2F-L_Xg056o0PVYQSXpaYi%2Fimage.png?alt=media\&token=33c8d1df-86e9-4528-8f35-613f1baae499)

上图代码中的Logdir指定的目录为运行后产生日志文件的目录，如图16所示我们可以打开文件管理器进行查看。

![图 3-16 日志目录](https://80031148-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYtTdBhaX9EsGjMVkHV%2F-Lb2s2C0yx5wc4UQADnR%2F-Lb2tzrvkHdYbtnoVf5V%2F1.png?alt=media\&token=65ae69fe-0d2c-422f-9838-ec7cae8c1898)

**3.11.3** **启动TensorBoard**

TensorBoard不需要额外安装，在TensorFlow安装时已自动完成，在Anaconda Prompt中先进入日志存放的目录（**注：非常重要**），再运行TensorBoard，并将日志的地址指向程序日志输出的地址。

命令：tensorboard --logdir=/path/log （/path/log为产生日志文件的目录）

启动服务的端口默认为6006；使用 --port 参数可以改编启动服务的端口。

TensorBoard是一个在本地启动的服务，启动完成后在浏览器网址：<http://localhost:6006>即可进行访问。

**3.11.4 TensorBoard常用API**

| **API**                              | **描述**                                |
| ------------------------------------ | ------------------------------------- |
| tf.summary.FileWrite()               | 创建FileWriter和事件文件，会在logdir中创建一个新的事件文件 |
| tf.summary.FileWriter.add\_summary() | 将摘要添加到事件文件                            |
| tf.summary.FileWriter.add\_event()   | 向事件文件添加一个事件                           |
| tf.summary.FileWriter.add\_graph()   | 向事件文件添加一个图                            |
| tf.summary.FileWriter.get\_logdir()  | 获取事件文件的路径                             |
| tf.summary.FileWriter.flush()        | 将所有事件都写入磁盘                            |
| tf.summary.FileWriter.close()        | 将事件写入磁盘，并关闭文件操作符                      |
| tf.summary.scalar()                  | 输出包含单个标量值的摘要                          |
| tf.summary.histogram()               | 输出包含直方图的摘要                            |
| tf.summary.audio()                   | 输出包含音频的摘要                             |
| tf.summary.image()                   | 输出包含图片的摘要                             |
| tf.summary.merge()                   | 合并摘要，包含所有输入摘要的值                       |

&#x20;                                              表3-2 TensorBoard常用API

## > 示例代码 <a href="#shi-li-dai-ma" id="shi-li-dai-ma"></a>

{% code title="1.py" %}

```python
import tensorflow as tf

#清除default graph和不断增加的节点
tf.reset_default_graph() 

# logdir改为自己机器上的合适路径
logdir='D:/log'

#定义一个简单的计算图，实现向量加法的操作
input1 = tf.constant([1.0, 2.0, 3.0], name="input1")
input2 = tf.Variable(tf.random_uniform([3]), name="input2")
output = tf.add_n([input1, input2], name="add")

#生成一个写日志的writer，并将当前的TensorFlow计算图写入日志。
writer = tf.summary.FileWriter(logdir,tf.get_default_graph())
writer.close()
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://minghuiwu.gitbook.io/tfbook/di-san-zhang-mo-dao-bu-wu-kan-chai-gong-tensorflow-ji-chu/di-san-zhang-tensorflow-ji-chu/3.11-tensorboard-ke-shi-hua-chu-bu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
