TensorFlow命名空間和TensorBoard圖節(jié)點實例
一,命名空間函數(shù)
tf.variable_scope
tf.name_scope
先以下面的代碼說明兩者的區(qū)別
# 命名空間管理函數(shù)
'''
說明tf.variable_scope和tf.name_scope的區(qū)別
'''
def manage_namespace():
with tf.variable_scope("foo"):
# 在命名空間foo下獲取變量"bar",于是得到的變量名稱為"foo/bar"。
a = tf.get_variable("bar",[1]) #獲取變量名稱為“bar”的變量
print a.name #輸出:foo/bar:0
with tf.variable_scope("bar"):
# 在命名空間bar下獲取變量"bar",于是得到的變量名稱為"bar/bar"。
a = tf.get_variable("bar",[1])
print a.name #輸出:bar/bar:0
with tf.name_scope("a"):
# 使用tf.Variable函數(shù)生成變量會受tf.name_scope影響,于是得到的變量名稱為"a/Variable"。
a = tf.Variable([1]) #新建變量
print a.name #輸出:a/Variable:0
# 使用tf.get_variable函數(shù)生成變量不受tf.name_scope影響,于是變量并不在a這個命名空間中。
a = tf.get_variable("b",[1])
print a.name #輸出:b:0
with tf.name_scope("b"):
# 使用tf.get_variable函數(shù)生成變量不受tf.name_scope影響,所以這里將試圖獲取名稱
# 為“b”的變量。然而這個變量已經(jīng)被聲明了,于是這里會報重復(fù)聲明的錯誤
tf.get_variable("b",[1])#提示錯誤
二,TensorBoard計算圖查看
1 以以下代碼實例,為指定任何的命名空間
def practice_num1(): # 練習(xí)1: 構(gòu)建簡單的計算圖 input1 = tf.constant([1.0, 2.0, 3.0],name="input1") input2 = tf.Variable(tf.random_uniform([3]),name="input2") output = tf.add_n([input1,input2],name = "add") #生成一個寫日志的writer,并將當(dāng)前的tensorflow計算圖寫入日志 writer = tf.summary.FileWriter(ROOT_DIR + "/log",tf.get_default_graph()) writer.close()
如何使用TensorBoard的過程不再介紹。查看未指明命名空間的運算圖

2 修改代碼制定命名空間之后的代碼
def practice_num1_modify():
#將輸入定義放入各自的命名空間中,從而使得tensorboard可以根據(jù)命名空間來整理可視化效果圖上的節(jié)點
# 練習(xí)1: 構(gòu)建簡單的計算圖
with tf.name_scope("input1"):
input1 = tf.constant([1.0, 2.0, 3.0],name="input1")
with tf.name_scope("input2"):
input2 = tf.Variable(tf.random_uniform([3]),name="input2")
output = tf.add_n([input1,input2],name = "add")
#生成一個寫日志的writer,并將當(dāng)前的tensorflow計算圖寫入日志
writer = tf.summary.FileWriter(ROOT_DIR + "/log",tf.get_default_graph())
writer.close()
查看運算圖

上圖只包含命名的兩個命名空間的節(jié)點,我們可以點擊名稱“input2”的圖標(biāo)上的+號,展開該命名空間

效果:通過命名空間可以整理可視化效果圖上的節(jié)點,使可視化的效果更加清晰。
以上這篇TensorFlow命名空間和TensorBoard圖節(jié)點實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python二進制轉(zhuǎn)化為十進制數(shù)學(xué)算法詳解
這篇文章主要介紹了Python二進制轉(zhuǎn)化為十進制數(shù)學(xué)算法,同時在這里也給大家分享一個好用的內(nèi)置函數(shù)map(),需要的朋友可以參考下2023-01-01
詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼
這篇文章主要介紹了詳解Python中@staticmethod和@classmethod區(qū)別及使用示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python編寫一個驗證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼
這篇文章主要介紹了Python編寫一個驗證碼圖片數(shù)據(jù)標(biāo)注GUI程序,本文給大家附上小編精心整理的源碼,需要的朋友可以參考下2019-12-12
Pytorch中的backward()多個loss函數(shù)用法
這篇文章主要介紹了Pytorch中的backward()多個loss函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
解決Tensorflow2.0 tf.keras.Model.load_weights() 報錯處理問題
這篇文章主要介紹了解決Tensorflow2.0 tf.keras.Model.load_weights() 報錯處理問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨想過來看看吧2020-06-06

