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()
原文地址:https://www.cnblogs.com/lvdongjie/p/11741745.html