tf.variable_scope可以让不同命名空间中的变量取相同的名字,无论tf.get_variable或者tf.Variable生成的变量

tf.name_scope具有类似的功能,但只限于tf.Variable生成的变量

import tensorflow as tf;
import numpy as np;
import matplotlib.pyplot as plt;    with tf.variable_scope('V1'):  a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))  a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.variable_scope('V2'):  a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))  a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')  with tf.Session() as sess:  sess.run(tf.initialize_all_variables())  print a1.name  print a2.name  print a3.name  print a4.name
输出:
V1/a1:0
V1/a2:0
V2/a1:0
V2/a2:0
import tensorflow as tf;
import numpy as np;
import matplotlib.pyplot as plt;    with tf.name_scope('V1'):  a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))  a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.name_scope('V2'):  a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))  a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')  with tf.Session() as sess:  sess.run(tf.initialize_all_variables())  print a1.name  print a2.name  print a3.name  print a4.name
报错:Variable a1 already exists, disallowed. Did you mean to set reuse=True in VarScope?
import tensorflow as tf;
import numpy as np;
import matplotlib.pyplot as plt;    with tf.name_scope('V1'):  # a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))  a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.name_scope('V2'):  # a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))  a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')  with tf.Session() as sess:  sess.run(tf.initialize_all_variables())  # print a1.name  print a2.name  # print a3.name  print a4.name
输出:
V1/a2:0
V2/a2:0

作者:Perry_Wu
链接:https://www.jianshu.com/p/3d2ff00edcef
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

tf.variable_scope和tf.name_scope的用法相关推荐

  1. 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_ ...

  2. tf.variable_scope() and tf.name_scope()

    https://blog.csdn.net/UESTC_C2_403/article/details/72328815 tf.variable_scope可以让变量有相同的命名,包括tf.get_va ...

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

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

  4. tf.variable_scope与tf.tf.get_variable

    实验一. 不设置随机种子,使用不同的初始化方法 import tensorflow as tf; import numpy as np; import matplotlib.pyplot as plt ...

  5. tf.name_scope() 和 tf.variable_scope() 的用法和玄机

    2019独角兽企业重金招聘Python工程师标准>>> 一. name_scope 和 variable_scope的用途 用途1: 共享变量 TensorFlow (TF) 中,n ...

  6. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记: tf.name_scope ...

  7. tf.name_scope()与tf.variable_scope()

    TensorFlow的tf.name_scope().tf.variable_scope()是两个作用域函数,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable ...

  8. tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope('scope_name'). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...

  9. TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习

    转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope('scope_name'). tf ...

最新文章

  1. Android EditText 编辑框 获取焦点的方法
  2. 吊炸天的 PHP 7 ,你值得拥有
  3. c# 获取machineguid_C#中怎么生成和获取GUID
  4. Maven的简单配置说明
  5. git 在 A 项目中引用 B 项目
  6. 求十个学生的平均成绩java,JAVA 声明一个数组,存一个学生的五门成绩。求该学生的总成绩、平均成绩。...
  7. OSI七层模型都是什么?
  8. python单向循环链表_python实现单向循环链表 -- 详细思路分析
  9. 你写的代码要被 GitHub 存在北极啦!期限是 1000 年!
  10. (二)html 复杂表格
  11. Adobe dreamweaver CS6小白入门教程
  12. 数据库原理与应用学习笔记(一)
  13. Spring Boot Actuator自定义健康检查
  14. Android JVM和DVM的区别
  15. 简单的车票管理系统(c语言)
  16. 算法:分治法之合并排序
  17. Linux服务篇之远程访问及控制SSH
  18. java代码实现瑟夫环问题
  19. CodeSmith 4 Release 注册破解序列号注册码方法
  20. 中国第一高楼封顶:596.5米!世界第二

热门文章

  1. Python丢弃返回值
  2. 13道Python3实例
  3. python 获取本机IP的三种方式
  4. python3循环遍历嵌套字典替换指定值
  5. android 长按缩放拖动_十年Android之路面试2000人,面试准备+内部泄露核心题(中高级)...
  6. mysql语句的执行顺序_SQL语句完整的执行顺序(02)
  7. C语言socket connect()函数(初始化套接字上的连接)(未完)(如何测试socket是否已经断开,如何判断socket是否断开)
  8. 电脑物理内存与虚拟内存的区别与关系
  9. 放置奇兵 算法 月度活动 破碎时空记录 第五关 丁丁(瓦伦丁)+杰赫拉
  10. pycharm提醒:PEP 8: invalid escape sequence xx 解决办法 (转义序列无效,需改成双反斜杠\\)