(TensorFlow)——tf.variable_scope和tf.name_scope详解

本文详细探讨了TensorFlow中变量的创建方式及其区别,包括tf.placeholder、tf.Variable和tf.get_variable,并通过实验对比了它们在命名冲突、变量共享等方面的特性。此外,还介绍了如何利用name_scope和variable_scope对变量进行有效的管理和共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、variable_scope和name_scope存在的价值:
和普通模型相比,深度学习模型的节点(参数)非常多,我们很难确定哪个变量属于哪层。为了解决此问题,所以引入了name_scope和variable_scope,两者分别承担着不同的责任:
*name_scope*: 为了更好的管理变量的命名空间。
*variable_scope*:绝大部分情况下会和tf.get_variable()配合使用,实现变量共享的功能。

2、实验一:
三种方式创建变量:tf.placeholder、tf.Variable、tf.get_variable
(1)实验目的:探索三种方式定义的变量之间的区别:

import tensorflow as tf
# 设置GPU按需增长
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# 1.placeholder 
v1 = tf.placeholder(tf.float32, shape=[2,3,4])
print v1.name
v1 = tf.placeholder(tf.float32, shape=[2,3,4], name='ph')
print v1.name
v1 = tf.placeholder(tf.float32, shape=[2,3,4], name='ph')
print v1.name
print type(v1)
print v1
Placeholder:0
ph:0
ph_1:0
<class 'tensorflow.python.framework.ops.Tensor'>
Tensor("ph_1:0", shape=(2, 3, 4), dtype=float32)
# 2. tf.Variable()
v2 = tf.Variable([1,2], dtype=tf.float32)
print v2.name
v2 = tf.Variable([1,2], dtype=tf.float32, name='V')
print v2.name
v2 = tf.Variable([1,2], dtype=tf.float32, name='V')
print v2.name
print type(v2)
print v2
Variable:0
V:0
V_1:0
<class 'tensorflow.python.ops.variables.Variable'>
Tensor("V_1/read:0", shape=(2,), dtype=float32)
# 3.tf.get_variable() 创建变量的时候必须要提供 name
v3 = tf.get_variable(name='gv', shape=[])  
print v3.name
v4 = tf.get_variable(name='gv', shape=[2])
print v4.name
gv:0
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-7-29efaac2d76c> in <module>()
      2 v3 = tf.get_variable(name='gv', shape=[])
      3 print v3.name
----> 4 v4 = tf.get_variable(name='gv', shape=[2])
      5 print v4.name
此处还有一堆错误信息。。。
ValueError: Variable gv already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
...
print type(v3)
print v3
<class 'tensorflow.python.ops.variables.Variable'>
Tensor("gv/read:0", shape=(), dtype=float32)

接下来要介绍tf.trainable_variables(),他能够将我们定义的所有trainable=True的所有变量以一个list形式返回,我们用他来查看之前所定义的placeholder、variable、get_variable之间的区别:

vs = tf.trainable_variables()
print len(vs)
for v in vs:
    print v
4
Tensor("Variable/read:0", shape=(2,), dtype=float32)
Tensor("V/read:0", shape=(2,), dtype=float32)
Tensor("V_1/read:0", shape=(2,), dtype=float32)
Tensor("gv/read:0", shape=(), dtype=float32)

(2)实验一结论:
我们发现输出结果中没有placeholder,因为它是占位符,当然是不可训练的。
只有tf.get_variable()创建的变量之间会发生命名冲突,在实际使用过程中,三种创建变量方式的用途也是分工明确的:

tf.placeholder()占位符。 *trainable=False*
tf.Variable()一般变量使用这种定义方式 *可以选择trainable类型*
tf.get_variable()一般都是和tf.variable_scope()配合使用。 *可以选择trainable类型*

3、实验二

with tf.name_scope('nsc1'):
    v1 = tf.Variable([1], name='v1')
    with tf.variable_scope('vsc1'):
        v2 = tf.Variable([1], name='v2')
        v3 = tf.get_variable(name='v3', shape=[])
print 'v1.name: ', v1.name
print 'v2.name: ', v2.name
print 'v3.name: ', v3.name
v1.name:  nsc1/v1:0
v2.name:  nsc1/vsc1/v2:0
v3.name:  vsc1/v3:0
with tf.name_scope('nsc1'):
    v4 = tf.Variable([1], name='v4')
print 'v4.name: ', v4.name
v4.name:  nsc1_1/v4:0

tf.name_scope() 并不会对 tf.get_variable() 创建的变量有任何影响。
tf.name_scope() 主要是用来管理命名空间的,这样子让我们的整个模型更加有条理。而 tf.variable_scope() 的作用是为了实现变量共享,它和 tf.get_variable() 来完成变量共享的功能。

