Python 在 TensorFlow 中显示图形的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34230613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:39:28  来源:igfitidea点击:

Display image of graph in TensorFlow?

pythonimagegraphstructuretensorflow

提问by O.rka

I wrote a simple script to calculate the golden ratio from 1,2,5. Is there a way to actually produce a visual through tensorflow (possibly with the aid of matplotlibor networkx) of the actual graph structure? The doc of tensorflow is pretty similar to a factor graph so I was wondering:

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

How can an image of the graph structure be generated through tensorflow?

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

In this example below, it would be C_1, C_2, C_3as individual nodes, and then C_1would have the tf.sqrtoperation followed by the operation that brings them together. Maybe the graph structure (nodes,edges) can be imported into networkx? I see that the tensorobjects have a graphattribute but I haven't found out how to actually use this for imaging purposes.

在下面的这个例子中,它将C_1, C_2, C_3作为单独的节点,然后C_1tf.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()

采纳答案by dga

You can get an image of the graph using 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. You create a SummaryWriterand include the sess.graph_defin it. The graph def will be output to the log directory.

您可以使用Tensorboard获取图形的图像。您需要编辑代码以输出图形,然后您可以启动 tensorboard 并查看它。请特别参阅TensorBoard:图形可视化。您创建一个SummaryWriter并将其包含sess.graph_def在其中。图 def 将输出到日志目录。

回答by Salvador Dali

This is exactly what tensorboard was created for. You need to slightly modify your code to store the information about your graph.

这正是创建 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()

This will create a logsfolder with event files in your working directory. After this you should run tensorboard from your command line tensorboard --logdir="logs"and navigate to the url it gives you (http://127.0.0.1:6006). In your browser go to GRAPHS tab and enjoy your graph.

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

You will use TB a lot if you are going to do anything with TF. So it makes sense to learn about it more from officialtutorialsand from this video.

如果你打算用 TF 做任何事情,你会经常使用 TB。所以从官方教程和这个视频中更多地了解它是有意义的。