02_01Graph_Session
import numpy as np
import tensorflow as tf
np.random.seed(42)
"""
学习:
1、图的创建
2、tf.constant() tf.add使用
3、tf.Session() 和 tf.Session().run() 方法的使用
""" def create_graph1():
# 先构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(value=5.0, dtype=tf.float32, shape=None, name='a')
b = tf.constant(value=8.0) # 2、用op add对上述两个常量分别加一个随机数
v1 = tf.add(x=a, y=np.random.random_sample(), name='v1')
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用tf.multiply进行2个tensor相乘。
rezult = tf.multiply(v1, v2)
print(a, b, v1, v2, rezult) def create_graph2():
"""
使用 + * 来代替 tf.add 和 tf.multiply
:return:
"""
# 先构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(value=5.0, dtype=tf.float32, shape=None, name='a')
b = tf.constant(value=8.0) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用tf.multiply进行2个tensor相乘。
rezult = v1 * v2
# rezult = tf.multiply(v1, v2)
print(a, b, v1, v2, rezult) def create_graph3():
# 先构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1,2,3,4,5,6,3,4,3,45,5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3,3,3,3,3,3234,56,324,3,5], dtype=tf.float32, shape=[5, 3]
) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2)
print(a, b, v1, v2, rezult) def create_graph4():
"""
学习不能跨图操作。
:return:
"""
# 先构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1,2,3,4,5,6,3,4,3,45,5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3,3,3,3,3,3234,56,324,3,5], dtype=tf.float32, shape=[5, 3]
) with tf.Graph().as_default():
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2)
print(a, b, v1, v2, rezult) # fixme 执行会话,获取结果。 def create_graph_do_session():
with tf.Graph().as_default():
# 一、构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1,2,3,4,5,6,3,4,3,45,5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3,3,3,3,3,3234,56,324,3,5], dtype=tf.float32, shape=[5, 3]
) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2)
print(a, b, v1, v2, rezult) # 二、构建会话
sess = tf.Session()
"""
tf.Session().run(self,
fetches, 给定具体获取哪些tensor的值,可以是1个,也可以是多个,给定多个tensor值,模型图只运行1次
feed_dict=None, 如果模型图中需要通过占位符传入数据,那么通过这个参数给定。
options=None, run_metadata=None)
"""
# print(sess.run(a))
# print(sess.run(b))
# print(sess.run(rezult))
# print(sess.run(v2))
_, _, _, v2_1, rezult_, v2_2 = sess.run([a, b, v1, v2, rezult, v2])
print(v2_1, rezult_, v2_2)
print(sess.run(v2))
sess.close() def create_graph_do_session1():
# 一、构建模型图
#print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
with tf.Graph().as_default():
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1,2,3,4,5,6,3,4,3,45,5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3,3,3,3,3,3234,56,324,3,5], dtype=tf.float32, shape=[5, 3]
) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2)
print(a, b, v1, v2, rezult) # 二、构建会话
sess = tf.Session()
"""
tf.Session().run(self,
fetches, 给定具体获取哪些tensor的值,可以是1个,也可以是多个,给定多个tensor值,模型图只运行1次
feed_dict=None, 如果模型图中需要通过占位符传入数据,那么通过这个参数给定。
options=None, run_metadata=None)
"""
# print(sess.run(a))
# print(sess.run(b))
# print(sess.run(rezult))
# print(sess.run(v2))
_, _, _, v2_1, rezult_, v2_2 = sess.run(fetches=[a, b, v1, v2, rezult, v2])
print(v2_1, rezult_, v2_2)
print(sess.run(fetches=v2))
sess.close() create_graph_do_session1()
def create_graph_do_session2():
# todo 演示关闭了会话后,再次调用会话会报错
# 一、构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1,2,3,4,5,6,3,4,3,45,5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3,3,3,3,3,3234,56,324,3,5], dtype=tf.float32, shape=[5, 3]
) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2) # 二、构建会话
sess = tf.Session()
_, _, _, v2_1, rezult_, v2_2 = sess.run(fetches=[a, b, v1, v2, rezult, v2])
# print(v2_1, rezult_, v2_2)
sess.close()
# RuntimeError: Attempted to use a closed Session.
print(sess.run(fetches=v2)) def create_graph_do_session3():
# todo 第二种执行会话的方式
with tf.Graph().as_default():
# 一、构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1,2,3,4,5,6,3,4,3,45,5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3,3,3,3,3,3234,56,324,3,5], dtype=tf.float32, shape=[5, 3]
) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2)
print(a, b, v1, v2, rezult) # 二、构建会话
# sess = tf.Session()
# print(rezult.eval(session=sess))
# print(v2.eval(session=sess))
# sess.close() with tf.Session() as sess:
rezult_ = sess.run(rezult) print(rezult.eval())
print(v2.eval()) def create_graph_do_interactive_session():
# todo 交互式会话 执行的方式
# 一、构建模型图
print('当前模型的默认图是:{}'.format(tf.get_default_graph()))
# 1、定义2个原始的输入的tensor对象
a = tf.constant(
value=[1, 2, 3, 4, 5, 6, 3, 4, 3, 45, 5], dtype=tf.float32, shape=[3, 5], name='a'
)
b = tf.constant(
value=[3, 3, 3, 3, 3, 3234, 56, 324, 3, 5], dtype=tf.float32, shape=[5, 3]
) # 2、用op add对上述两个常量分别加一个随机数
v1 = a + np.random.random_sample()
v2 = tf.add(b, tf.random_normal(shape=[], dtype=tf.float32, seed=43)) # 3、使用2个tensor矩阵相乘。
rezult = tf.matmul(v1, v2)
print(a, b, v1, v2, rezult) # 二、构建交互式会话
sess = tf.InteractiveSession()
print(sess.run([v2, v2, rezult]))
print(rezult.eval())
print(v2.eval())
print('当前的默认会话:{}'.format(tf.get_default_session())) # 总结。用tf实际写代码的一般结构。
# 一、构建模型图
# with tf.Graph().as_default():
# # 1、基于你的业务知识构建模型图。
#
# # 二、执行会话
# with tf.Session() as sess:
# sess.run(tf.global_variables_initializer())
# # a 、加载数据(features 和 targets)
# # b 、跑图
# sess.run()
# # c 、进行模型损失和准确率的查看。
# # d 、模型持久化 #
# if __name__ == '__main__':
# # create_graph4()
# # create_graph_do_session3()
# create_graph_do_interactive_session()
02_01Graph_Session的更多相关文章
随机推荐
- go语言 goquery爬虫
goquery 类似ruby的gem nokogiri goquery的选择器功能很强大,很好用.地址:https://github.com/PuerkitoBio/goquery 这是一个糗百首 ...
- laravel——表操作集成操作
背景:通过判断不同的请求参数,返回不同的需要的数据 一.准备工作: 数据库新建一个表 DROP TABLE IF EXISTS `tb_category2`; CREATE TABLE IF NOT ...
- 安装opencv出现的问题
ImportError: DLL load failed***** 1,pip uninstall opencv-python 卸载2,pip install opencv-contrib-pytho ...
- windows nginx
nginx.exe -s stop stop是快速停止nginx,可能并不保存相关信息: nginx.exe -s quit quit是完整有序的停止nginx,并保存相关信息. nginx.exe ...
- IDEA中看Flink 1.9源码时报Sources not found for: org.apache.flink:flink-shaded-hadoop-2:2.4.1-7.0
1.场景 在阅读Flink 1.9源码时,个别类如YarnClientImpl.java只能查看.class文件,想查看对应的.java source文件,点击Download source时,报So ...
- python在运行时终止执行 sys.exit
使用sys.exit 或者exit,quit均可以退出执行 # 程序执行中,需要时停止执行 import sys if __name__ == '__main__': for ii in range( ...
- Flask-session,WTForms,POOL,Websocket通讯原理 -握手,加密解密过程
1.Flask-session Flask中的session 需要执行 session_interface - open_session存储到redis中,存的key:session:d3f07db2 ...
- C#屏蔽Alt+F4组合键
在开发的软件不希望用户关闭的情况下,或者我们不想用户回到桌面,这时候我们就需要屏蔽windows键,或者Alt + F4 键等. //1.将窗体的属性KeyPrieview设置为true //keyp ...
- 1211 BBS后台管理文章添加
目录 昨日内容回顾 侧边栏inclusion_tag inclusion_tag的响应 使用 自定义inclusion_tag,标签,过滤器 文章的点赞点踩 前端 后端 校验规则 文章的评论功能 1. ...
- 《Exception团队》第七次作业:团队项目设计完善&编码
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件编码实现的工程要求 作业任务 1. ...