https://blog.csdn.net/qq_22522663/article/details/78729029

1. tf.Variable与tf.get_variable
tensorflow提供了通过变量名称来创建或者获取一个变量的机制。通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递。
TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。
当然,变量也可以通过tf.Varivale来创建。当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价。

#以下两个定义是等价的
v = tf.get_variable('v', shape=[1], initializer=tf.constant_initializer(1.0))
v = tf.Variable(tf.constant(1.0, shape=[1], name='v')
1
2
3
tf.get_varialbe和tf.Variable最大的区别在于:tf.Variable的变量名是一个可选项,通过name=’v’的形式给出。但是tf.get_variable必须指定变量名。

2. tf.get_variable与tf.variable_scope
上面已经提到过了:TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。在这里,我主要解释下大家深恶痛绝的reuse问题。
其实只要记住一件事情就ok了:当reuse为False或者None时(这也是默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。
下面我们通过代码来看下:

#reuse=False时会报错的情况:
with tf.variable_scope('foo'):
v = tf.get_variable('v',[1],initializer=tf.constant_initializer(1.0))

with tf.variable_scope('foo'):
v1 = tf.get_variable('v',[1])
1
2
3
4
5
6
在这种情况下会报错:Variable foo/v already exists, disallowed.Did you mean to set reuse=True in Varscope?
其原因就是在命名空间foo中创建了相同的变量。如果我要在foo下创建一个变量v1,其name=‘v’,只需要将reuse设置为Ture就ok了。将上面第二部分代码修改为:

with tf.variable_scope('foo', reuse=True):
v1 = tf.get_variable('v',[1])
print(v1.name) #结果为foo/v
1
2
3
当reuse已经设置为True时,tf.variable_scope只能获取已经创建过的变量。这个时候,在命名空间bar中创建name=‘v’的变量v3,将会报错:Variable bar/v dose not exists, diallowed. Did you mean to set reuse=None in VarScope?

with tf.variable_scope('bar', reuse=True):
v3 = tf.get_variable('v',[1])
1
2
简而言之,reuse=False时,tf.variable_scope创建变量;reuse=True时,tf.variable_scope获取变量。

3. tf.variable_scope与tf.name_scope
除了tf.variable_scope,tf.name_scope函数也提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。
tf.get_variable函数不受tf.name_scope的影响。
我们从代码看下这句话的具体意思。
首先是tf.variable_scope:

with tf.variable_scope('foo'):
a = tf.get_variable('bar',[1])
print(a.name)#结果为foo/bar:0
1
2
3
再看tf.name_scope:

with tf.name_scope('a'):
a=tf.Variable([1])
print(a.name)#结果为a/Variable:0

b=tf.get_variable('b',[1])
print(b.name)#结果为b:0
1
2
3
4
5
6
从这个结果中,我们能很清晰地看到,tf.get_variable创建的变量并不是a/b:0,而是b:0。这就表示了在tf.name_scope函数下,tf.get_variable不受其约束。
---------------------
作者:Csclion
来源:CSDN
原文:https://blog.csdn.net/qq_22522663/article/details/78729029
版权声明:本文为博主原创文章,转载请附上博文链接!

彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同的更多相关文章

  1. tensorflow共享变量 the difference between tf.Variable() and get_variable()

    一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...

  2. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  3. TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

    1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...

  4. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

    tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...

  5. tensorflow 笔记12:函数区别:placeholder,variable,get_variable,参数共享

    一.函数意义: 1.tf.Variable() 变量 W = tf.Variable(<initial-value>, name=<optional-name>) 用于生成一个 ...

  6. TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()

    Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...

  7. tf.Variable() 与tf.get_variable()的区别

    每次调用 tf.Variable() 都会产生一个新的变量,变量名称是一个可选参数,运行命名相同,如果命名冲突会根据命名先后对名字进行处理, tf.get_variable()的变量名称是必填参数,t ...

  8. TensorFlow函数(二)tf.get_variable() 和 tf.Variable()

    tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...

  9. tf.Variable()、tf.get_variable()和tf.placeholder()

    1.tf.Variable() tf.Variable(initializer,name) 功能:tf.Variable()创建变量时,name属性值允许重复,检查到相同名字的变量时,由自动别名机制创 ...

随机推荐

  1. Flex4学习笔记1---基本语法

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  2. Python基础1 介绍、基本语法

    ---恢复内容开始--- 本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表 ...

  3. Linux 配置开机自启 和 修改环境变量

    [ 操作系统 Centos7 ] 一,开机自动启动的配置 1,赋予 /etc/rc.local 的执行权限 => chmod +x /etc/rc.local 2,在 /etc/rc.local ...

  4. node.js 爬虫动态代理ip

    参考文章: https://andyliwr.github.io/2017/12/05/nodejs_spider_ip/ https://segmentfault.com/q/10100000081 ...

  5. 网站优化JS css压缩

    在nginx 中开启gzip压缩后,可以大大减少资js css 体积,原来200KB,压缩后只有66KB server{ gzip on; gzip_types text/plain applicat ...

  6. Centos 7 安装 sphinx2.2 (转)

    一.首先到Sphinx官网找到下载地址:http://sphinxsearch.com/downloads/release/如果你比较懒,好吧:http://sphinxsearch.com/file ...

  7. KVM虚拟化技术(一)虚拟化简介

    一 .虚拟化 虚拟化是指计算机元件在虚拟的基础上而不是真实的基础上运行.虚拟化技术可以扩大硬件的容量,简化软件的重新配置过程.CPU的虚拟化技术可以单CPU模 拟多CPU并行,允许一个平台同时运行多个 ...

  8. Bug : Cannot evaluate ...toString()

  9. Delphi编写Shell扩展

    用delphi创建一个外壳扩展(Shell Extension)程序的基本步骤如下: (1) 创建一个 ActiveX Library 工程,命名为“CloudUpload“(2) 创建一个新的自动化 ...

  10. 吴裕雄 python深度学习与实践(3)

    import threading, time def doWaiting(): print('start waiting:', time.strftime('%S')) time.sleep(3) p ...