TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
转载http://blog.csdn.net/jerr__y/article/details/60877873
1. 首先看看比较简单的 tf.name_scope(‘scope_name’).
tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。
'''
Signature: tf.name_scope(*args, **kwds)
Docstring:
Returns a context manager for use when defining a Python op.
'''
# 也就是说,它的主要目的是为了更加方便地管理参数命名。
# 与 tf.Variable() 结合使用。简化了命名
with tf.name_scope('conv1') as scope:
weights1 = tf.Variable([1.0, 2.0], name='weights')
bias1 = tf.Variable([0.3], name='bias')
# 下面是在另外一个命名空间来定义变量的
with tf.name_scope('conv2') as scope:
weights2 = tf.Variable([4.0, 2.0], name='weights')
bias2 = tf.Variable([0.33], name='bias')
# 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
print weights1.name
print weights2.name
conv1/weights:0
conv2/weights:0
# 注意,这里的 with 和 python 中其他的 with 是不一样的
# 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行上面的代码
# 就会再生成其他命名空间
with tf.name_scope('conv1') as scope:
weights1 = tf.Variable([1.0, 2.0], name='weights')
bias1 = tf.Variable([0.3], name='bias')
with tf.name_scope('conv2') as scope:
weights2 = tf.Variable([4.0, 2.0], name='weights')
bias2 = tf.Variable([0.33], name='bias')
print weights1.name
print weights2.name
conv1_1/weights:0
conv2_1/weights:0
import tensorflow as tf
2.下面来看看 tf.variable_scope(‘scope_name’)
tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。
# 这里是正确的打开方式~~~可以看出,name 参数才是对象的唯一标识
import tensorflow as tf
with tf.variable_scope('v_scope') as scope1:
Weights1 = tf.get_variable('Weights', shape=[2,3])
bias1 = tf.get_variable('bias', shape=[3])
# 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
Weights2 = tf.get_variable('Weights')
print Weights1.name
print Weights2.name
# 可以看到这两个引用名称指向的是同一个内存对象
v_scope/Weights:0
v_scope/Weights:0
也可以结合 tf.Variable() 一块使用。
import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1:
Weights1 = tf.get_variable('Weights', shape=[2,3])
# bias1 = tf.Variable([0.52], name='bias')
# 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
Weights2 = tf.get_variable('Weights')
bias2 = tf.Variable([0.52], name='bias')
print Weights1.name
print Weights2.name
print bias2.name
v_scope/Weights:0
v_scope/Weights:0
v_scope_1/bias:0
如果 reuse=True 的scope中的变量没有已经定义,会报错!!
import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1:
Weights1 = tf.get_variable('Weights', shape=[2,3])
bias1 = tf.Variable([0.52], name='bias')
print Weights1.name
print bias1.name
# 下面来共享上面已经定义好的变量
# note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
with tf.variable_scope('v_scope', reuse=True) as scope2:
Weights2 = tf.get_variable('Weights')
bias2 = tf.get_variable('bias', [1]) # ‘bias
print Weights2.name
print bias2.name
# 这样子的话就会报错
# Variable v_scope/bias does not exist, or was not created with tf.get_variable()
v_scope/Weights:0
v_scope/bias:0
本文代码:https://github.com/yongyehuang/Tensorflow-Tutorial
TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习的更多相关文章
- TensorFlow基础笔记(13) Mobilenet训练测试mnist数据
主要是四个文件 mnist_train.py #coding: utf-8 import os import tensorflow as tf from tensorflow.examples.tut ...
- TensorFlow基础笔记(0) 参考资源学习文档
1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...
- TensorFlow基础笔记(3) cifar10 分类学习
TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
- TensorFlow基础笔记(11) conv2D函数
#链接:http://www.jianshu.com/p/a70c1d931395 import tensorflow as tf import tensorflow.contrib.slim as ...
- TensorFlow基础笔记(9) Tensorboard可视化显示以及查看pb meta模型文件的方法
参考: http://blog.csdn.net/l18930738887/article/details/55000008 http://www.jianshu.com/p/19bb60b52dad ...
- TensorFlow基础笔记(8) TensorFlow简单人脸识别
数据材料 这是一个小型的人脸数据库,一共有40个人,每个人有10张照片作为样本数据.这些图片都是黑白照片,意味着这些图片都只有灰度0-255,没有rgb三通道.于是我们需要对这张大图片切分成一个个的小 ...
- TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例
http://blog.csdn.net/huachao1001/article/details/78502910 http://blog.csdn.net/u014432647/article/de ...
- TensorFlow基础笔记(6) 图像风格化实验
参考 http://blog.csdn.net/wspba/article/details/53994649 https://www.ctolib.com/AdaIN-style.html Ackno ...
随机推荐
- Snail—OC学习之类别Category
类别就是向类加入一些实用的功能或者方法 利于开发 类能够是系统类.能够是自己定义类 类别跟子类是不一样的.类别仅仅能加入一些方法 属性变量什么的不能够加入 不创建新类,就可以对已有类进行扩展 做项目的 ...
- hive sql 修改列名
ALTER TABLE dev.dev_jypt_jiadian_cate3_pred_20181109 CHANGE utem_third_cate_name item_third_cate_nam ...
- 使用 dockerfile 创建镜像
dockerfile 是一个文本格式的配置文件,可以使用 dockerfile 快速创建自定义的镜像. dockerfile 一般包含4部分信息:基础镜像信息.维护者信息.镜像操作指令.容器启动时执行 ...
- javascript页面刷新的一些方法
在使用js刷新页面的时候,有时会遇到表单的重复提交问题 这时就需要一些强制刷新的办法,从网上大概搜了一下,js的刷新方法大致有以下几种, 刷新页面,不提示重新发送: window.location.r ...
- QListWidget加入小控件
在写一个简单的文件浏览器时,遇到一个问题.想实现新建目录时能像一般的文件管理器那样,目录图标以下有一个编辑框提示用户给目录命名(例如以下图),可是不知道怎么给单元项QListWidgetItem加入Q ...
- [na]802.1x协议无线认证协议&dot1x有线认证实验
以前搞无线时候,会涉及到无线client接入方式的认证协议. 认证方式+加密方式+有线的dot1x. 注:以前都是doc粘贴到博客的,加上没写博客的习惯,因此会比较乱. EAP(扩展认证协议)是什么? ...
- MySQL抓包工具:MySQL Sniffer 和性能优化
简介 MySQL Sniffer 是一个基于 MySQL 协议的抓包工具,实时抓取 MySQLServer 端的请求,并格式化输出.输出内容包访问括时间.访问用户.来源 IP.访问 Database. ...
- rabbitmq 常用的一些命令
rabbitmqctl set_user_tags admin administrator #给用户设置角色 rabbitmqctl set_permissions -p emove admin &q ...
- 前端开发中Cookie那些事儿
前段时间做了项目,在前端实现中频繁的操作cookie,记录几点供大家参考! cookie操作在前端开发过程中经常遇到,当然如果只是用来存储一些简单用户数据,还是比较简单的,我们要做的可能只是设置coo ...
- SQL server插入数据后,如何获取自增长字段的值?
insert into Tb_People(uname,era,amount) values( '兆周','老年','10000') select @@identity --当运行完插入语句后,执行s ...