import tensorflow as tf# variable_scope()示例
"""
tensorflow中通过变量名称获取变量的机制主要是通过tf.get_variable和tf.variable_scope函数实现的
tf提供tf.get_variable函数来创建或获取变量;当tf.get_variable用于创建变量时,它和tf.Variable的功能基本等价
"""
# tf.get_variable函数调用时提供维度信息和初始化方法,tf.get_variable函数与tf.Variable函数最大区别是:tf.Variable函数中变量名称是一个可选参数
# tf.get_variable函数中的变量名称是一个必填参数,tf.get_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"))
# 为了解决出现变量复用造成的tf.get_variable错误,需要通过tf.variable_scope()函数生成一个上下文管理器,并明确指出在这个上下文管理器中,
# tf.get_variable()将获取已经生成的变量
with tf.variable_scope("foo"):v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))
"""
由于命名空间foo中已经存在名为v的变量,所以下面代码将会报错
with tf.variable_scope("foo"):v = tf.get_variable("v", [1])
"""
# 在生成上下文管理器时,将参数reuse设置为True,这样tf.get_variable函数将直接获取已经声明的变量
with tf.variable_scope("foo", reuse=True):v1 = tf.get_variable("v", [1])print(v1 == v)  # v,v1代表的是相同的tf中的变量
# 将reuse设置为True时,tf.variable_scope将只能获取已经创建过的变量,因为在bar命名空间中,还没有创建过变量v,所以报错
"""
with tf.variable_scope("bar", reuse=True):v = tf.get_variable("v",[1])
"""
# 如果tf.variable_scope()函数使用参数reuse=None,reuse=False创建上下文管理器时,tf.get_variable操作将会创建新的变量,如果同名变量已存在,将会报错
# tf.variable_scope()函数是可以嵌套的
with tf.variable_scope("root"):print(tf.get_variable_scope().reuse)with tf.variable_scope("foo", reuse=True):print(tf.get_variable_scope().reuse)with tf.variable_scope("bar"):print(tf.get_variable_scope().reuse)print(tf.get_variable_scope().reuse)
# tf.variable_scope()函数除了可以控制tf.get_variable执行功能之外,也提供了一个管理变量命名空间的方式
with tf.variable_scope("hi"):v1 = tf.get_variable("v", [1])print(v1.name)print(tf.get_variable_scope().reuse)with tf.variable_scope("hi"):with tf.variable_scope("bar"):v2 = tf.get_variable("v", [1])print(v2.name)v4 = tf.get_variable("v1", [1])  # 此处的变量名一定不能是vprint(v4.name)with tf.variable_scope("", reuse=True):v5 = tf.get_variable("hi/bar/v", [1])print(v5 == v2)v6 = tf.get_variable("hi/v1", [1])print(v6 == v4)# name_scope()在可视化过程中,为变量划分范围,表示计算图中的一个层级,不影响get_variable创建的变量,只会影响Variable()创建的变量
"""
name_scope 对用get_variable()创建的变量的名字不会有任何影响,而 Variable()创建的操作会被加上前缀,并且会给操作加上名字前缀
"""
with tf.variable_scope("fooo"):with tf.name_scope("bar"):v = tf.get_variable("v", [1])b = tf.Variable(tf.zeros([1]), name="b")x = 1.0 + vprint(v.name)print(b.name)print(x.op.name)

tensorflow中的变量管理相关推荐

  1. 《TensorFlow:实战Google深度学习框架》——5.3 TensorFlow中的变量初始化函数

  2. tensorflow中张量、常量、变量、占位符

    引言 从实例出发 #先导入TensorFlow import tensorflow as tf# Create TensorFlow object called hello_constant hell ...

  3. TensorFlow中变量管理reuse参数的使用

    TensorFlow用于变量管理的函数主要有两个: tf. get_variable()和tf.variable_scope(), 前者用于创建或获取变量的值,后者用于生成上下文管理器,创建命名空间, ...

  4. tensorflow中name_scope和variable_scope变量的使用

    1. variable_scope的使用 首先,使用variable_scope可以很方便的管理get_varibale. 如何确定 get_variable 的 prefixed name? 1.1 ...

  5. 【TensorFlow】TensorFlow从浅入深系列之八 -- 教你学会变量管理

    本文是<TensorFlow从浅入深>系列之第8篇 TensorFlow从浅入深系列之一 -- 教你如何设置学习率(指数衰减法) TensorFlow从浅入深系列之二 -- 教你通过思维导 ...

  6. Tensorflow 之 name/variable_scope 变量管理

    name/variable_scope 的作用 充分理解 name / variable_scope TensorFlow 入门笔记 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来 ...

  7. TensorFlow中查看checkpoint文件中的变量名和对应值

    在加载模型时, 需要知道checkpoint中变量名称,一下代码可以查看TensorFlow中checkpoint文件中的变量名: #!/usr/bin/env python # -*- coding ...

  8. android冻结命令,在Android上使用冻结tensorflow图中的变量

    TLDR:如何在Android上使用冻结tensorflow图中的变量? 1.我想做什么 我有一个Tensorflow模型,它在多个变量中保持一个内部状态,用:state_var = tf.Varia ...

  9. Tensorflow 获取model中的变量列表,用于模型加载等

    目录 前言 1. 用tensorflow自带的工具 2. 用tensorflow.contrib.slim. 3. 从保存的model中提取var_list 4. 其他 前言 在加载预训练的网络模型时 ...

最新文章

  1. 简单的分级别写日志程序
  2. 最强写作AI竟然学会象棋和作曲,语言模型跨界操作引热议,在线求战
  3. java中wait方法使用实例_java中wait、notify和notifyAll的概念用法和例子?
  4. 联想手机android系统耗电,联想 K900 Android 4.2 手机续航能力实测
  5. layui表单加文件 php_layui框架实现文件上传及TP3.2.3(thinkPHP)对上传文件进行后台处理操作示例...
  6. Java 利用 split 方法切割字符串
  7. css 背景图 左右空白,缩小窗口时CSS背景图出现右侧空白BUG的解决方法
  8. java电力巡检项目讲解,电力巡检-东软平台产品官网
  9. Android 如何隐藏应用程序的图标
  10. max_prepared_transactions设置
  11. 【5G UP】5G QoS参数那点事儿
  12. 易泊夏武离线车牌识别,车牌识别SDK
  13. 50句哲理 语录(二)
  14. Facebook Haystack 管理百亿照片
  15. 创智汇集,汉韵流芳!大创智国风汉服赏与您相约十月
  16. 论逻辑思维和理解能力对程序员的重要性
  17. php iofactory列,PhpSpreadsheet中文文档 | Spreadsheet操作教程实例
  18. 【云原生】Kubernetes PDB(Pod Disruption Budget)介绍与简单使用
  19. SCCM 2007 R2 setp by setp(五)-SQL的安装
  20. 移动支付系统贴牌,自由对接支付宝微信或者银行等

热门文章

  1. 【译】Swift算法俱乐部-查找最大/最小值
  2. com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1问题出现的原因及解决办法
  3. JavaScript 异步编程--Generator函数、async、await
  4. 从信息熵到Codec
  5. 一道百度算法面试题讲解
  6. 开源wkhtmltopdf使用心得 (二)
  7. 2014 百度之星题解 1002 - Disk Schedule
  8. ntoskrnl损坏
  9. 手机吸费软件肆意捞金,通过代码签名证书加以防范
  10. 学习 ARM 系列 -- FS2410 开发板上 Nand Flash 到内存的代码搬移