实现简单的加法运算
import tensorflow as tf
'''
运行时会警告指出你的CPU支持AVX运算加速了线性代数计算,即点积,矩阵乘法,卷积等。可以从源代码安装TensorFlow来编译,也可以添加下面两行代码选则关闭
'''
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def tensorflow_demo():
a = 2
b = 3
c = a + b
print("普通加法运算的结果:\n", c)
# TensorFlow实现加法运算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法运算的结果:\n", c_t)
# 开启会话
with tf.Session() as sess:
c_t_value = sess.run(c_t)
print("c_t_value:\n", c_t_value)
return None
if __name__ == "__main__": # 注意为两个下划线
# TensorFlow的基本结构
tensorflow_demo()
# 运行结果
普通加法运算的结果:
5
TensorFlow加法运算的结果:
Tensor("add:0", shape=(), dtype=int32)
c_t_value:
5
TensorFlow结构分析
TensorFlow程序经常被组织成一个构件图阶段和一个执行图阶段。
在构建阶段,数据与操作的执行步骤被描述成一个图。
在执行阶段,使用会话执行构建好的图中的操作。
-
图和会话:
图:TensorFlow将计算表示为指令之间的依赖关系的一种表示法
会话:TensorFlow跨一个或多个本地或远程设备运行数据图的机制
-
张量:TensorFlow中的基本数据对象
-
节点:提供图中执行的操作
TensorFlow数据流图
TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源框架。
节点(Operation)在图中表示数学操作,线(edges)则表示在节点间互相联系的多维数据数组,即张量(tensor)
图与TensorBoard
图包含了一组tf.Operation代表的计算单元对象和tf.Tensor代表的计算单元之间的流动关系。
图相关操作
默认图:通常TensorFlow会默认创建一张图
查看默认图的两种方法:
- 通过调用tf.get_default_graph()访问,要将操作添加到默认图形中,直接创建OP即可
- op,sess都含有graph属性默认在一张图中
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def graph_demo():
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法运算的结果:\n", c_t)
# 查看默认图
# first:调用方法
default_g = tf.get_default_graph()
print("default_g:\n", default_g)
# second:查看属性
print("a_t的图属性:\n", a_t.graph)
print("c_t的图属性:\n", c_t.graph)
# 开启会话
with tf.Session() as sess:
c_t_value = sess.run(c_t)
print("c_t_value:\n", c_t_value)
print("sess的图属性:\n", sess.graph)
return None
if __name__ == "__main__":
graph_demo()
'''
运行结果:
TensorFlow加法运算的结果:
Tensor("add:0", shape=(), dtype=int32)
default_g:
<tensorflow.python.framework.ops.Graph object at 0x0000021EF853C240>
a_t的图属性:
<tensorflow.python.framework.ops.Graph object at 0x0000021EF853C240>
c_t的图属性:
<tensorflow.python.framework.ops.Graph object at 0x0000021EF853C240>
c_t_value:
5
sess的图属性:
<tensorflow.python.framework.ops.Graph object at 0x0000021EF853C240>
'''
创建图
可以通过tf.graph()自定义创建图
如果要在这张图中创建OP,典型用法是使用tf.graph.as_default()上下文管理器
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def graph_demo():
# 自定义图
new_g = tf.Graph()
# 在自己的图中定义数据和操作
with new_g.as_default():
a_new = tf.constant(20)
b_new = tf.constant(30)
c_new = a_new + b_new
print("c_new:\n", c_new)
print("a_new的图属性:\n", a_new.graph)
print("c_new的图属性:\n", c_new.graph)
# 开启会话
with tf.Session() as sess:
c_t_value = sess.run(c_t)
print("c_t_value:\n", c_t_value)
print("sess的图属性:\n", sess.graph)
with tf.Session(graph=new_g) as new_sess:
c_new_value = new_sess.run(c_new)
print("c_new_value:\n", c_new_value)
print("new_sess的图属性:\n", new_sess.graph)
return None
if __name__ == "__main__":
graph_demo()
'''
运行结果:
a_new的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001B6C2B217F0>
c_new的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001B6C2B217F0>
c_t_value:
5
sess的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001B6B0CCC358>
c_new_value:
50
new_sess的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001B6C2B217F0>
'''
TensorBoard:可视化学习
TensorBoard为TensorFlow中的可视化工具。
实现过程
-
数据序列化-event文件
TensorBoard通过读取TensorFlow的事件来运行,需要将数据生成一个序列化的Summary protobuf对象。
# 返回filewriter,写入事件文件到指定目录(最好用绝对路径),以提供给tensorboard使用 tf.summary.FileWrite('C:\Users\user\Desktop\test', graph=sess.graph) # 这将会在指定目录中生成一个event文件,名称如下 events.out.tfevents.{timestamp}.{hostname}
-
启动TensorBoard
tensorboard --logdir="C:\Users\user\Desktop\test" # 然后再浏览器中打开Tensorboard的图页面127.0.0.1:6006