我正在尝试为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
的属性,这也许正是我要寻找的东西。尽管如此,我看不到可以调用该方法来返回此属性的方法。
答案 0 :(得分:0)
从您的代码中,我认为您正在使用tensorflow v <2。因此,不确定是否可以解决您的问题,但是我可以使用v2.2.0创建adj.list和adj.mat格式
split用于解析名称,紧跟在此answer
之后邻接矩阵生成
# adjacency matrix
# if operation input is node1 and output is node2, then mat[node1][node2] = 1
graph_adj_mat = []
# name to number mapping to set 1/0 from the node name tensorflow gives
graph_node_name_to_num_map = {}
# node number to name map will be needed later to understand matrix
# as tensorflow identify node using name
graph_node_num_to_name_map = {}
# usage of compat module to use Session in v2.2.0
# if v < 2 use tf.Session() as sess
with tf.compat.v1.Session() as sess:
# initiating the matrix and necessary map
for op in sess.graph.get_operations():
graph_node_num_to_name_map[len(graph_adj_mat)] = op.name
graph_node_name_to_num_map[op.name] = len(graph_adj_mat)
graph_adj_mat.append([0]*len(sess.graph.get_operations()))
# parsing the name and setting adj. mat
# edge direction input tensor to output tensor
for op in sess.graph.get_operations():
dst_node_name = op.name.split(':')[0]
for in_tensor in op.inputs:
src_node_name = in_tensor.name.split(':')[0]
graph_adj_mat[graph_node_name_to_num_map[src_node_name]][graph_node_name_to_num_map[dst_node_name]] = 1
print(graph_adj_mat)
print(graph_node_num_to_name_map)
邻接表生成(使用dict)
# adjacency list is dictionary of tensor name,
# each input tensor name key holds output tensor name containing list
graph_adj_list = {}
with tf.compat.v1.Session() as sess:
for op in sess.graph.get_operations():
graph_adj_list[op.name] = []
for op in sess.graph.get_operations():
dst_node_name = op.name.split(':')[0]
for in_tensor in op.inputs:
src_node_name = in_tensor.name.split(':')[0]
graph_adj_list[src_node_name].append(dst_node_name)
# graph_adj_list[in_tensor_name] contains list containing tensor names which are produced using in_tensor_name
print(graph_adj_list)
使用给定代码的修改版本测试输出,
import tensorflow as tf
print(tf.__version__)
tf.compat.v1.disable_eager_execution()
tf.compat.v1.reset_default_graph()
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')