tensorflow中有两个关于variable的op,tf.Variable()tf.get_variable()下面介绍这两个创建变量函数的区别

先来看看这两个函数的参数列表,就不打了,直接截图:

首先有一个区别非常明显:

   (1) tf.Variable() 初始化是直接传入initial_value , 我们使用的时候一般是这样子初始化的:

 
a = tf.Variable(initial_value=tf.random_normal(shape=[200, 100], stddev=0.1), trainable=True)

(2)tf.get_variable()初始化是传入一个initializer:

   

b = tf.get_variable(name = 'weights', shape=[200, 100], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1))

当然,明显的区别明显都能看出来,重点是下面这个区别

    使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错

import tensorflow as tf
w_1 = tf.Variable(3,name="w_1")
w_2 = tf.Variable(1,name="w_1")
print w_1.name
print w_2.name
#输出
#w_1:0
#w_1_1:0
import tensorflow as tfw_1 = tf.get_variable(name="w_1",initializer=1)
w_2 = tf.get_variable(name="w_1",initializer=2)
#错误信息
#ValueError: Variable w_1 already exists, disallowed. Did
#you mean to set reuse=True in VarScope?

所以当我们需要共享变量的时候,需要使用tf.get_variable()。在其他情况下,这两个的用法是一样的。为了方便变量管理,tensorflow 还有一个变量管理器,叫做tf.variable_scope ,举个栗子:

import tensorflow as tfwith tf.variable_scope("scope1"): # scopename is scope1
    w1 = tf.get_variable("w1", shape=[])w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):w1_p = tf.get_variable("w1", shape=[])w2_p = tf.Variable(1.0, name="w2")print(w1 is w1_p, w2 is w2_p)  # True False

这就是这两个函数的区别了,在构建网络的时候用起来,你就能够更加深入的了解他们的区别。

												

tf.Variable和 tf.get_variable区别(1)相关推荐

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

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

  2. TF:tensorflow框架中常用函数介绍—tf.Variable()和tf.get_variable()用法及其区别

    TF:tensorflow框架中常用函数介绍-tf.Variable()和tf.get_variable()用法及其区别 目录 tensorflow框架 tensorflow.Variable()函数 ...

  3. tf.Variable 和 tf.get_variable的区别(2)

    上上篇博文也写了这个话题,这次自己又敲了一下代码,再次研究了一下关于tf.Variable() 和 tf.get_variable() 的区别, 我就先不说太多,先直接看看代码,再来总结分析,下面代码 ...

  4. tf.Variable()、tf.get_variable()

    - tf.Variable() W = tf.Variable(<initial-value>, name=<optional-name>) 用于生成一个初始值为initial ...

  5. tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope

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

  6. tf.Variable、tf.get_variable、tf.variable_scope、tf.name_scope、random、initializer

    转自:tensorflow学习笔记(二十三):variable与get_variable TF.VARIABLE.TF.GET_VARIABLE.TF.VARIABLE_SCOPE以及TF.NAME_ ...

  7. tf.Variable() 和 tf.get_variable(),tf.name_scope() 和 tf.variable_scope()

    在gpu并行训练网络时,往往需要共享已创建的变量,tensorflow中为了使这些变量名和操作名唯一并且不重复,用了几个函数来应对这种情况.于是就有了tf.Variable(), tf.get_var ...

  8. TensorFlow学习笔记(一): tf.Variable() 和tf.get_variable()详解

    对于tf.Variable和tf.get_variable,这两个都是在我们训练模型的时候常遇到的函数,我们首先要知道懂得它的语法格式.常用的语法格式的作用以及在实际代码中是如何调用.如何运行的,运行 ...

  9. tensorflow 变量及命名空间 tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope()

    tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope() 1. 基础功能 1.1 tf.Variable() ...

最新文章

  1. Django2.0——模板渲染(一)
  2. IE8 CSS hack
  3. 浅析网站建设基本流程
  4. 通过History Trends Unlimited通过统计台式机Edge浏览器Top10网页历史访问量(截止至2021.11.23)
  5. 当identity_insert 设置为off时不能为表_硬盘跳线怎么设置 硬盘跳线设置方法【详解】...
  6. web服务器-nginx
  7. 玩转Google开源C++单元测试框架Google Test系列(gtest)之六 - 运行参数
  8. left join 和inner join关联查询区别
  9. POJ 2888 Magic Bracelet ——Burnside引理
  10. C++对类(或者结构体)中字符数组赋值时,出现表达式必须是可修改的左值的问题
  11. objective-c NSMutableAttributedString
  12. Atitit cs计算机科学概论 艾提拉解读版 2. 第二部分 信息层 4 5. 第三部分 硬件层 5 8. 第四部分 程序设计层 7 13. 第五部分 操作系统层 10 16. 第六部分 应
  13. QTableWidget设置Item不可编辑状态
  14. u-boot源码个别分析
  15. MyEclipse更换背景豆沙绿
  16. 樊登读书赋能读后感_樊登读书会读后感01012019
  17. 《谷歌大数据经典论文读后感》
  18. 不死僵尸木马lpt7.asp.asp与lpt5.cnzzz.asp的删除方法
  19. Android自定义视频播放器(三)
  20. centos系统mysql数据库搭建教程

热门文章

  1. Dynamo:亚马逊的高可用键值存储
  2. C++ 动态二维数组(二维vector)
  3. 新知客:现代“买椟还珠”故事
  4. linux-文件目录类
  5. 关于ensp配置出现 Error: Please renew the default configurations.
  6. 爬取某瓣电影中你好,李焕英电影的短评并生成词云
  7. Docker Compose编排(写法格式及实验)
  8. 跳转语句_C/C++中的goto语句
  9. STP/RSTP/MSTP协议简介
  10. crontab 每5分钟_Crontab安装步骤和命令使用详细解说