【发布时间】:2019-10-23 16:54:22
【问题描述】:
我正在尝试为 TensorFlow 计算图生成某种文本表示。我知道 Tensorboard 可以为我提供可视化。但是,我需要某种表示形式(邻接矩阵或邻接列表),从中我可以解析与图相关的信息。
到目前为止,我已经尝试了以下方法:
import tensorflow as tf
a = tf.constant(1.3, name = const_a)
b = tf.constant(3.1, name = const_b)
c = tf.add(a,b, name = 'addition')
d = tf.multiply(c,a, name = 'multiplication')
e = tf.add(d,c, name = 'addition_1')
with tf.Session() as sess:
print(sess.run([c,d,e]))
在此之后,我决定将图形对象保存在一个单独的变量中,并尝试从那里解析信息:
graph = tf.get_default_graph()
我发现了如何从 this 文档中获取所有操作的列表。
for op in graph.get_operations():
print(op.values())
这部分实际上为我提供了计算图节点的信息。
(<tf.Tensor 'const_a:0' shape=() dtype=float32>,)
(<tf.Tensor 'const_b:0' shape=() dtype=float32>,)
(<tf.Tensor 'addition:0' shape=() dtype=float32>,)
(<tf.Tensor 'multiplication:0' shape=() dtype=float32>,)
(<tf.Tensor 'addition_1:0' shape=() dtype=float32>,)
但是,我似乎找不到任何方法可以为我提供有关计算图边缘的信息。我找不到任何可以为我提供与每个操作相关的输入张量的方法。我想知道名为addition_1 的操作具有由操作addition 和multiplication; 产生的输入张量或可用于派生此信息的东西。从documentation 看来,Operation 对象似乎有一个名为inputs 的属性,这可能是我正在寻找的东西。尽管如此,我没有看到可以调用来返回此属性的方法。
【问题讨论】:
标签: python tensorflow tensor operation