如何在特定的name_scope下初始化局部变量

时间:2019-04-08 03:06:13

标签: python tensorflow deep-learning

with tf.name_scope('trans_part'):
    with tf.name_scope('t_conv3'):    
        # next line is the error line
        t = tf.layers.conv2d_transpose(t, filters=f, kernel_size=w, strides=s, padding='same')
        t = tf.nn.tanh(t)

    with tf.name_scope('identical_conv4'):
        t = tf.layers.conv2d(inputs=t, filters=f, kernel_size=w, strides=1, padding='same')
        t  = tf.nn.tanh(t)

        t = tf.layers.conv2d(inputs=t, filters=f, kernel_size=w, strides=1, padding='same')
        t = tf.nn.tanh(t)

初始化

var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='trans_part')

sess.run(tf.variables_initializer(var_list=var))

错误:

line 43 
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_transpose/kernel_1

问题:

  1. 如何在特定name_scope下初始化变量?
  2. 这是由初始化程序还是name_scope引起的?

1 个答案:

答案 0 :(得分:1)

使用tf.variable_scope()代替tf.name_scope()tf.name_scope()仅将作用域名称附加到结果张量名称(例如,应用卷积/密集层的结果),而不附加到基础变量。 tf.variable_scope()但是会​​将作用域名称附加到两者。

import tensorflow as tf

x = tf.placeholder(tf.float32, shape=(None, 32, 32, 3))
t = x
with tf.variable_scope('trans_part'):
    with tf.name_scope('t_conv3', default_name=scope):    
        t = tf.layers.conv2d_transpose(t,
                                       filters=3,
                                       kernel_size=3,
                                       strides=1,
                                       padding='same')
        print(t.name) # trans_part/t_conv3/conv2d_transpose/BiasAdd:0
        t = tf.nn.tanh(t)
    with tf.name_scope('identical_conv4'):
        t = tf.layers.conv2d(inputs=t,
                             filters=3,
                             kernel_size=3,
                             strides=1,
                             padding='same')
        t  = tf.nn.tanh(t)

        t = tf.layers.conv2d(inputs=t,
                             filters=3,
                             kernel_size=3,
                             strides=1,
                             padding='same')
        t = tf.nn.tanh(t)

var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='trans_part')

with tf.Session() as sess:
    sess.run(tf.variables_initializer(var_list=var_list))

print([v.name for v in var_list])
# ['trans_part/conv2d_transpose/kernel:0',
#  'trans_part/conv2d_transpose/bias:0',
#  'trans_part/conv2d/kernel:0',
#  'trans_part/conv2d/bias:0',
#  'trans_part/conv2d_1/kernel:0',
#  'trans_part/conv2d_1/bias:0']

您还可以将范围的名称添加到您定义的图层的名称中,然后过滤掉名称中没有范围名称子字符串的变量:

import tensorflow as tf

x = tf.placeholder(tf.float32, shape=(None, 32, 32, 3))
t = x
with tf.name_scope('trans_part') as scope:
    with tf.name_scope('t_conv3'):    
        # next line is the error line
        t = tf.layers.conv2d_transpose(t,
                                       filters=3,
                                       kernel_size=3,
                                       strides=1,
                                       padding='same',
                                       name=scope + 'con2d_transpose')
        t = tf.nn.tanh(t)

    with tf.name_scope('identical_conv4') as scope2:
        t = tf.layers.conv2d(inputs=t,
                             filters=3,
                             kernel_size=3,
                             strides=1,
                             padding='same',
                             name=scope + 'conv2d1')
        t  = tf.nn.tanh(t)

        t = tf.layers.conv2d(inputs=t,
                             filters=3,
                             kernel_size=3,
                             strides=1,
                             padding='same',
                             name=scope + 'conv2d2')
        t = tf.nn.tanh(t)

# from all trainable variables pick those that do have 'trans_part`
# substring in their name        
var_list = [v for v in tf.trainable_variables() if 'trans_part' in v.name]

with tf.Session() as sess:
    sess.run(tf.variables_initializer(var_list=var_list))

print([v.name for v in var_list])
# ['trans_part/con2d_transpose/kernel:0',
#  'trans_part/con2d_transpose/bias:0',
#  'trans_part/conv2d1/kernel:0',
#  'trans_part/conv2d1/bias:0',
#  'trans_part/conv2d2/kernel:0',
#  'trans_part/conv2d2/bias:0']

请注意,在这种情况下,您定义的每个图层的名称都应该唯一!