溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

發(fā)布時間:2021-10-18 09:21:16 來源:億速云 閱讀:148 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一、圖

圖:數(shù)據(jù)(張量Tenrsor)+ 操作(節(jié)點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)

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

2、.graph查看圖屬性
# 方法二:查看屬性
    # 查看節(jié)點屬性
    print('a的屬性:', a.graph)
    print('c的屬性:', c.graph)
    # 查看會話屬性
    print('會話sess的圖屬性:', sess.graph)

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

可以發(fā)現(xiàn)這些圖的地址都是同一個地址,是因為它們都是默認(rèn)使用了默認(rèn)圖。

代碼
# 查看默認(rèn)圖
def View_Graph():
    # 方法一:調(diào)用方法
    default = tf.get_default_graph()
    print('default:', default)   
    # 方法二:查看屬性
    # 查看節(jié)點屬性
    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)

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

2、創(chuàng)建靜態(tài)圖
 # 2 創(chuàng)建靜態(tài)圖(張量和節(jié)點)
    with new_graph.as_default():
        a = tf.constant(10)
        b = tf.constant(20)
        c = a + b
        print(c)
3、開啟會話(運行)
# 3 開啟對話(運行)
    with tf.Session(graph=new_graph) as sess:
        print('c=', sess.run(c))

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

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é)點屬性
    print('a的屬性:', a.graph)
    print('c的屬性:', c.graph)
    # 查看會話屬性
    print('會話sess的圖屬性:', sess.graph)

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

代碼
# 自定義圖
def Create_myGraph():
    # 1 創(chuàng)建自定義圖
    new_graph = tf.Graph()
    print(new_graph)
    
    # 2 創(chuàng)建靜態(tài)圖(張量和節(jié)點)
    with new_graph.as_default():
        a = tf.constant(10)
        b = tf.constant(20)
        c = a + b
        print(c)
    
    # 3 開啟對話(運行)
    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

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

3、打開給定的網(wǎng)址

http://localhost:6006/(cmd中給的網(wǎng)址)

得到可視化結(jié)果:

TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析

總代碼

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é)點)   
    # 會話(執(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é)點屬性
    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é)點)
    with new_graph.as_default():
        a = tf.constant(10)
        b = tf.constant(20)
        c = a + b
        print(c)   
    # 3 開啟對話(運行)
    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()

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“TensorFlow可視化工具中TensorBoard默認(rèn)圖與自定義圖的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI