TensorFLow 變量命名空間實(shí)例
一、name_scope
with tf.name_scope(name):
name_scope: 為了更好地管理變量的命名空間而提出的。比如在 tensorboard 中,因?yàn)橐肓?name_scope, 我們的 Graph 看起來才井然有序。
name_scope 對(duì) get_variable 創(chuàng)建變量的 name 沒有影響,即 get_variable 創(chuàng)建的變量不在 name_scope 這個(gè)命名空間中
二、variable_scope
with tf.variable_scope(name_or_scope, reuse=None):
variable_scope: 大部分情況下,跟 tf.get_variable() 配合使用,實(shí)現(xiàn)變量共享的功能
可通過tf.get_variable_scope().reuse == True/False 判斷參變量是否共享
當(dāng)前變量作用域可以用tf.get_variable_scope()進(jìn)行檢索并且reuse 標(biāo)簽可以通過調(diào)用tf.get_variable_scope().reuse_variables()設(shè)置為True
三、共享參變量
1、方法
使用 tf.Variable() 創(chuàng)建同一個(gè) name 的變量(操作名不同),均不會(huì)報(bào)錯(cuò),但系統(tǒng)會(huì)自動(dòng)修改 name(實(shí)質(zhì)還是不讓共享參變量)
使用 tf.get_varible() 創(chuàng)建同一個(gè) name 的變量(操作名不同),均會(huì)報(bào)錯(cuò)(為了避免無意識(shí)的參變量復(fù)用造成的錯(cuò)誤)
我們可以在 variable_scope 中使用 tf.get_variable() 創(chuàng)建變量,并通過 with tf.variable_scope(name_or_scope, reuse=True) 來共享參變量:
reuse=True:將只能獲取命名空間中已經(jīng)創(chuàng)建過的變量,如果變量不存在,則tf.get_variable函數(shù)將報(bào)錯(cuò)。
reuse=None / False:tf.get_variable操作將創(chuàng)建新的變量,如果同名的變量已經(jīng)存在,則tf.get_variable函數(shù)將報(bào)錯(cuò)。
2、代碼示例
# 下面是定義一個(gè)卷積層的通用方式
def conv_relu(input, kernel_shape, bias_shape):
# Create variable named "weights".
weights = tf.get_variable("weights", kernel_shape,
initializer=tf.random_normal_initializer())
# Create variable named "biases".
biases = tf.get_variable("biases", bias_shape,
initializer=tf.constant_intializer(0.0))
conv = tf.nn.conv2d(input, weights,
strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv + biases)
# 定義一個(gè)圖片過濾器
def my_image_filter(input_images):
with tf.variable_scope("conv1"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
with tf.variable_scope("conv2"):
# Variables created here will be named "conv2/weights", "conv2/biases".
return conv_relu(relu1, [5, 5, 32, 32], [32])
# 實(shí)驗(yàn)一:調(diào)用 my_image_filter() 兩次
result1 = my_image_filter(image1)
result2 = my_image_filter(image2)
>>> Raises ValueError(... conv1/weights already exists ...), tf.get_variable()會(huì)檢測已經(jīng)存在的變量是否已經(jīng)共享
# 解決方法一, 可以在設(shè)計(jì)網(wǎng)絡(luò)時(shí)加上一個(gè)布爾型的 reuse 參數(shù)
with tf.variable_scope("image_filters"):
result1 = my_image_filter(image1)
with tf.variable_scope("image_filters", reuse=True):
result2 = my_image_filter(image2)
# 解決方法二
with tf.variable_scope("image_filters") as scope:
# 下面我們兩次調(diào)用 my_image_filter 函數(shù),但是由于引入了變量共享機(jī)制
# 可以看到我們只是創(chuàng)建了一遍網(wǎng)絡(luò)結(jié)構(gòu)。
result1 = my_image_filter(image1)
scope.reuse_variables()
result2 = my_image_filter(image2)
# 解決方法三
with tf.variable_scope("image_filters") as scope:
result1 = my_image_filter(image1)
with tf.variable_scope(scope, reuse=True):
result2 = my_image_filter(image2)
# 打印出所有的可訓(xùn)練參變量
vs = tf.trainable_variables()
print('There are %d trainable_variables in the Graph: ' % len(vs))
for v in vs:
print(v)
# 輸出結(jié)果證明確實(shí):參變量共享,因?yàn)橹挥兴膫€(gè)變量,沒有創(chuàng)建新的變量。
There are 4 trainable_variables in the Graph:
Tensor("image_filters/conv1/weights/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("image_filters/conv1/biases/read:0", shape=(32,), dtype=float32)
Tensor("image_filters/conv2/weights/read:0", shape=(5, 5, 32, 32), dtype=float32)
Tensor("image_filters/conv2/biases/read:0", shape=(32,), dtype=float32)
四、取出所有可訓(xùn)練參數(shù)
# Returns all variables created with trainable=True in a var_list var_list = tf.trainable_variables() init = tf.global_variables_initializer() sess.run(init) for var in var_list: sess.run(var)
以上這篇TensorFLow 變量命名空間實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Jupyter Notebook “signal only works&nb
這篇文章主要介紹了解決Jupyter Notebook “signal only works in main thread“問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python基于隨機(jī)采樣一至性實(shí)現(xiàn)擬合橢圓(優(yōu)化版)
這篇文章主要對(duì)上一版的Python基于隨機(jī)采樣一至性實(shí)現(xiàn)擬合橢圓的優(yōu)化,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-11-11
Win10下用Anaconda安裝TensorFlow(圖文教程)
這篇文章主要介紹了Win10下用Anaconda安裝TensorFlow(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Pandas實(shí)現(xiàn)Excel文件讀取,增刪,打開,保存操作
Pandas?是一種基于?NumPy?的開源數(shù)據(jù)分析工具,用于處理和分析大量數(shù)據(jù)。本文將通過Pandas實(shí)現(xiàn)對(duì)Excel文件進(jìn)行讀取、增刪、打開、保存等操作,需要的可以參考一下2023-04-04
Python+Django實(shí)現(xiàn)接口測試工具的示例代嗎
本文主要介紹了Python+Django實(shí)現(xiàn)接口測試工具,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

