一、图结构
1、什么是图结构?
图结构:其实就是数据(Tensor) + 操作(Operation)
2、图的基本操作
(1)默认图
查看默认图的方法:
- 调用方法:tf.get_default_graph()
- 查看属性:.graph
(2)创建图
#创建图
new_g = tf.Graph()
with new_g.as_default():
定义数据和操作
二、TensorBoard可视化
TensorFlow可用于训练大规模深度神经网络所需的计算,使用该工具设计的计算往往复杂而深奥,为了方便理解TensorFlow程序,提供了TensorBoard可视化工具
1、实现程序可视化过程
(1)数据序列化——>生成events文件
TensorBoard通过读取TensorFlow的事件文件来运行,需要将数据生成一个序列化的 Summary protobuf对象
#返回filewriter,导入事件文件到指定路径(最好是绝对路径),以提供给tensorboard使用
#tf.summary.FileWriter(path, graph=xxx)
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def tensorflow_demo():
#构建图
a = tf.constant(100)
b = tf.constant(200)
c = tf.add(a, b)
#执行图
with tf.compat.v1.Session() as sess:
c_t = sess.run(c)
print("default_g attr: ", tf.compat.v1.get_default_graph())
print("c_t: ", c_t)
print("c graph attr: ", c.graph)
#生成序列化文件
tf.compat.v1.summary.FileWriter("./temp/sunmmary", graph=sess.graph)
(2)启动TensorBoard
tensorboard --logdir= "xxxx/temp/summary" (指定路径目录)
const和const_1是指令名称