import tensorflow as tf
import numpy as np"""
本节内容:1、Graph图的创建。2、使用tf.constant 和 tf.add()的使用3、使用会话,tf.Session()  以及  sess.run()的参数
"""def create_graph():print('当前模型图为:{}'.format(tf.get_default_graph()))# 1、构建2个常量对象a = tf.constant(value=5.0, dtype=tf.float32, shape=None, name='a')b = tf.constant(value=8.0)# 2、使用操作符tf.add 对2个常数分别加一个随机数。v1 = tf.add(x=a, y=np.random.random_sample(), name='v1')v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=42))# 3、对上述2者进行相乘rezult = tf.multiply(x=v1, y=v2)print(v1, v2, rezult)def create_graph1():print('当前模型图为:{}'.format(tf.get_default_graph()))# 1、构建2个常量对象a = tf.constant(value=5.0, dtype=tf.float32, shape=None, name='a')b = tf.constant(value=8.0)# 2、使用操作符tf.add 对2个常数分别加一个随机数。v1 = tf.add(x=a, y=np.random.random_sample(), name='v1')v2 = b + tf.random_normal(shape=[], dtype=tf.float32, seed=42)# 3、对上述2者进行相乘rezult = v1*v2print(v1, v2, rezult)def create_graph2():"""2个矩阵相乘:return:"""# 1、构建2个常量的矩阵a = tf.constant(value=[1,2,3,4,5,6,7,3,2,4,3,4],dtype=tf.float32, shape=[3, 5])b = tf.constant(value=[2,3,4,3,5,5,43,3,2,1,3,4],dtype=tf.float32, shape=[5, 3])# 2、2个常量tensor分别加一个随机数v1 = a + np.random.random_sample()v2 = b + tf.random_normal(shape=[], dtype=tf.float32)# 3、矩阵相乘rezult = tf.matmul(v1, v2)print(v1, v2, rezult)def create_graph3():"""不能跨图操作,否则会报错。:return:"""print('当前的默认图为:{}'.format(tf.get_default_graph()))# 1、构建2个常量的矩阵a = tf.constant(value=[1,2,3,4,5,6,7,3,2,4,3,4],dtype=tf.float32, shape=[3, 5])with tf.Graph().as_default():print('当前的默认图为:{}'.format(tf.get_default_graph()))b = tf.constant(value=[2,3,4,3,5,5,43,3,2,1,3,4],dtype=tf.float32, shape=[5, 3])# 2、2个常量tensor分别加一个随机数v1 = a + np.random.random_sample()v2 = b + tf.random_normal(shape=[], dtype=tf.float32)# 3、矩阵相乘rezult = tf.matmul(v1, v2)print(v1, v2, rezult)def create_graph_do_session():# 1、构建2个常量的矩阵a = tf.constant(value=[1,2,3,4,5,6,7,3,2,4,3,4],dtype=tf.float32, shape=[3, 5])b = tf.constant(value=[2,3,4,3,5,5,43,3,2,1,3,4],dtype=tf.float32, shape=[5, 3])# 2、2个常量tensor分别加一个随机数v1 = a + np.random.random_sample()v2 = b + tf.random_normal(shape=[], dtype=tf.float32)# 3、矩阵相乘rezult = tf.matmul(v1, v2)print(v1, v2, rezult)# 二、构建会话sess = tf.Session()"""tf.Session().run(self, fetches,         # 给定具体要获取哪些tensor的值,可以是一个,也可以是多个。(给定多个,只运行模型图1次。)feed_dict=None,  # 如果模型图中定义了传入的数据(通过占位符定义),那么通过这个参数给定。options=None, run_metadata=None)"""# print(sess.run(v1))print(sess.run(v2))# print(sess.run(rezult))print(sess.run(fetches=[v1, v2, rezult, v2]))sess.close()# fixme RuntimeError: Attempted to use a closed Session.print(sess.run(v2))def create_graph_do_session1():"""第二种执行跑会话的方法。 eval()方法。:return:"""# 1、构建2个常量的矩阵a = tf.constant(value=[1,2,3,4,5,6,7,3,2,4,3,4],dtype=tf.float32, shape=[3, 5])b = tf.constant(value=[2,3,4,3,5,5,43,3,2,1,3,4],dtype=tf.float32, shape=[5, 3])# 2、2个常量tensor分别加一个随机数v1 = a + np.random.random_sample()v2 = b + tf.random_normal(shape=[], dtype=tf.float32)# 3、矩阵相乘rezult = tf.matmul(v1, v2)print(v1, v2, rezult)# 二、构建会话# sess = tf.Session()# print(v2.eval(session=sess))# print(rezult.eval(session=sess))# sess.close()with tf.Session() as sess:print(v2.eval())print(rezult.eval())def create_graph_do_session2():"""第二种构建会话的方式。 tf.InteractiveSession() 交互式会话:return:"""# 1、构建2个常量的矩阵a = tf.constant(value=[1,2,3,4,5,6,7,3,2,4,3,4],dtype=tf.float32, shape=[3, 5])b = tf.constant(value=[2,3,4,3,5,5,43,3,2,1,3,4],dtype=tf.float32, shape=[5, 3])# 2、2个常量tensor分别加一个随机数v1 = a + np.random.random_sample()v2 = b + tf.random_normal(shape=[], dtype=tf.float32)# 3、矩阵相乘rezult = tf.matmul(v1, v2)print(v1, v2, rezult)# 二、构建会话sess = tf.InteractiveSession()print(tf.get_default_session())print(v2.eval())print(rezult.eval())# 总结。使用tf构建模型图,一般的逻辑结构为:
# with tf.Graph().as_default():
#     # 一、基于业务逻辑构建模型图
#     pass
#     # 二、构建会话
#     with tf.Session() as sess:
#         # 1、变量初始化
#         # 2、加载数据。
#         # 3、执行模型训练
#         # 4、模型效果查看(打印 训练 和验证数据的 损失和 准确率)
#         # 5、模型持久化
#         passif __name__ == '__main__':# create_graph3()create_graph_do_session2()
D:\Anaconda\python.exe D:/AI20/HJZ/04-深度学习/2-TensorFlow基础/tf_基础代码/01_01Graph和Session.py
Tensor("add:0", shape=(3, 5), dtype=float32) Tensor("add_1:0", shape=(5, 3), dtype=float32) Tensor("MatMul:0", shape=(3, 3), dtype=float32)
2019-11-30 21:36:01.954243: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
<tensorflow.python.client.session.InteractiveSession object at 0x000001D8CB982D30>
[[ 1.7119892  2.7119892  3.7119892][ 2.7119892  4.7119894  4.7119894][42.71199    2.7119892  1.7119892][ 0.7119892  2.7119892  3.7119892][ 3.7119892  3.7119892  3.7119892]]
[[223.5455   97.34502  99.8936 ][255.7504  140.5499  146.09848][280.09116 119.89066 123.43925]]Process finished with exit code 0

