1. tf.Variable()
W = tf.Variable(<initial-value>, name=<optional-name>)
1
用于生成一个初始值为initial-value的变量。必须指定初始化值

2.tf.get_variable()
W = tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None,
regularizer=None, trainable=True, collections=None)
1
2
获取已存在的变量(要求不仅名字,而且初始化方法等各个参数都一样),如果不存在,就新建一个。
可以用各种初始化方法,不用明确指定值。

3.区别
推荐使用tf.get_variable(), 因为:

1. 初始化更方便
比如用xavier_initializer:

W = tf.get_variable("W", shape=[784, 256],
initializer=tf.contrib.layers.xavier_initializer())
1
2
2. 方便共享变量
因为tf.get_variable() 会检查当前命名空间下是否存在同样name的变量,可以方便共享变量。而tf.Variable 每次都会新建一个变量。

需要注意的是tf.get_variable() 要配合reuse和tf.variable_scope() 使用。

4. 举个栗子
4.1 首先介绍一下tf.variable_scope().
如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。为了解决这个问题,TensorFlow 提出了 tf.variable_scope 函数:它的主要作用是,在一个作用域 scope 内共享一些变量,举个简单的栗子:

with tf.variable_scope("foo"):
v = tf.get_variable("v", [1]) #v.name == "foo/v:0"
1
2
简单来说就是给变量名前再加了个变量空间名。

4.2 对比
接下来看看怎么用tf.get_variable()实现共享变量:

with tf.variable_scope("one"):
a = tf.get_variable("v", [1]) #a.name == "one/v:0"
with tf.variable_scope("one"):
b = tf.get_variable("v", [1]) #创建两个名字一样的变量会报错 ValueError: Variable one/v already exists
with tf.variable_scope("one", reuse = True): #注意reuse的作用。
c = tf.get_variable("v", [1]) #c.name == "one/v:0" 成功共享,因为设置了reuse

assert a==c #Assertion is true, they refer to the same object.
1
2
3
4
5
6
7
8
然后看看如果用tf.Variable() 会有什么效果:

with tf.variable_scope("two"):
d = tf.get_variable("v", [1]) #d.name == "two/v:0"
e = tf.Variable(1, name = "v", expected_shape = [1]) #e.name == "two/v_1:0"

assert d==e #AssertionError: they are different objects
1
2
3
4
5
可以看到,同样的命名空间(‘two’)和名字(v),但是d和e的值却不一样。

Reference:
1. https://stackoverflow.com/questions/37098546/difference-between-variable-and-get-variable-in-tensorflow
2. https://www.tensorflow.org/versions/r1.2/api_docs/python/tf/variable_scope
3. http://blog.csdn.net/u013645510/article/details/53769689
---------------------
作者:adrianna_xy
来源:CSDN
原文:https://blog.csdn.net/u012223913/article/details/78533910
版权声明:本文为博主原创文章,转载请附上博文链接!

tf.get_variable()的更多相关文章

  1. Tensorflow函数——tf.variable_scope()

    Tensorflow函数——tf.variable_scope()详解 https://blog.csdn.net/yuan0061/article/details/80576703 2018年06月 ...

  2. tf.tile()函数的用法

    y = tf.tile(tf.range(2, dtype=tf.int32)[:, tf.newaxis], [2,3]) # tf.tile(input,[a,b]) 输入数据,按照对应维度将矩阵 ...

  3. tf.multiply()和tf.matmul()区别

    (1)tf.multiply是点乘,即Returns x * y element-wise. (2)tf.matmul是矩阵乘法,即Multiplies matrix a by matrix b, p ...

  4. tf.reduce_sum() and tf.where()的用法

    import tensorflow as tfimport numpy as npsess=tf.Session()a=np.ones((5,6))c=tf.cast(tf.reduce_sum(a, ...

  5. tf.random_shuffle()函数解析

    tf.random_shuffle(value, seed=None, name=None) 函数就是随机地将张量沿第一维度打乱 value:将被打乱的张量. seed:一个 Python 整数.用于 ...

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

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

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

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

  8. Tensorflow踩坑之tf.nn.bidirectional_dynamic_rnn()报错 “ValueError: None values not supported.”

    详细解决方法见链接:https://stackoverflow.com/questions/39808336/tensorflow-bidirectional-dynamic-rnn-none-val ...

  9. 内存卡(TF或其它)的标准

    内存卡(TF或其它)的标准 来源 https://zh.wikipedia.org/wiki/SDXC 一般的内存卡是SD2.0的规范的,就是 Class10(10M/sec)或低于 Class10 ...

随机推荐

  1. Spark1.6 Idea下远程调试

    使用的开发工具:scala2.10.4.Idea16.JDK8 1.导入依赖的包和源码 在下载Spark中到的导入spark的相关依赖了包和其源码.zip,相关源码的下载地址:https://gith ...

  2. zclip复制到剪切板插件有个bug

    今天发现zclip复制到剪切板插件有个bug,就是在遨游和360浏览器的兼容模式下,点击复制没响应,后来我看了页面代码,发现在这两个浏览器的兼容模式下,生成的是<object>,其他浏览器 ...

  3. Mybatis 接口绑定

    MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...

  4. Unity的JIT和AOT实现

    https://myhloli.com/about-il2cpp.html JIT方式: Unity的跨平台技术是通过一个Mono虚拟机实现的.而这个虚拟机更新太慢,不能很好地适应众多的平台. And ...

  5. C++并发编程 02 数据共享

    在<C++并发编程实战>这本书中第3章主要将的是多线程之间的数据共享同步问题.在多线程之间需要进行数据同步的主要是条件竞争. 1  std::lock_guard<std::mute ...

  6. mysql 不能插入中文和显示中文

    一)不能显示中文解决办法: 参考:http://bbs3.chinaunix.net/thread-880131-1-1.html 1:windows平台,因为windows操作系统默认的是用了gb2 ...

  7. ajaxGet 获取封装

    callback 表示下一个功能(回调函数) function ajaxGet(url,callback,data){           如果路径上有参数  就在url后面拼接参数 否则只请求url ...

  8. python(十二)下:ORM框架SQLAlchemy使用学习

    此出处:http://blog.csdn.net/fgf00/article/details/52949973 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 ...

  9. [Illuminate\Database\QueryException] SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas sword: NO) (SQL: select * from information_schema.tables where table_schema = la

    [Illuminate\Database\QueryException] SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost ...

  10. mysql开启查询日志功能

    1.开启查询日志  https://www.cnblogs.com/kerrycode/p/7130403.html MYsql 查询日志配置    mysql> show variables ...