首页 > 解决方案 > 如何初始化特定name_scope下的局部变量

问题描述

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 引起的?

标签: pythontensorflowdeep-learning

解决方案


使用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']

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


推荐阅读