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