tensorflow中创建多个计算图(Graph)
tf程序中,系统会自动创建并维护一个默认的计算图,计算图可以理解为神经网络(Neural Network)结构的程序化描述。如果不显式指定所归属的计算图,则所有的tensor和Operation都是在默认计算图中定义的,使用tf.get_default_graph()函数可以获取当前默认的计算图句柄。
# -*- coding: utf-8 -*-)
import tensorflow as tf
a=tf.constant([1.0,2.0])
b=tf.constant([1.0,2.0])
result = a+b
print(a.graph is tf.get_default_graph()) # 输出为True,表示tensor a 是在默认的计算图中定义的
print(result.graph is tf.get_default_graph()) # 输出为True, 表示 Operation result 是在默认的计算图中定义的
print 'a.graph = {0}'.format(a.graph)
print 'default graph = {0}'.format(tf.get_default_graph())
输出:
True
True
a.graph = <tensorflow.python.framework.ops.Graph object at 0x7f0480c9ca90>
default graph = <tensorflow.python.framework.ops.Graph object at 0x7f0480c9ca90>
tf中可以定义多个计算图,不同计算图上的张量和运算是相互独立的,不会共享。计算图可以用来隔离张量和计算,同时提供了管理张量和计算的机制。计算图可以通过Graph.device函数来指定运行计算的设备,为TensorFlow充分利用GPU/CPU提供了机制。
- 使用 g = tf.Graph()函数创建新的计算图;
- 在with g.as_default():语句下定义属于计算图g的张量和操作
- 在with tf.Session()中通过参数 graph = xxx指定当前会话所运行的计算图;
- 如果没有显式指定张量和操作所属的计算图,则这些张量和操作属于默认计算图;
- 一个图可以在多个sess中运行,一个sess也能运行多个图
创建多个计算图:
# -*- coding: utf-8 -*-)
import tensorflow as tf
# 在系统默认计算图上创建张量和操作
a=tf.constant([1.0,2.0])
b=tf.constant([2.0,1.0])
result = a+b
# 定义两个计算图
g1=tf.Graph()
g2=tf.Graph()
# 在计算图g1中定义张量和操作
with g1.as_default():
a = tf.constant([1.0, 1.0])
b = tf.constant([1.0, 1.0])
result1 = a + b
with g2.as_default():
a = tf.constant([2.0, 2.0])
b = tf.constant([2.0, 2.0])
result2 = a + b
# 在g1计算图上创建会话
with tf.Session(graph=g1) as sess:
out = sess.run(result1)
print 'with graph g1, result: {0}'.format(out)
with tf.Session(graph=g2) as sess:
out = sess.run(result2)
print 'with graph g2, result: {0}'.format(out)
# 在默认计算图上创建会话
with tf.Session(graph=tf.get_default_graph()) as sess:
out = sess.run(result)
print 'with graph default, result: {0}'.format(out)
print g1.version # 返回计算图中操作的个数
输出:
with graph g1, result: [ 2. 2.]
with graph g2, result: [ 4. 4.]
with graph default, result: [ 3. 3.]
3
tensorflow中创建多个计算图(Graph)的更多相关文章
- Tensorflow中的图(tf.Graph)和会话(tf.Session)详解
Tensorflow中的图(tf.Graph)和会话(tf.Session) Tensorflow编程系统 Tensorflow工具或者说深度学习本身就是一个连贯紧密的系统.一般的系统是一个自治独立的 ...
- tensorflow中有向图(计算图、Graph)、上下文环境(Session)和执行流程
计算图(Graph) Tensorflow是基于图(Graph)的计算框架,图的节点由事先定义的运算(操作.Operation)构成,图的各个节点之间由张量(tensor)来链接,Tensorflow ...
- TensorFlow 中的张量,图,会话
tensor的含义是张量,张量是什么,听起来很高深的样子,其实我们对于张量一点都不陌生,因为像标量,向量,矩阵这些都可以被认为是特殊的张量.如下图所示: 在TensorFlow中,tensor实际上就 ...
- tensorflow中slim模块api介绍
tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35 http://blog.csdn.net/guvcolie/article/details/77686 ...
- TensorFlow中的设备管理——Device的创建与注册机制
背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 作为一款优秀的异构深度学习算法框架,TensorFlow可以在多种设备上运行算 ...
- TensorFlow中的Session、Graph、operation、tensor
TensorFlow中的Session.Graph.operation.tensor
- TensorFlow中的Placement启发式算法模块——Placer
背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 受限于单个Device的计算能力和存储大小,许多深度学习模型都有着使用模型分片 ...
- Tensorflow中的run()函数
1 run()函数存在的意义 run()函数可以让代码变得更加简洁,在搭建神经网络(一)中,经历了数据集准备.前向传播过程设计.损失函数及反向传播过程设计等三个过程,形成计算网络,再通过会话tf.Se ...
- [翻译] Tensorflow中name scope和variable scope的区别是什么
翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...
随机推荐
- 搭建backup服务器基本流程
守护进程实现,将daemon配置在backup服务器,因为这样其他服务器就能通过服务推即可. 服务端配置流程: 前提两台服务41为backup服务 31是其他服务器即客户端 在41服务器中配置 ...
- Python操作SQLAlchemy
Mysql环境: MySQL 一.概述什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 什么是 MySQL.Oracle.SQLite.Access.MS ...
- OJ 1101 谁是中间的那个
前言:主要考察排序用法 sort(cow+1,cow+1+n,cmp);//数组按cmp方法排序 Description 一天,农夫乔伊像往常一样来到了他的牧场,他突然对他的奶牛产奶量产生了兴趣.他想 ...
- uboot相关的几篇好文
http://www.eeworld.com.cn/mcu/2015/0727/article_21246.html http://blog.csdn.net/kernel_yx/article/de ...
- Docker容器技术-优化Docker镜像
一.优化Docker镜像 1.降低部署时间 一个大的Docker应用是如何影响在新Docker宿主机上的部署时间. (1)编写Dockerfile创建一个大Docker镜像 [root@bogon ~ ...
- list— 把数组中的值赋给一组变量
(PHP 4, PHP 5, PHP 7) list — 把数组中的值赋给一组变量 array list ( mixed $var1 [, mixed $... ] ) 像 array() 一样,这不 ...
- Linux挂载第二块硬盘操作方法
远程SSH登录上Centos服务器后,进行如下操作 提醒:挂载操作会清空数据,请确认挂载盘无数据或者未使用 第一步:列出所有已挂载磁盘 命令: disk -h [root@gluster_node1 ...
- SwfUpload文件上传
SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大.以前在项目中用过几次,但它的配置参数太多了,用过后就忘记怎么用了,到以后要用时又得到官网上看它的文档,真是太烦了.所以 ...
- java filter 实现权限控制
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...
- 泛型学习第三天——C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
定义一个类: public class UserInfo { public System.Guid ID { get; set; } public string LoginName ...