python - 如何初始化特定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
问题:
- 如何初始化特定name_scope下的变量?
- 这是由初始化程序还是 name_scope 引起的?
解决方案
使用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']
请注意,在这种情况下,您定义的每一层的名称都应该是唯一的!
推荐阅读
- java - Java 字符串参数化 - 间接 toString 方法的性能损失
- postgresql - PostgreSQL:在完成之前通过 dblink 获取查询的 PID
- java - 两个日期和时区之间的差异
- java - 如何使用凭证详细信息导出 Tally 分类账期初和期末余额?
- sql - (PL)SQL:使用同一表中的现有字段更新字段
- swiftui - Xcode 11.3 / iOS 13.3 NavigationLink 仅推送一次
- python - 为什么在python中更新字典时
- python - nginx:[警告] 0.0.0.0:80 上的服务器名称“域”冲突,忽略
- javascript - 响应式数据表导致奇怪的选择框错误
- c# - 在 C# 中用构造函数赋值和用属性赋值有什么区别?