首页 文章

在TensorFlow中显示图表的图像?

提问于
浏览
17

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

How can an image of the graph structure be generated through 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()

2 回答

  • 45

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

  • 9

    这正是为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 tutorialsvideo中了解更多信息是有道理的 .

相关问题