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的更多相关文章

随机推荐

  1. js 删除 数组中某个元素(转载)

    来源:https://www.jb51.net/article/134312.htm js删除数组中某一项或几项的几种方法 https://www.jb51.net/article/154737.ht ...

  2. SQL月度统计

    select Convert ( VARCHAR(7),CreateTime,120) as Date ,sum(Money) as M FROM [LBAmmeterDB].[dbo].Am_Tas ...

  3. 【转】简易剖析Hadoop作业工作机制

    原文地址:https://www.cnblogs.com/duma/p/10666269.html 建议:结合第四版Hadoop权威指南阅读,更有利于理解 运行机制 运行一个 MR 程序主要涉及以下 ...

  4. 【scala】scala安装测试

    下载安装scala:scala-2.13.1.tgz 解压: [hadoop@hadoop01 ~]$ tar -zxvf scala-2.13.1.tgz 查看目录: [hadoop@hadoop0 ...

  5. c# 常见文件操作

  6. 【转】基于TMS320C6455的千兆以太网设计

    基于TI公司最新DSP芯片TMS320C6455.设计并实现了以太网通信软硬件接口.采用TMS320C6455片内以太网接口模块EMAC/MDIO,结合片外AR8031 PHY芯片,在嵌入式操作系统D ...

  7. AxureRP分页签 / Tab选项卡切换功能~

    最终结果图如下: 实现过程: 1.从元件库中拖一个动态面板,调整所需大小,接下来的步骤都通过双击动态面板来完成. 2.双击动态面板,弹出框“面板状态管理”,新建状态并命名.此处新建了TAB1.TAB2 ...

  8. python高级特性-迭代器

    凡是可作用于for循环的对象都是Iterable类型: 凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列: 集合数据类型如list.dict.str等是Itera ...

  9. P2261 [CQOI2007]余数求和[整除分块]

    题目大意 给出正整数 n 和 k 计算 \(G(n, k)=k\ \bmod\ 1 + k\ \bmod\ 2 + k\ \bmod\ 3 + \cdots + k\ \bmod\ n\) 的值 其中 ...

  10. Vue当中的this

    10事件绑定 methods当中的this就是Vue实例对象vm var vm = new Vue({ el: '#app', data: { num: 0 }, // 注意点: 这里不要忘记加逗号 ...