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. MySQL Hardware--FIO压测

    FIO参数 .txt 支持文件系统或者裸设备,-filename=/dev/sda2或-filename=/dev/sdb direct= 测试过程绕过机器自带的buffer,使测试结果更真实 rw= ...

  2. MQTT的websockets应用_转

    转自:mosquitto 与websocket 的结合 前言 mosquitto 作为一个消息代理, 客户端与 mosquitto 服务端的通信时基于 MQTT 协议的, 而现在的主流 web 应用时 ...

  3. C++(三十九) — 主函数中增加调试信息

        程序调试技术: (1)阅读程序,静态代码复查: (2)“cout大法” (3)使用开发工具,断点 int main() { TShape s(lu, lu); cout << __ ...

  4. 本地安装部署Jira

    https://blog.csdn.net/u013492736/article/details/83315650 1. 首先在官网下自行搭建服务器的版本,有适合于windows的,也有linux版本 ...

  5. linux 查看目录的剩余空间大小

    两个命令df .du结合比较直观 df    -h                     查看整台服务器的硬盘使用情况 cd    /                       进入根目录 du ...

  6. suse双网卡绑定

    这里使用两张网卡eth1.eth2进行 编辑/etc/sysconfig/network/ifcfg-bond0文件(此文件没有需要创建) device='bond0' BOOTPROTO='stat ...

  7. 个性化排序算法实践(五)——DCN算法

    wide&deep在个性化排序算法中是影响力比较大的工作了.wide部分是手动特征交叉(负责memorization),deep部分利用mlp来实现高阶特征交叉(负责generalizatio ...

  8. yarn cluster和yarn client模式区别——yarn-cluster适用于生产环境,结果存HDFS;而yarn-client适用于交互和调试,也就是希望快速地看到application的输出

    Yarn-cluster VS Yarn-client 从广义上讲,yarn-cluster适用于生产环境:而yarn-client适用于交互和调试,也就是希望快速地看到application的输出. ...

  9. Spring -07 -AOP [面向切面编程] - 使用注解@+ AspectJ 方式实现环绕/前/后等通知 -超简洁 --静态代理/动态代理{JDK/cglib}

    1.spring 不会自动去寻找注解,必须告诉 spring 哪些包下的类中可能有注解;使用注解来取代配置文件.1.1 引入xmlns:context ,指定扫描范围 <context:comp ...

  10. java架构

    技术架构是以Spring Framework为核心容器,Spring MVC为模型视图控制器,MyBatis作为数据访问层, Apache Shiro为权限授权层,使用Ehcahe对常用数据进行缓存. ...