TensorFlow学习笔记(六)
来源:互联网 发布:数据存储系统厂家 编辑:程序博客网 时间:2024/05/02 04:17
name_scope与variable_scope
最近在使用Keras,这个库可以选择backend,它有三个backend,包括:TensorFlow,CNTK,Theano。我选择使用的是TensorFlow作为backend,在Keras的optimizers.py文件中有:
from . import backend as K...if K.backend() == 'tensorflow': import tensorflow as tf...with K.name_scope(self.__class__.__name__):...也就是说,其中用到了TensorFlow的name_scope,所以我们在这里再来回顾一下name_scope与variable_scope。
首先我们来看一段代码:
with tf.name_scope("helloworld") as name_scope: arr1=tf.get_variable("arr1",shape=[2,10],dtype=tf.float32) print name_scope print arr1.name print "scope_name:%s" % tf.get_variable_scope().original_name_scope输出结果如下:
由上面可知,1)tf.name_scope()返回的是一个string;2)在name_scope中定义的variable的name并没有helloworld/前缀;3)tf.get_variable_scope()的original_name_scope为空。
下面,我们通过一段代码来对variable_scope进行实验:
with tf.variable_scope("hello") as variable_scope: arr1 = tf.get_variable("arr1", shape=[2, 10], dtype=tf.float32) print variable_scope print variable_scope.name print arr1.name print tf.get_variable_scope().original_name_scope with tf.variable_scope("hi") as v_scope2: print tf.get_variable_scope().original_name_scope这段代码的输出为:
可以看出来,1)tf.variable_scope()返回的是一个op对象;2)我们在variable_scope中定义的variable的name加上了"hello/"的前缀;3)tf.get_variable_scope()的original_name_scope是嵌套后的scope name。
那如果我们将name_scope与variable_scope一起使用会有怎样的效果呢?
with tf.name_scope("name1"): with tf.variable_scope("var1"): w = tf.get_variable("w",shape=[2]) op_add = tf.add(w,[3])print w.nameprint op_add.name
输出结果为:
可见,无论是name_scope还是variable_scope都会给op加上前缀。
综上,1)name_scope对于get_variable()创建的变量的名字不会有任何影响,而我们在其中创建的op将会被加上前缀;2)tf.get_variable_scope()返回的只是variable_scope,并不管name_scope,所以我们在使用tf.get_variable_scope().reuse_variables()时可以无视name_scope。
不过,值得注意的是,name_scope虽然对于使用get_variable()创建的变量的名字不会有影响,但是对于使用tf.Variable()创建的变量的名字是有影响的:
with tf.name_scope("name1"):a=tf.Variable(0,dtype='int64',name='a')b=tf.get_variable("b",shape=[3])print a.nameprint b.name得到的结果为:
也就是说,name_scope会为利用tf.Variable()创建的变量加上前缀。
- TensorFlow学习笔记(六)
- TensorFlow学习笔记(六)
- TensorFlow学习笔记(六)Variable变量
- python tensorflow学习笔记(六)最小二乘法
- tensorflow学习笔记(六):LSTM 与 GRU
- tensorflow学习笔记(六):LSTM 与 GRU
- TensorFlow学习笔记(六):如何理解dropout?
- tensorflow学习笔记(六):TF.contrib.learn大杂烩
- tensorflow学习笔记(六):cnn过程可视化
- Tensorflow深度学习笔记(六)--代价函数
- 莫烦tensorflow教程笔记(六)
- TensorFlow学习(六):形状相关操作
- TensorFlow学习笔记(一):TensorFlow安装
- TensorFlow学习笔记(二):TensorFlow入门
- tensorflow学习笔记(一):tensorflow安装
- tensorflow学习笔记(三):tensorflow 基础知识
- Docker学习笔记(六)-fixbug3-docker安装Tensorflow的问题
- tensorflow学习笔记六:保存和加载训练模型
- java集合
- 功能独特的开源人工智能项目
- 电商项目搜索自定义组合控件
- 初步认知HashSet
- Java数据结构和算法(链表)
- TensorFlow学习笔记(六)
- Spring@Autowired注解与自动装配
- Java设计模式之适配器模式
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 使用TensorFlow双流卷积神经网络对CK+表情数据库进行分类
- Java 中的编译期常量是什么?使用它有什么风险?
- String常量池问题的几个例子
- 61. Rotate List (旋转链表)
- win10专业版秘钥激活