【发布时间】:2017-07-04 13:33:04
【问题描述】:
如何列出节点依赖的所有 Tensorflow 变量/常量/占位符?
示例 1(常量的添加):
import tensorflow as tf
a = tf.constant(1, name = 'a')
b = tf.constant(3, name = 'b')
c = tf.constant(9, name = 'c')
d = tf.add(a, b, name='d')
e = tf.add(d, c, name='e')
sess = tf.Session()
print(sess.run([d, e]))
我想要一个函数list_dependencies() 比如:
-
list_dependencies(d)返回['a', 'b'] -
list_dependencies(e)返回['a', 'b', 'c']
示例 2(占位符和权重矩阵之间的矩阵相乘,然后加上偏置向量):
tf.set_random_seed(1)
input_size = 5
output_size = 3
input = tf.placeholder(tf.float32, shape=[1, input_size], name='input')
W = tf.get_variable(
"W",
shape=[input_size, output_size],
initializer=tf.contrib.layers.xavier_initializer())
b = tf.get_variable(
"b",
shape=[output_size],
initializer=tf.constant_initializer(2))
output = tf.matmul(input, W, name="output")
output_bias = tf.nn.xw_plus_b(input, W, b, name="output_bias")
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run([output,output_bias], feed_dict={input: [[2]*input_size]}))
我想要一个函数list_dependencies() 比如:
-
list_dependencies(output)返回['W', 'input'] -
list_dependencies(output_bias)返回['W', 'b', 'input']
【问题讨论】:
标签: python tensorflow