Tensorflow结点打包和依赖控制
深度学习库能够充分发挥GPU并行计算的能力,但是有时我们却不得不需要串行。这时就需要用到依赖控制。
import tensorflow as tf
a = tf.Variable(1)
b = tf.Variable(2)
s = tf.add(a, b)
asiggn = tf.assign(a, 4)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run([a, b, s, asiggn]))
理论上,这段程序有时输出6,有时输出3。也就是说,求和操作和复制操作无法确定谁先执行。
如果想要先求和再赋值,那么需要使用依赖控制指明依赖。
import tensorflow as tf
a = tf.Variable(1)
b = tf.Variable(2)
s = tf.add(a, b)
with tf.control_dependencies([s]):
asiggn = tf.assign(a, 4)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run([a, b, s, asiggn]))
每一次sess.run,每个结点只求解一次。
然而需要理解的一个关键点是:
将多个结点打包之后,对这个包加上依赖控制,并不会影响被打包的各个结点的依赖。
# 将结点打包,同时执行多个结点
import tensorflow as tf
tf.reset_default_graph()
a = tf.Variable(1, name="a")
b = tf.Variable(2, name="b")
c = tf.Variable(3, name="c")
with tf.control_dependencies([a, b, c]):
before_sum = tf.add_n([a, b, c])
a_add1 = tf.assign(a, tf.add(a, 1, name='a_add1'))
b_add2 = tf.assign(b, tf.add(b, 2, name='b_add2'))
c_add3 = tf.assign(c, tf.add(c, 3, name='c_add3'))
with tf.control_dependencies([before_sum]):
# group操作run之后返回值为None,它只负责同时执行,它并不负责控制依赖
op = tf.group(a_add1, b_add2, c_add3)
with tf.control_dependencies([op]): # 如果没有这句话,则sum操作和op操作是并行的,导致出现奇怪的现象
after_sum = tf.add_n([a, b, c])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run([before_sum, op, a, b, c, after_sum]))
输出为:[12, None, 2, 4, 6, 12]
表示beforesum并没有在op之前执行。
要想在op之前求before_sum,那就需要为op中的每个结点添加依赖
# 将结点打包,同时执行多个结点
import tensorflow as tf
tf.reset_default_graph()
a = tf.Variable(1, name="a")
b = tf.Variable(2, name="b")
c = tf.Variable(3, name="c")
with tf.control_dependencies([a, b, c]):
before_sum = tf.add_n([a, b, c])
with tf.control_dependencies([before_sum]):
a_add1 = tf.assign(a, tf.add(a, 1, name='a_add1'))
with tf.control_dependencies([before_sum]):
b_add2 = tf.assign(b, tf.add(b, 2, name='b_add2'))
with tf.control_dependencies([before_sum]):
c_add3 = tf.assign(c, tf.add(c, 3, name='c_add3'))
with tf.control_dependencies([before_sum]):
# group操作run之后返回值为None,它只负责同时执行,它并不负责控制依赖
op = tf.group(a_add1, b_add2, c_add3)
with tf.control_dependencies([op]): # 如果没有这句话,则sum操作和op操作是并行的,导致出现奇怪的现象
after_sum = tf.add_n([a, b, c])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run([before_sum, op, a, b, c, after_sum]))
"""
执行顺序不同会出现什么情况
"""
Tensorflow结点打包和依赖控制的更多相关文章
- gradle 打包所有依赖 Invalid signature file digest for Manifest main attributes(转)
1.打包所有依赖: // 指定main函数的类 jar { manifest { attributes "Main-Class": "com.baeldung.fatja ...
- Maven项目中War包的打包及依赖方式
两个web项目之间的依赖引用方式.Web项目之间,通过war包的方式进行引用的.例如,有两个项目,puzzle-web和puzzle-web-demo,两个均是web项目,puzzle-web-dem ...
- sbt的assembly插件使用(打包所有依赖)
1.sbt是什么 对于sbt 我也是小白, 为了搞spark看了一下scala,学习scala时指定的构建工具就是sbt(因为sbt也是用scala开发的嘛),起初在我眼里就是一个maven(虽然ma ...
- 【maven】之打包war依赖子项目jar
比如 p-common p-core p-dao p-service p-web service项目依赖dao,dao依赖core和common,web依赖service 在使用maven tomca ...
- Maven项目打包jar依赖外部jar
有时候我们想要做一些java 的小程序,需要把打包成jar,单独执行,做一个maven项目,maven非常方便,有自动打包成jar的插件,但是有时候我们的项目可能会依赖其他的jar包,所以非常麻烦. ...
- maven打包加入依赖包以及加入本地依赖包的方法
Maven引入本地Jar包并打包进War包中 1.概述 在平时的开发中,有一些Jar包因为种种原因,在Maven的中央仓库中没有收录,所以就要使用本地引入的方式加入进来. 2. 拷贝至项目根目录 项目 ...
- Spring Boot 打包分离依赖 JAR 和配置文件
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding> ...
- maven 打包添加依赖
1.将依赖与自己的代码打入同一个jar包 只需在pom中添加如下plugin 在include 中添加需要的依赖,在exclude 中添加不需要的依赖 <groupId>org.apach ...
- spring boot打包,依赖、配置文件分离,拷贝启动脚本
一.最终打包的目录结构 二.项目结构 三.开始 1.最终打包的目录,可根据自己需要修改. <properties> <mzservice.path>${project.buil ...
随机推荐
- Linux扩展文件分区
**************操作之前请看章节6,看系统是否支持LVM分区管理方式*************** 1:新增磁盘 插入新的磁盘,比如物理机可以直接在卡槽插入,虚拟机可以在控制台添加磁盘或者 ...
- KDiff
BeyondCompare是收费的,用了一段时间不能用了.找到一个 KDiff做对比工具,也很好用. 在这里下载: http://sourceforge.net/projects/kdiff3/fil ...
- thrift系列 - 快速入门
1.简介 Thrift是当前流行的RPC框架之一,它有强大的代码生成引擎,可以跨语言,轻松解决程序间的通信问题. 本文旨在帮助大家快速入门,若想深入原理,请参见thrift官网:h ...
- [Angular] Communicate with Angular Elements using Inputs and Events
In a real world scenario we obviously need to be able to communicate with an Angular Element embedde ...
- artTemplate--使用artTemplate时,由于json对象属性有特殊格式 导致调用报错artTemplate,syntax error,Template Error
我们首先看下面的代码 data = { "siteName" : "西部云谷二期17", "PM10" : "10017" ...
- Redis 操作数据
展现最新数据 Web应用常常要展现最新数据,就会根据时间对数据排序: SELECT * FROM foo WHERE ... ORDER BY time DESC LIMIT 10 随着数据的增加,问 ...
- 微信小程序 - 上传图片(组件)
更新日期: 2019/3/14:首次发布,更新了2018/12/30的UI以及反馈信息获取方式,具体请下载:demo. 2019/3/20:感谢544429676@qq.com指出的现存bug,已修复 ...
- 使用树莓派3获取CPU温度
一.命令: cat /sys/class/thermal/thermal_zone0/temp 二.上图:
- Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists
Add a Column to a table if not exists MySQL allows you to create a table if it does not exist, but d ...
- hdu 1016 Prime Ring Problem (dfs)
一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...