3-Tensorflow-demo_1-Graph和Session相关推荐

  1. list python 转tensor_TensorFlow 中的几个关键概念:Tensor,Operation,Graph,Session

    前言:TensorFlow是一种符号式编程框架,首先要构造一个图(graph),然后在这个图上做运算.打个比方,graph就像一条生产线,session就像生产者.生产线具有一系列的加工步骤(加减乘除 ...

  2. TensorFlow学习笔记1:graph、session和op

    graph即tf.Graph(),session即tf.Session(),很多人经常将两者混淆,其实二者完全不是同一个东西. graph定义了计算方式,是一些加减乘除等运算的组合,类似于一个函数.它 ...

  3. AttributeError: module 'tensorflow' has no attribute 'Session'.解决办法

    # 定义计算图 tens1 = tf.constant([1,2,3])# 创建一个会话 sess = tf.Session()# 使用这个创建好的会话来得到关心的运算的结果.比如可以调用 sess. ...

  4. AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘错误解决

    AttributeError: module 'tensorflow' has no attribute 'Session'错误解决 根据网上教程安装tensorflow环境时,最后都会有个测试代码用 ...

  5. tensorflow 查看graph

    查看tensorflow里面Graph中所有的tensor print([n.name for n in graph.as_graph_def().node])

  6. module 'tensorflow' has no attribute 'Session'

    AttributeError: module 'tensorflow' has no attribute 'Session'错误解决 原创陆羽飞 最后发布于2019-10-07 13:32:07 阅读 ...

  7. tensorflow2.0及以上版本在使用Session模块时报错:AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘ 已解决

    tensorflow2.0版本及之后的版本在使用Session模块时报错:AttributeError: module 'tensorflow' has no attribute 'Session' ...

  8. AttributeError: module 'tensorflow' has no attribute 'Session'错误解决

    Ubuntu刚安装好Tensorflow以后运行 import tensorflow as tf tf.Session() 报错AttributeError: module 'tensorflow' ...

  9. Tensorflow 学习二 tf.Session().run

    以下为tf.Session().run 说明,其接受的fetches参数可以有多种类型. def run(self, fetches, feed_dict=None, options=None, ru ...

  10. DeepLearning | Tensorflow编程基础:Session、Constant、Variable、Tensor、Placeholder、OP

    转载地址https://blog.csdn.net/liangjun_feng/article/details/79723048 Tensorflow的运行方式 Tensorflow作为谷歌开源的深度 ...

最新文章

  1. 使用Linq Xml 来读取一个目录下所有的文件并保存为Xml文档
  2. linux 文件系统_Linux 虚拟文件系统
  3. Elasticsearch2.x Cluster Health
  4. draw.io二次开发(2)文件解读
  5. python 链栈及基本操作
  6. AAAI 2018论文解读 | 基于文档级问答任务的新注意力模型
  7. 被流氓360设置浏览器主页的解决办法(如果你也遇到了跟我一样的问题,不妨看一下是不是这个原因)...
  8. [剑指offer]面试题48:不能被继承的类
  9. U盘流畅运行linux发行版,做各种linux发行版的启动U盘方法
  10. 申请了:苹果已经在测试“毫米波”5G技术
  11. 想要创业有没有过来人能够指导一下
  12. Adobe驳斥Flash过度耗电论 称HTML5更耗电
  13. 台安PLC通过手机热点远程在线模拟,远程编程,远程调试
  14. linux下的c++filt 命令
  15. Python2中使用input出现的NameError: name ‘***‘ is not defined问题原因及解决办法
  16. 单片机应用系统设计技术——数控云台master
  17. python作业网站_怒刷python作业-WEB资讯专栏-DMOZ中文网站分类目录-免费收录各类优秀网站的中文网站目录....
  18. 做一个有时间观念的人
  19. cmake 安装下载
  20. [转]Linux学习路线

热门文章

  1. 雷军演讲全文:我看未来20年
  2. 华为之后,谁能站上国产高端手机“C位”?
  3. 好心情:家有精神疾病患者,家属务必做好5件事
  4. Echarts图表坐标轴文字太长,省略显示,鼠标放上显示全部(vue)
  5. 8年前诞生于淘宝,细数阿里云RPA 的前世今生!
  6. Java使用EasyExcel操作Excel表格
  7. 5款大神手机必备APP,个个都是精品,你有什么理由拒绝
  8. Non-parseable settings : Duplicated tag: ‘mirrors‘
  9. viewholder_Android ViewHolder模式示例
  10. Java基础知识学习:简单随手记录(1)