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的更多相关文章
随机推荐
- 修改网口速度mii-tool和ethtool
mii-tool # mii-tool -F 100baseTx-FD media: 100baseT4, 100baseTx-FD, 100baseTx-HD, 10baseT-FD, 10base ...
- vue_插槽的理解和使用
对于插槽的概念和使用,这是vue的一个难点,这需要我们静下心来,慢慢研究.以下是我这两天通过官网和其他资料的学习和使用总结出来的笔记,如有错误或者有不同见解的,欢迎留言,一起学习. 什么是插槽? 插槽 ...
- vi / vim 基本操作
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi n filename :打开文件,并将光标置于第n行首 vi filename :打开 ...
- 空指针异常:解决 RequestContextHolder.getRequestAttributes()为空的问题
现象:实现Feign请求拦截器时,执行如下代码,报空指针异常 ServletRequestAttributes attributes = (ServletRequestAttributes) Requ ...
- C++ --- 编码习惯小区别
定义一个变量,最好养成C代码的习惯 C代码: 用到的变量X,要在最开始定义:先定义,用的时候在初始化: [例如] int x; //先定义 ..... ..... ; //再初始化 C++代码: 用到 ...
- java 静态代码块和spring @value等注解注入顺序
java 静态代码块和spring @value等注解注入顺序 问题所在 先上代码 java方法 @Value("${mf.cashost}") public static S ...
- 03 c++中this指针
概念: 成员函数:在类中定义的函数.普通函数无法被继承,成员函数可以被继承.友元函数相当于普通函数. 友元函数不是类的组成,没有this指针,必须将成员函数操作符作为参数传递对象. 在c++中成员函数 ...
- CEOI2019 / CodeForces 1192B. Dynamic Diameter
题目简述:给定一棵$N \leq 10^5$个节点的树,边上带权,维护以下两个操作: 1. 修改一条边的边权: 2. 询问当前树的直径长度. 解1:code 注意到树的直径有以下性质: 定理:令$\t ...
- python 类 双下划线解析
__getattr__用法:说明:这是python里的一个内建函数,当调用的属性或者方法不存在时,该方法会被调用调用不存在的属性调用不存在的方法
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...