1、第一组,用tf.Variable()的方式定义:

def my_image_filter():
    conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),
        name="conv1_weights")
    conv1_biases = tf.Variable(tf.zeros([32]), name="conv1_biases")
    conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),
        name="conv2_weights")
    conv2_biases = tf.Variable(tf.zeros([32]), name="conv2_biases")
    return None

# First call creates one set of 4 variables.
result1 = my_image_filter()
# Another set of 4 variables is created in the second call.
result2 = my_image_filter()
# 获取所有的可训练变量
vs = tf.trainable_variables()
print 'There are %d train_able_variables in the Graph: ' % len(vs)
for v in vs:
    print v
There are 8 train_able_variables in the Graph: 
Tensor("conv1_weights/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("conv1_biases/read:0", shape=(32,), dtype=float32)
Tensor("conv2_weights/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("conv2_biases/read:0", shape=(32,), dtype=float32)
Tensor("conv1_weights_1/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("conv1_biases_1/read:0", shape=(32,), dtype=float32)
Tensor("conv2_weights_1/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("conv2_biases_1/read:0", shape=(32,), dtype=float32)

2.第二种方式,用 tf.get_variable() 的方式

# 下面是定义一个卷积层的通用方式
def conv_relu(kernel_shape, bias_shape):
    # Create variable named "weights".
    weights = tf.get_variable("weights", kernel_shape, initializer=tf.random_normal_initializer())
    # Create variable named "biases".
    biases = tf.get_variable("biases", bias_shape, initializer=tf.constant_initializer(0.0))
    return None


def my_image_filter():
    # 按照下面的方式定义卷积层,非常直观,而且富有层次感
    with tf.variable_scope("conv1"):
        # Variables created here will be named "conv1/weights", "conv1/biases".
        relu1 = conv_relu([5, 5, 32, 32], [32])
    with tf.variable_scope("conv2"):
        # Variables created here will be named "conv2/weights", "conv2/biases".
        return conv_relu( [5, 5, 32, 32], [32])


with tf.variable_scope("image_filters") as scope:
    # 下面我们两次调用 my_image_filter 函数,但是由于引入了 变量共享机制
    # 可以看到我们只是创建了一遍网络结构。
    result1 = my_image_filter()
    scope.reuse_variables()
    result2 = my_image_filter()


# 看看下面,完美地实现了变量共享!!!
vs = tf.trainable_variables()
print 'There are %d train_able_variables in the Graph: ' % len(vs)
for v in vs:
    print v
There are 4 train_able_variables in the Graph: 
Tensor("image_filters/conv1/weights/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("image_filters/conv1/biases/read:0", shape=(32,), dtype=float32)
Tensor("image_filters/conv2/weights/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("image_filters/conv2/biases/read:0", shape=(32,), dtype=float32)

(2)实验二结论:
首先我们要确立一种Graph的思想。在TensorFlow中,我们定义一个变量,相当于往图中增加了一个节点。和普通的Python程序不同,在一般的函数中,我们对输入进行处理,然后返回一个结果,而定义在函数里面的一些局部变量我们就不管了。但是在tensorflow中我们在函数中创建了一个变量,就是往Graph中添加了一个节点,即使出了这个函数,这个节点还是存在于Graph中。

Ref:
https://blog.youkuaiyun.com/Jerr__y/article/details/70809528

关于我之前问你的代码,有几个问题,@property是什么用法 def call(self, inputs, state, scope=‘convLSTM’): “”“Long short-term memory cell (LSTM).”“” with tf.variable_scope(scope or type(self).name): # “BasicLSTMCell” # Parameters of gates are concatenated into one multiply for efficiency. if self._state_is_tuple: c, h = state else: c, h = tf.split(state, 2, 3) concat = _conv_linear([inputs, h], self.filter_size, self.num_features * 4, True) # i = input_gate, j = new_input, f = forget_gate, o = output_gate i, j, f, o = tf.split(concat, 4, 3) new_c = (c * tf.nn.sigmoid(f + self._forget_bias) + tf.nn.sigmoid(i) * self._activation(j)) new_h = self._activation(new_c) * tf.nn.sigmoid(o) if self._state_is_tuple: new_state = LSTMStateTuple(new_c, new_h) else: new_state = tf.concat([new_c, new_h], 3) return new_h, new_state 这一段我需要更详细解释 def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=None): dtype = [a.dtype for a in args][0] with slim.arg_scope([slim.conv2d], stride=1, padding='SAME', activation_fn=None, scope=scope, weights_initializer=tf.truncated_normal_initializer(mean=0.0, stddev=1.0e-3), biases_initializer=bias and tf.constant_initializer(bias_start, dtype=dtype)): if len(args) == 1: res = slim.conv2d(args[0], num_features, [filter_size[0], filter_size[1]], scope='LSTM_conv') else: res = slim.conv2d(tf.concat(args, 3), num_features, [filter_size[0], filter_size[1]], scope='LSTM_conv') return res 这一段也详细解释
03-21
WARNING:tensorflow:From /root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version. Instructions for updating: non-resource variables are not supported in the long term 2025-07-26 19:47:00.316548: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1 2025-07-26 19:47:00.379323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: pciBusID: 0000:39:00.0 name: NVIDIA GeForce RTX 4090 computeCapability: 8.9 coreClock: 2.52GHz coreCount: 128 deviceMemorySize: 23.55GiB deviceMemoryBandwidth: 938.86GiB/s 2025-07-26 19:47:00.379583: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.379632: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcublas.so.10'; dlerror: libcublas.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.380958: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2025-07-26 19:47:00.381316: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2025-07-26 19:47:00.381386: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcusolver.so.10'; dlerror: libcusolver.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.381440: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcusparse.so.10'; dlerror: libcusparse.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.381492: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcudnn.so.7'; dlerror: libcudnn.so.7: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.381501: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2025-07-26 19:47:00.381919: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA 2025-07-26 19:47:00.396214: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2000000000 Hz 2025-07-26 19:47:00.405365: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f45c0000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2025-07-26 19:47:00.405415: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2025-07-26 19:47:00.409166: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix: 2025-07-26 19:47:00.409199: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] WARNING:tensorflow:From /root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py:1089: Layer.apply (from tensorflow.python.keras.engine.base_layer_v1) is deprecated and will be removed in a future version. Instructions for updating: Please use `layer.__call__` method instead. loaded ./checkpoint/decom_net_train/model.ckpt loaded ./checkpoint/illumination_adjust_net_train/model.ckpt No restoration pre model! (480, 640, 3) (680, 720, 3) (415, 370, 3) Start evalating! 0 Traceback (most recent call last): File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1365, in _do_call return fn(*args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1350, in _run_fn target_list, run_metadata) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1443, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Restoration_net/de_conv6_1/biases [[{{node Restoration_net/de_conv6_1/biases/read}}]] During handling of the above exception, another exception occurred: Traceback (most recent call last): File "evaluate.py", line 92, in <module> restoration_r = sess.run(output_r, feed_dict={input_low_r: decom_r_low, input_low_i: decom_i_low}) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 958, in run run_metadata_ptr) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1181, in _run feed_dict_tensor, options, run_metadata) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1359, in _do_run run_metadata) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1384, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Restoration_net/de_conv6_1/biases [[node Restoration_net/de_conv6_1/biases/read (defined at /root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/variables.py:256) ]] Original stack trace for 'Restoration_net/de_conv6_1/biases/read': File "evaluate.py", line 28, in <module> output_r = Restoration_net(input_low_r, input_low_i) File "/root/Python/KinD-master/KinD-master/model.py", line 70, in Restoration_net conv6=slim.conv2d(up6, 256,[3,3], rate=1, activation_fn=lrelu,scope='de_conv6_1') File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1191, in convolution2d conv_dims=2) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1089, in convolution outputs = layer.apply(inputs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func return func(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 1695, in apply return self.__call__(inputs, *args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 547, in __call__ outputs = super(Layer, self).__call__(inputs, *args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 758, in __call__ self._maybe_build(inputs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 2131, in _maybe_build self.build(input_shapes) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 172, in build dtype=self.dtype) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 460, in add_weight **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 447, in add_weight caching_device=caching_device) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 743, in _add_variable_with_custom_getter **kwargs_for_getter) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1573, in get_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1316, in get_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 551, in get_variable return custom_getter(**custom_getter_kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1793, in layer_variable_getter return _model_variable_getter(getter, *args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1784, in _model_variable_getter aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/variables.py", line 328, in model_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/variables.py", line 256, in variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 520, in _true_getter aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 939, in _get_single_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 259, in __call__ return cls._variable_v1_call(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 220, in _variable_v1_call shape=shape) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 198, in <lambda> previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2614, in default_variable_creator shape=shape) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 263, in __call__ return super(VariableMetaclass, cls).__call__(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1666, in __init__ shape=shape) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1854, in _init_from_args self._snapshot = array_ops.identity(self._variable, name="read") File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper return target(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 282, in identity ret = gen_array_ops.identity(input, name=name) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3901, in identity "Identity", input=input, name=name) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 744, in _apply_op_helper attrs=attr_protos, op_def=op_def) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3327, in _create_op_internal op_def=op_def) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1791, in __init__ self._traceback = tf_stack.extract_stack()
最新发布
07-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值