TensorBoard 计算图的查看方式

yizhihongxing

TensorBoard 计算图的查看方式

在 TensorFlow 中,我们可以使用 TensorBoard 查看计算图。本文将详细讲解如何使用 TensorBoard 查看计算图,并提供两个示例说明。

示例1:使用 TensorBoard 查看计算图

在 TensorFlow 中,我们可以使用 tf.summary.FileWriter() 函数将计算图写入 TensorBoard。以下是使用 TensorBoard 查看计算图的示例代码:

import tensorflow as tf

# 定义计算图
x = tf.placeholder(tf.float32, [None, 784], name='x')
W = tf.Variable(tf.zeros([784, 10]), name='W')
b = tf.Variable(tf.zeros([10]), name='b')
y = tf.nn.softmax(tf.matmul(x, W) + b, name='y')

# 将计算图写入 TensorBoard
writer = tf.summary.FileWriter('logs/', tf.get_default_graph())
writer.close()

在这个示例中,我们首先定义了一个简单的计算图,并使用 tf.summary.FileWriter() 函数将计算图写入 TensorBoard。然后,我们关闭了写入器。

接下来,我们可以在命令行中输入以下命令,启动 TensorBoard:

tensorboard --logdir=logs/

然后,我们可以在浏览器中输入以下地址,查看计算图:

http://localhost:6006/

示例2:使用 TensorBoard 查看 Keras 模型计算图

在 Keras 中,我们可以使用 tf.keras.callbacks.TensorBoard() 回调函数将计算图写入 TensorBoard。以下是使用 TensorBoard 查看 Keras 模型计算图的示例代码:

import tensorflow as tf

# 加载数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# 定义模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 定义 TensorBoard 回调函数
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir='logs/')

# 训练模型
model.fit(x_train, y_train, epochs=5, callbacks=[tensorboard_callback])

# 查看计算图
# 在命令行中输入以下命令,启动 TensorBoard:
# tensorboard --logdir=logs/
# 然后,在浏览器中输入以下地址,查看计算图:
# http://localhost:6006/

在这个示例中,我们首先加载了 MNIST 数据集,并定义了一个简单的 Keras 模型。然后,我们使用 tf.keras.callbacks.TensorBoard() 回调函数将计算图写入 TensorBoard。接着,我们训练模型,并在训练模型时,使用 TensorBoard 回调函数。最后,我们可以在命令行中输入以下命令,启动 TensorBoard,并在浏览器中查看计算图。

结语

以上是 TensorBoard 计算图的查看方式的详细攻略,包括使用 tf.summary.FileWriter() 函数将计算图写入 TensorBoard 和使用 tf.keras.callbacks.TensorBoard() 回调函数将 Keras 模型计算图写入 TensorBoard 两种方法,并提供了两个示例。在实际应用中,我们可以根据具体情况来选择合适的方法,以查看计算图。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:TensorBoard 计算图的查看方式 - Python技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • TensorFlow—基础—GFile

      使用TensorFlow的时候经常遇到 tf.gfile.exists()….   关于gfile,一个googler是这样给出的解释: The main roles of the tf.gfile module are: To provide an API that is close to Python’s file objects, and To…

    tensorflow 2023年4月8日
    00
  • 深度学习之TensorFlow安装与初体验

    学习前 搞懂一些关系和概念首先,搞清楚一个关系:深度学习的前身是人工神经网络,深度学习只是人工智能的一种,深层次的神经网络结构就是深度学习的模型,浅层次的神经网络结构是浅度学习的模型。 浅度学习:层数少于3层,使用全连接的一般被认为是浅度神经网络,也就是浅度学习的模型,全连接的可能性过于繁多,如果层数超过三层,计算量呈现指数级增长,计算机无法计算到结果,所以…

    2023年4月5日
    00
  • Tensorflow – tf常用函数使用(持续更新中)

    本人较懒,故间断更新下常用的tf函数以供参考:    reduce_sum( ) 个人理解是降维求和函数,在 tensorflow 里面,计算的都是 tensor,可以通过调整 axis 的维度来控制求和维度。 参数: input_tensor:要减少的张量.应该有数字类型. axis:要减小的尺寸.如果为None(默认),则缩小所有尺寸.必须在范围[-ra…

    tensorflow 2023年4月6日
    00
  • 解决Jupyter notebook[import tensorflow as tf]报错

     参考: https://blog.csdn.net/caicai_zju/article/details/70245099

    tensorflow 2023年4月6日
    00
  • Tensorflow&CNN:验证集预测与模型评价

    https://blog.csdn.net/sc2079/article/details/90480140   本科毕业设计终于告一段落了。特写博客记录做毕业设计(路面裂纹识别)期间的踩过的坑和收获。希望对你有用。   目前有:     1.Tensorflow&CNN:裂纹分类     2.Tensorflow&CNN:验证集预测与模型评价…

    2023年4月8日
    00
  • tensorflow-TFRecord报错ValueError: Protocol message Feature has no “feature” field.

    编写代码用TFRecord数据结构存储数据集信息是报错:ValueError: Protocol message Feature has no “feature” field.或和这个错误非常类似的错误。 请检查 features=tf.train.Features(feature={…} 里面有没有单子写错。如果有一个单词写错都会报和上面类似的错误

    tensorflow 2023年4月8日
    00
  • ubuntu-14.04安装最新tensorflow记录

    1.安装英伟达驱动./NVIDIA-Linux-x86_64-384.69.runnvidia-smi成功表示驱动ok 2.安装cudadpkg -i cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.debapt-get updateapt-get install cuda安装patch2(也可以不装)dp…

    tensorflow 2023年4月8日
    00
  • Tensorflow Probability Distributions 简介

    摘要:Tensorflow Distributions提供了两类抽象:distributions和bijectors。distributions提供了一系列具备快速、数值稳定的采样、对数概率计算以及其他统计特征计算方法的概率分布。bijectors提供了一系列针对distribution的可组合的确定性变换。 1.1 methods 一个distributi…

    2023年4月8日
    00
合作推广
合作推广
分享本页
返回顶部