tensorflow随笔-新的计算图
生成新的计算图,并完成常量初始化,在新的计算 图中完成加法计算
import tensorflow as tfg1=tf.Graph()with g1.as_default():value=[1.,2.,3.,4.,5.,6.]init = tf.constant_initializer(value)x=tf.get_variable("x",initializer=init,shape=[2,3])y=tf.get_variable("y",shape=[2,3],initializer=tf.ones_initializer())result=tf.add(x,y,name="myadd")with tf.Session(graph=g1) as sess:tf.global_variables_initializer().run()with tf.variable_scope("",reuse=True):print(sess.run(tf.get_variable("x")))print(sess.run(tf.get_variable("y")))print(sess.run(result))
A)tf.Graph.as_default()会创建一个新图,这个图成为当前线程的默认图。
B)在相同进程中创建多个计算图使用tf.Graph.as_default()。如果不创建新的计算图,默认的计算图将被自动创建。
C)如果创建一个新线程,想使用该线程的默认计算图,使用tf.Graph.as_default(),这个函数返回一个上下文管理器( context manager),它能够在这个上下文里面覆盖默认的计算图。在代码务必使用with。
# -*- coding: utf-8 -*-"""Spyder Editor生成新的计算图,并完成常量初始化[代码1]myhaspl@myhaspl.com"""import tensorflow as tfg = tf.Graph()with g.as_default():c = tf.constant(5.0)assert c.graph is gprint "ok"
[代码2]
# -*- coding: utf-8 -*-"""Spyder Editor生成新的计算图,并完成常量初始化myhaspl@myhaspl.com"""import tensorflow as tfwith tf.Graph().as_default() as g:c = tf.constant(5.0)assert c.graph is gprint "ok"
[代码3]# -*- coding: utf-8 -*-"""Spyder Editor生成新的计算图,并完成常量初始化myhaspl@myhaspl.com4"""import tensorflow as tfwith tf.Graph().as_default() as g:c = tf.constant(5.0)assert c.graph is gprint "ok"sess=tf.Session(graph=g)print sess.run(c)sess.close()
[代码4]
# -*- coding: utf-8 -*-"""Spyder Editor生成新的计算图,并完成常量初始化myhaspl@myhaspl.com"""import tensorflow as tfg=tf.get_default_graph()#默认计算图会自动注册c = tf.constant(4.0)result=c*cassert result.graph is g#验证是否result操作属于g这个计算图print "ok1"with tf.Graph().as_default() as g1:c = tf.constant(5.0)assert c.graph is g1print "ok2"assert c.graph is gprint "ok3"sess=tf.Session(graph=g1)print sess.run(c)sess.close()
运行:
输出验证失败
ok1
ok2
…
assert c.graph is g
AssertionError
…
[代码5]
# -*- coding: utf-8 -*-"""Spyder Editor生成新的计算图,并完成常量初始化,在新的计算 图中完成加法计算myhaspl@myhaspl.com"""import tensorflow as tfg1=tf.Graph()with g1.as_default():value=[1.,2.,3.,4.,5.,6.]init = tf.constant_initializer(value)x=tf.get_variable("x",initializer=init,shape=[2,3])y=tf.get_variable("y",shape=[2,3],initializer=tf.ones_initializer())result=tf.add(x,y,name="myadd")assert result.graph is g1#验证是否result操作属于g1这个计算图print "ok"with tf.Session(graph=g1) as sess:tf.global_variables_initializer().run()with tf.variable_scope("",reuse=True):print(sess.run(tf.get_variable("x")))print(sess.run(tf.get_variable("y")))print(sess.run(result))
tensorflow随笔-新的计算图相关推荐
- tensorflow随笔-底层梯度
(1)tf.AggregationMethod是一个类 Class AggregationMethod 类拥有的方法主要用于聚集梯度 计算偏导数需要聚集梯度贡献,这个类拥有在计算图中聚集梯度的很多方法 ...
- tensorflow随笔——C++工程化
本文主要描述如何基于Tensroflow从线下训练到线上预测的整个流程.主要包括以下几步: 使用Python接口搭建模型训练参数并固化成pb文件 使用Bazel编译Tensorflow源码得到tens ...
- 第一篇随笔——新的开端
新的开端 这是我第一次开通博客也是第一次在博客上写随笔,这既是博客的新开端也是JAVA学习的新开端,希望能好好经营这第一个博客~. 觉得自己专业吗?对专业的期望 对于这个问题我不得不坦然承认虽然进入信 ...
- 一周焦点 | 李飞飞离职谷歌;Facebook推OCR神器Rosetta;TensorFlow开源新库TFDV
▌业界焦点 1.阿里从来不只属于马云,但马云会永远属于阿里 9 月 10 日教师节,阿里巴巴集团创始人马云发出题为"教师节快乐"的公开信宣布:一年后的阿里巴巴 20 周年之际,即 ...
- AI一分钟 | 蔚来赴美IPO,开盘跌破发行价;TensorFlow开源新库TFDV
图片来源:腾讯汽车 ▌蔚来汽车挂牌纽交所,12 位车主"替"李斌敲了钟 美国当地时间9月12日上午9点,在提交招股书不到一个月的时间后,蔚来汽车正式挂牌纽交所,股票代码为「NIO」 ...
- 一个TensorFlow的新模型,Kaparthy小哥用了都说好
昨天午饭时分,营长注意到Kaparthy小哥(特斯拉AI主管.马斯克红人.李飞飞高徒)的Twitter活跃了起来: 大意是说,Google一周前给TensorFlow增加的新模型NASNet让人很是兴 ...
- 【tensorflow】重置/清除计算图
转自:https://blog.csdn.net/u014636245/article/details/84073239,侵删 当在搭建网络查看计算图时,如果重复运行程序会导致重定义报错.为了可以在同 ...
- tensorflow随笔-队列管理器QueueRunner-生产者与消费者
# -*- coding: utf-8 -*- """ Spyder EditorThis is a temporary script file. "" ...
- tensorflow随笔-队列(1)
tf.FIFOQueue Class FIFOQueue 先进先出的队列 属性 dtypes 队列元素的每个组件的dtypes列表 name 队列名字 基础队列的名称. names 队列元素的每个组件 ...
最新文章
- Android 多媒体综述
- 划分微型计算机的标志是,划分微型计算机的标志为
- Java中Image类与ImageIcon类的区别
- OVS packet处理流程(三十二)
- 【复杂网络】图模型在欺诈检测应用一点看法
- ubuntu挂载windows下的文件目录的步骤
- Qt 中Treewidget添加右键菜单
- MySQL报错this is incompatible with sql_mode=only_full_group_by
- 软件测试——Junit的使用
- C# WebService 上传图片
- mysql存储php数组_mysql数据库存储PHP数组、对象的方法
- python定时器 中止_定时器Timer如何终止运行的问题
- 光棍节程序员闯关秀 小游戏
- [转]oracle性能调优之--Oracle 10g AWR 配置
- 如何将QQ群文件快速下载,怎么快速下载QQ中的文件,QQ群文件下载
- fluent使用DPM模型计算出的颗粒沉积(trap)数据(.dpm格式)后处理python实现
- 2.1 linux 存储结构和硬盘划分
- 鸿蒙分期靠谱吗,花呗借呗“陷阱”正式被确认,这两个禁忌不要触碰,别说没提醒你...
- linux加新的硬盘,linux添加新硬盘
- vue动态设置路由重定向