TensorFlow(2)图(默认图与自定义图) TensorBoard可视化
生活随笔
收集整理的這篇文章主要介紹了
TensorFlow(2)图(默认图与自定义图) TensorBoard可视化
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
一、圖
1、默認(rèn)圖
1、調(diào)用方法查看默認(rèn)圖屬性
2、.graph查看圖屬性
代碼
2、自定義圖(創(chuàng)建圖)
1、創(chuàng)建自定義圖
2、創(chuàng)建靜態(tài)圖
3、開啟會話(運(yùn)行)
4、查看自定義圖
代碼
二、TensorBoard可視化
1、可視化處理
2、?打開TensorBoard
1、先移到文件夾的前面
2、 打開TensorBoard(從文件中獲取數(shù)據(jù))
3、打開給定的網(wǎng)址
總代碼
一、圖
圖:數(shù)據(jù)(張量Tenrsor)+ 操作(節(jié)點(diǎn)Operation)? ? ? ? ?(靜態(tài))
圖可以用:1、默認(rèn)圖;2、自定義圖。
1、默認(rèn)圖
查看默認(rèn)圖的方式:
1、調(diào)用方法:tf.get_default_graph()
2、查看屬性:.graph
1、調(diào)用方法查看默認(rèn)圖屬性
# 方法一:調(diào)用方法default = tf.get_default_graph()print('default:', default)
2、.graph查看圖屬性
# 方法二:查看屬性# 查看節(jié)點(diǎn)屬性print('a的屬性:', a.graph)print('c的屬性:', c.graph)# 查看會話屬性print('會話sess的圖屬性:', sess.graph)
可以發(fā)現(xiàn)這些圖的地址都是同一個(gè)地址,是因?yàn)樗鼈兌际悄J(rèn)使用了默認(rèn)圖。
代碼
# 查看默認(rèn)圖
def View_Graph():# 方法一:調(diào)用方法default = tf.get_default_graph()print('default:', default)# 方法二:查看屬性# 查看節(jié)點(diǎn)屬性print('a的屬性:', a.graph)print('c的屬性:', c.graph)# 查看會話屬性print('會話sess的圖屬性:', sess.graph)
2、自定義圖(創(chuàng)建圖)
1、創(chuàng)建自定義圖
# 1 創(chuàng)建自定義圖new_graph = tf.Graph()print(new_graph)
2、創(chuàng)建靜態(tài)圖
# 2 創(chuàng)建靜態(tài)圖(張量和節(jié)點(diǎn))with new_graph.as_default():a = tf.constant(10)b = tf.constant(20)c = a + bprint(c)
3、開啟會話(運(yùn)行)
# 3 開啟對話(運(yùn)行)with tf.Session(graph=new_graph) as sess:print('c=', sess.run(c))
4、查看自定義圖
# 4 查看自定義圖View_Graph(a, b, c, sess)
# 查看圖
def View_Graph(a, b, c, sess):# 方法一:調(diào)用方法default = tf.get_default_graph()print('default:', default)# 方法二:查看屬性# 查看節(jié)點(diǎn)屬性print('a的屬性:', a.graph)print('c的屬性:', c.graph)# 查看會話屬性print('會話sess的圖屬性:', sess.graph)
代碼
# 自定義圖
def Create_myGraph():# 1 創(chuàng)建自定義圖new_graph = tf.Graph()print(new_graph)# 2 創(chuàng)建靜態(tài)圖(張量和節(jié)點(diǎn))with new_graph.as_default():a = tf.constant(10)b = tf.constant(20)c = a + bprint(c)# 3 開啟對話(運(yùn)行)with tf.Session(graph=new_graph) as sess:print('c=', sess.run(c))# 4 查看自定義圖View_Graph(a, b, c, sess)
二、TensorBoard可視化
1、可視化處理
?tf.summary.FileWriter(path, graph=)
# 可視化tf.summary.FileWriter("C:\\Users\\Administrator\\Desktop\\summary", graph=sess.graph) #path 圖
2、?打開TensorBoard
在cmd中操作:
1、先移到文件夾的前面
cd C://Users//Administrator//Desktop
2、 打開TensorBoard(從文件中獲取數(shù)據(jù))
tensorboard --logdir=summary
3、打開給定的網(wǎng)址
http://localhost:6006/(cmd中給的網(wǎng)址)
得到可視化結(jié)果:
?
總代碼
import tensorflow as tf# 創(chuàng)建TensorFlow框架
def Create_Tensorflow():# 圖(靜態(tài))a = tf.constant(2) # 數(shù)據(jù)1(張量)b = tf.constant(6) # 數(shù)據(jù)2(張量)c = a + b # 操作(節(jié)點(diǎn))# 會話(執(zhí)行)with tf.Session() as sess:print('c=', sess.run(c))# 可視化tf.summary.FileWriter("C:\\Users\\Administrator\\Desktop\\summary", graph=sess.graph)# 查看默認(rèn)圖View_Graph(a, b, c, sess)# 查看圖
def View_Graph(a, b, c, sess):# 方法一:調(diào)用方法default = tf.get_default_graph()print('default:', default)# 方法二:查看屬性# 查看節(jié)點(diǎn)屬性print('a的屬性:', a.graph)print('c的屬性:', c.graph)# 查看會話屬性print('會話sess的圖屬性:', sess.graph)# 自定義圖
def Create_myGraph():# 1 創(chuàng)建自定義圖new_graph = tf.Graph()print(new_graph)# 2 創(chuàng)建靜態(tài)圖(張量和節(jié)點(diǎn))with new_graph.as_default():a = tf.constant(10)b = tf.constant(20)c = a + bprint(c)# 3 開啟對話(運(yùn)行)with tf.Session(graph=new_graph) as sess:print('c=', sess.run(c))# 4 查看自定義圖View_Graph(a, b, c, sess)if __name__ == '__main__':# 創(chuàng)建TensorFlow框架Create_Tensorflow()# 創(chuàng)建自定義圖Create_myGraph()
總結(jié)
以上是生活随笔為你收集整理的TensorFlow(2)图(默认图与自定义图) TensorBoard可视化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow(1)TensorF
- 下一篇: TensorFlow(3)张量与变量