Tensorflow:如何通过名称获取张量?
Tensorflow: How to get a tensor by name?
我无法通过名称恢复张量,我什至不知道是否可行。
我有一个函数可以创建我的图表:
def create_structure(tf, x, input_size,dropout):
with tf.variable_scope("scale_1") as scope:
W_S1_conv1 = deep_dive.weight_variable_scaling([7,7,3,64], name='W_S1_conv1')
b_S1_conv1 = deep_dive.bias_variable([64])
S1_conv1 = tf.nn.relu(deep_dive.conv2d(x_image, W_S1_conv1,strides=[1, 2, 2, 1], padding='SAME') + b_S1_conv1, name="Scale1_first_relu")
.
.
.
return S3_conv1,regularizer
我想访问这个函数外的变量S1_conv1。我试过了:
with tf.variable_scope('scale_1') as scope_conv:
tf.get_variable_scope().reuse_variables()
ft=tf.get_variable('Scale1_first_relu')
但这给了我一个错误:
ValueError:共享不足:变量 scale_1/Scale1_first_relu 不存在,不允许。您是要在 VarScope 中设置 reuse=None 吗?
但这行得通:
with tf.variable_scope('scale_1') as scope_conv:
tf.get_variable_scope().reuse_variables()
ft=tf.get_variable('W_S1_conv1')
我可以通过
解决这个问题
return S3_conv1,regularizer, S1_conv1
但我不想那样做。
我认为我的问题是 S1_conv1 并不是一个真正的变量,它只是一个张量。有没有办法做我想做的事?
所有张量都有字符串名称,如下所示
[tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
一旦知道名称,就可以使用 <name>:0
获取张量(0 指端点,有点多余)
例如,如果你这样做
tf.constant(1)+tf.constant(2)
您有以下张量名称
[u'Const', u'Const_1', u'add']
所以你可以获取加法的输出为
sess.run('add:0')
请注意,这不是 public API 的一部分。自动生成的字符串张量名称是一个实现细节,可能会更改。
有一个函数tf.Graph.get_tensor_by_name()。例如:
import tensorflow as tf
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d, name='example')
with tf.Session() as sess:
test = sess.run(e)
print e.name #example:0
test = tf.get_default_graph().get_tensor_by_name("example:0")
print test #Tensor("example:0", shape=(2, 2), dtype=float32)
在这种情况下你要做的就是:
ft=tf.get_variable('scale1/Scale1_first_relu:0')
或者更简单,从模型 .pb
文件附带的相应 .pbtxt
文件中推断出来。由于它取决于模型,每个情况都不同。
我无法通过名称恢复张量,我什至不知道是否可行。
我有一个函数可以创建我的图表:
def create_structure(tf, x, input_size,dropout):
with tf.variable_scope("scale_1") as scope:
W_S1_conv1 = deep_dive.weight_variable_scaling([7,7,3,64], name='W_S1_conv1')
b_S1_conv1 = deep_dive.bias_variable([64])
S1_conv1 = tf.nn.relu(deep_dive.conv2d(x_image, W_S1_conv1,strides=[1, 2, 2, 1], padding='SAME') + b_S1_conv1, name="Scale1_first_relu")
.
.
.
return S3_conv1,regularizer
我想访问这个函数外的变量S1_conv1。我试过了:
with tf.variable_scope('scale_1') as scope_conv:
tf.get_variable_scope().reuse_variables()
ft=tf.get_variable('Scale1_first_relu')
但这给了我一个错误:
ValueError:共享不足:变量 scale_1/Scale1_first_relu 不存在,不允许。您是要在 VarScope 中设置 reuse=None 吗?
但这行得通:
with tf.variable_scope('scale_1') as scope_conv:
tf.get_variable_scope().reuse_variables()
ft=tf.get_variable('W_S1_conv1')
我可以通过
解决这个问题return S3_conv1,regularizer, S1_conv1
但我不想那样做。
我认为我的问题是 S1_conv1 并不是一个真正的变量,它只是一个张量。有没有办法做我想做的事?
所有张量都有字符串名称,如下所示
[tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
一旦知道名称,就可以使用 <name>:0
获取张量(0 指端点,有点多余)
例如,如果你这样做
tf.constant(1)+tf.constant(2)
您有以下张量名称
[u'Const', u'Const_1', u'add']
所以你可以获取加法的输出为
sess.run('add:0')
请注意,这不是 public API 的一部分。自动生成的字符串张量名称是一个实现细节,可能会更改。
有一个函数tf.Graph.get_tensor_by_name()。例如:
import tensorflow as tf
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d, name='example')
with tf.Session() as sess:
test = sess.run(e)
print e.name #example:0
test = tf.get_default_graph().get_tensor_by_name("example:0")
print test #Tensor("example:0", shape=(2, 2), dtype=float32)
在这种情况下你要做的就是:
ft=tf.get_variable('scale1/Scale1_first_relu:0')
或者更简单,从模型 .pb
文件附带的相应 .pbtxt
文件中推断出来。由于它取决于模型,每个情况都不同。