在 TensorFlow 中显示图形图像?

Display image of graph in TensorFlow?

我写了一个简单的脚本来计算 1,2,5 的黄金比例。有没有办法通过实际图形结构的tensorflow(可能借助matplotlibnetworkx)实际产生视觉效果? tensorflow 的文档与因子图非常相似,所以我想知道:

如何通过tensorflow生成图结构的图像?

在下面的这个例子中,它将 C_1, C_2, C_3 作为单独的节点,然后 C_1 将有 tf.sqrt 操作,然后是将它们组合在一起的操作。也许图结构(节点,边)可以导入到networkx?我看到 tensor 对象有一个 graph 属性,但我还没有找到如何将它实际用于成像目的。

#!/usr/bin/python

import tensorflow as tf
C_1 = tf.constant(5.0)
C_2 = tf.constant(1.0)
C_3 = tf.constant(2.0)

golden_ratio = (tf.sqrt(C_1) + C_2)/C_3

sess = tf.Session()
print sess.run(golden_ratio) #1.61803
sess.close()

您可以使用 Tensorboard. You need to edit your code to output the graph, and then you can launch tensorboard and see it. See, in particular, TensorBoard: Graph Visualization 获取图表的图像。您创建一个 SummaryWriter 并在其中包含 sess.graph_def。 graph def会输出到log目录。

这正是创建 tensorboard 的目的。您需要稍微修改代码以存储有关图形的信息。

import tensorflow as tf
C_1 = tf.constant(5.0)
C_2 = tf.constant(1.0)
C_3 = tf.constant(2.0)

golden_ratio = (tf.sqrt(C_1) + C_2)/C_3

with tf.Session() as sess:
    writer = tf.summary.FileWriter('logs', sess.graph)
    print sess.run(golden_ratio)
    writer.close()

这将在您的工作目录中创建一个包含事件文件的 logs 文件夹。在此之后,您应该从命令行 tensorboard --logdir="logs" 运行 tensorboard 并导航到它为您提供的 url (http://127.0.0.1:6006)。在您的浏览器中转到 GRAPHS 选项卡并欣赏您的图表。

如果你要用TF做任何事情,你会经常用到TB。因此,从 official tutorials and from this video.

中了解更多信息是有意义的