当前位置: 首页 > news >正文

TensorFlow入门(四) name / variable_scope 的使

name/variable_scope 的作用

欢迎转载,但请务必注明原文出处及作者信息。

@author: huangyongye 
@creat_date: 2017-03-08

refer to: Sharing Variables 

name / variable_scope 详细理解请看: TensorFlow入门(七) 充分理解 name / variable_scope

* 起因:在运行 RNN LSTM 实例代码的时候出现 ValueError。 * 
在 TensorFlow 中,经常会看到这两个东东出现,这到底是什么鬼,是用来干嘛的。在做 LSTM 的时候遇到了下面的错误:

ValueError: Variable rnn/basic_lstm_cell/weights already exists, disallowed.

然后谷歌百度都查了一遍,结果也不知是咋回事。我是在 jupyter notebook 运行的示例程序,第一次运行的时候没错,然后就总是出现上面的错误。后来才知道是 get_variable() 和 variable_scope() 搞的鬼。 

下面就来分析一下 TensorFlow 中到底用这来干啥。

import tensorflow as tf
# 设置GPU按需增长
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

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

转载于:https://www.cnblogs.com/Ph-one/p/9257285.html

相关文章:

  • tf.equal的使用
  • tf.argmax()以及axis解析
  • 使SourceInsight支持Python语言的方法
  • 线性回归与分类, 解决与区别
  • 单片机中printf函数的重映射
  • Tensorflow一些常用基本概念与函数(1)
  • STM32之独立看门狗(IWDG)与窗口看门狗(WWDG)总结
  • 线性回归、Logistic回归、Softmax回归
  • TensorFlow学习---tf.nn.dropout防止过拟合
  • 神经网络优化算法如何选择Adam,SGD
  • tf.nn.relu
  • tf.nn.max_pool
  • 【TensorFlow】tf.nn.max_pool实现池化操作
  • git博客好的例子
  • 桌面版Ubuntu系统固定IP设置和Network-manager设置
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • Apache Spark Streaming 使用实例
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • CSS进阶篇--用CSS开启硬件加速来提高网站性能
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Linux gpio口使用方法
  • miaov-React 最佳入门
  • React-Native - 收藏集 - 掘金
  • 编写符合Python风格的对象
  • 对JS继承的一点思考
  • 飞驰在Mesos的涡轮引擎上
  • 汉诺塔算法
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 前端面试之CSS3新特性
  • 嵌入式文件系统
  • 数据科学 第 3 章 11 字符串处理
  • 思考 CSS 架构
  • 算法-插入排序
  • 用quicker-worker.js轻松跑一个大数据遍历
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • ​TypeScript都不会用,也敢说会前端?
  • #pragma预处理命令
  • (Python) SOAP Web Service (HTTP POST)
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)ssm高校实验室 毕业设计 800008
  • (九)One-Wire总线-DS18B20
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (转)编辑寄语:因为爱心,所以美丽
  • .chm格式文件如何阅读
  • .NET 8.0 中有哪些新的变化?
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .net6Api后台+uniapp导出Excel
  • .NET与 java通用的3DES加密解密方法
  • /usr/bin/env: node: No such file or directory