14、TensorFLow 变量命名空间
来源:互联网 发布:sd卡受损数据恢复 编辑:程序博客网 时间:2024/05/16 14:11
一、name_scope
with tf.name_scope(name):
- name_scope: 为了更好地管理变量的命名空间而提出的。比如在
tensorboard
中,因为引入了name_scope
, 我们的Graph
看起来才井然有序。name_scope
对get_variable
创建变量的 name 没有影响,即get_variable
创建的变量不在name_scope
这个命名空间中
二、variable_scope
with tf.variable_scope(name_or_scope, reuse=None):
- variable_scope: 大部分情况下,跟
tf.get_variable()
配合使用,实现变量共享
的功能- 可通过
tf.get_variable_scope().reuse == True/False
判断参变量是否共享- 当前变量作用域可以用
tf.get_variable_scope()
进行检索并且reuse
标签可以通过调用tf.get_variable_scope().reuse_variables()
设置为True
三、共享参变量
1、方法
- 使用
tf.Variable()
创建同一个 name 的变量(操作名不同),均不会报错,但系统会自动修改 name(实质还是不让共享参变量) - 使用
tf.get_varible()
创建同一个 name 的变量(操作名不同),均会报错(为了避免无意识的参变量复用造成的错误) - 我们可以在
variable_scope
中使用tf.get_variable()
创建变量,并通过with tf.variable_scope(name_or_scope, reuse=True)
来共享参变量:- reuse=True:将只能获取命名空间中已经创建过的变量,如果变量不存在,则
tf.get_variable
函数将报错。 - reuse=None / False:
tf.get_variable
操作将创建新的变量,如果同名的变量已经存在,则tf.get_variable
函数将报错。
- reuse=True:将只能获取命名空间中已经创建过的变量,如果变量不存在,则
2、代码示例
# 下面是定义一个卷积层的通用方式def conv_relu(input, 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_intializer(0.0)) conv = tf.nn.conv2d(input, weights, strides=[1, 1, 1, 1], padding='SAME') return tf.nn.relu(conv + biases)# 定义一个图片过滤器def my_image_filter(input_images): with tf.variable_scope("conv1"): # Variables created here will be named "conv1/weights", "conv1/biases". relu1 = conv_relu(input_images, [5, 5, 32, 32], [32]) with tf.variable_scope("conv2"): # Variables created here will be named "conv2/weights", "conv2/biases". return conv_relu(relu1, [5, 5, 32, 32], [32])# 实验一:调用 my_image_filter() 两次result1 = my_image_filter(image1)result2 = my_image_filter(image2)>>> Raises ValueError(... conv1/weights already exists ...), tf.get_variable()会检测已经存在的变量是否已经共享# 解决方法一, 可以在设计网络时加上一个布尔型的 reuse 参数 with tf.variable_scope("image_filters"): result1 = my_image_filter(image1)with tf.variable_scope("image_filters", reuse=True): result2 = my_image_filter(image2)# 解决方法二with tf.variable_scope("image_filters") as scope: # 下面我们两次调用 my_image_filter 函数,但是由于引入了变量共享机制 # 可以看到我们只是创建了一遍网络结构。 result1 = my_image_filter(image1) scope.reuse_variables() result2 = my_image_filter(image2)# 解决方法三with tf.variable_scope("image_filters") as scope: result1 = my_image_filter(image1)with tf.variable_scope(scope, reuse=True): result2 = my_image_filter(image2)# 打印出所有的可训练参变量vs = tf.trainable_variables()print('There are %d trainable_variables in the Graph: ' % len(vs))for v in vs: print(v)# 输出结果证明确实:参变量共享,因为只有四个变量,没有创建新的变量。There are 4 trainable_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)
四、取出所有可训练参数
# Returns all variables created with trainable=True in a var_listvar_list = tf.trainable_variables()init = tf.global_variables_initializer()sess.run(init)for var in var_list: sess.run(var)
五、参考资料
1、https://www.tensorflow.org/programmers_guide/variables#sharing_variables
2、共享变量
3、TensorFlow 变量与命名空间_固有结界
阅读全文
0 0
- 14、TensorFLow 变量命名空间
- TensorFlow 变量共享,命名空间
- Tensorflow 变量命名空间及变量重用
- Tensorflow 变量命名空间及变量重用
- 变量-命名空间
- tensorflow09 《TensorFlow实战Google深度学习框架》笔记-05-02变量管理(命名空间)code
- TensorFlow命名空间和TensorBoard图节点
- 不同命名空间的相同变量
- Flex 组成、变量、函数、命名空间
- Flex 组成、变量、函数、命名空间
- javascript变量、作用域、命名空间
- C语言命名空间与定义变量
- Node.js global 变量 (全局命名空间!)
- javascript 变量、作用域、命名空间、this
- 原来std命名空间中使用了max变量啊
- C++命名空间里面定义变量出现重定义错误
- js避免变量冲突问题,使用命名空间
- 关于c++的引用变量与命名空间
- css3 加载瀑布流 column-coun属性
- 误删除oracle数据的恢复指令
- 项目开发流程概要
- C++ const用法 尽可能使用const
- Rabbitmq集群及使用keepalived实现rabbitmq高可用性集群
- 14、TensorFLow 变量命名空间
- unity 360°全景图
- 线性结构4 Pop Sequence
- touch:按下时震动,松开时停止
- C# 多线程异步编程笔记
- 第13周项目3- 是否二叉排序树?
- 有关人工智能方面的调研报告
- poj2082(单调栈)
- Fresco的简单使用