变量作用域

tensorflow提供了变量作用域和共享变量这样的概念,有几个重要的作用。

  • 让模型代码更加清晰,作用分明

变量作用域域

通过tf.variable_scope(<scope_name>)创建指定名字的变量作用域

with tf.variable_scope("itcast") as scope:
print("----")

加上with语句就可以在整个itcast变量作用域下就行操作。

嵌套使用

变量作用域可以嵌套使用

with tf.variable_scope("itcast") as itcast:
with tf.variable_scope("python") as python:
print("----")

变量作用域下的变量

在同一个变量作用域下,如果定义了两个相同名称的变量(这里先用tf.Variable())会怎么样呢?

with tf.variable_scope("itcast") as scope:
a = tf.Variable([1.0,2.0],name="a")
b = tf.Variable([2.0,3.0],name="a")

我们通过tensoflow提供的计算图界面观察

我们发现取了同样的名字,其实tensorflow并没有当作同一个,而是另外又增加了一个a_1,来表示b的图

变量范围

当每次在一个变量作用域中创建变量的时候,会在变量的name前面加上变量作用域的名称

with tf.variable_scope("itcast"):
a = tf.Variable(1.0,name="a")
b = tf.get_variable("b", [1])
print(a.name,b.name)

得道结果

(u'itcast/a:0', u'itcast/b:0')

对于嵌套的变量作用域来说

with tf.variable_scope("itcast"):
with tf.variable_scope("python"):
python3 = tf.get_variable("python3", [1])
assert python3.name == "itcast/python/python3:0"
var2 = tf.get_variable("var",[3,4],initializer=tf.constant_initializer(0.0))

最新文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (32) ------ 第六章 继承与建模高级应用之TPH与TPT (1)
  2. xcode8插件无法使用
  3. -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object&#39; 解决方法
  4. DZNEmptyDataSet,优秀的空白页或者出错页封装
  5. Java线程池应用
  6. JAVA并行框架学习之ForkJoin
  7. C#当中的多线程_线程基础
  8. uva 1378 A Funny Stone Game (博弈-SG)
  9. nginx 阻止非自己域名解析到服务器
  10. 北京U3D外包团队 UE4红军抗战案例 Unity3D红军抗战案例 UE4下载和安装虚幻4游戏引擎
  11. orcal - 分组
  12. mysql 正确清理binlog 删除数据后磁盘空间的
  13. wfp(Application的运用)
  14. Java泛型深入理解(转载)
  15. asp.net url址址中中文汉字参数传递乱码解决方法
  16. VB6学习笔记
  17. 求LCA练习+部分算法复习 2017.1.22
  18. Spring 整合Mybatis 出现了Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create Poola
  19. PyTorch Notes | PyTorch 编程实践笔记
  20. 《Drools7.0.0.Final规则引擎教程》第4章 4.2 no-loop

热门文章

  1. HTML5 canvas绘制arcTo、translate和rotate的画法探索
  2. 转:Uncovering Drupalgeddon 2(cve-2018-7600)漏洞深度解析(附漏洞利用代码地址)
  3. python网页下载
  4. java 读入文件 BufferedReader
  5. Kail Linux渗透测试教程之Recon-NG框架
  6. django 编码错误
  7. misaka and last order SCU - 4489 (筛法的灵活应用)
  8. java8新特性——简介
  9. 莫队p2 【bzoj3809】Gty的二逼妹子序列
  10. setTimeout递归调用跳转页面