python神經(jīng)網(wǎng)絡(luò)tf.name_scope和tf.variable_scope函數(shù)區(qū)別
學(xué)習(xí)前言
最近在學(xué)目標(biāo)檢測……SSD的源碼好復(fù)雜……看了很多版本的SSD源碼,發(fā)現(xiàn)他們會使用tf.variable_scope,剛開始我還以為就是tf.name_scope,才發(fā)現(xiàn)原來兩者是不一樣的
兩者區(qū)別
tf.name_scope()和tf.variable_scope()是兩個作用域,一般與兩個創(chuàng)建/調(diào)用變量的函數(shù)tf.variable() 和tf.get_variable()搭配使用。
為什么要使用兩個不同的作用域方式呢?其主要原因與變量共享相關(guān)。
變量共享主要涉及兩個函數(shù):tf.Variable() 和tf.get_variable()
在tf.variable_scope的作用域下需要使用tf.get_variable()函數(shù),這是因?yàn)閠f.get_variable()擁有一個變量檢查機(jī)制,會檢測已經(jīng)存在的變量是否設(shè)置為共享變量,當(dāng)同名變量存在共享機(jī)制時,不會報(bào)錯,如果并未設(shè)置為共享變量,則報(bào)錯。
如果使用tf.Variable() 的話每次都會新建變量。但是很多時候我們希望重用一些變量,所以我們使用到了get_variable(),它會去搜索變量名,有就直接用,沒有再新建。
在進(jìn)行變量共享的時候需要使用到標(biāo)志reuse,當(dāng)reuse = True時是可以共享,F(xiàn)alse時不可以共享。
tf.variable_scope函數(shù)
tf.variable_scope( name_or_scope, default_name=None, values=None, initializer=None, regularizer=None, caching_device=None, partitioner=None, custom_getter=None, reuse=None, dtype=None, use_resource=None, constraint=None, auxiliary_name_scope=True )
其中:
1、name_or_scope:范圍的名稱。
2、default_name:如果name_or_scope參數(shù)為None,則使用默認(rèn)的名稱,該名稱將是唯一的;如果提供了name_or_scope,它將不會被使用,因此它不是必需的,并且可以是None。
3、values:傳遞給操作函數(shù)的Tensor參數(shù)列表。
4、initializer:此范圍內(nèi)變量的默認(rèn)初始值設(shè)定項(xiàng)。
5、regularizer:此范圍內(nèi)變量的默認(rèn)正規(guī)化器。
6、caching_device:此范圍內(nèi)變量的默認(rèn)緩存設(shè)備。
7、partitioner:此范圍內(nèi)變量的默認(rèn)分區(qū)程序。
8、custom_getter:此范圍內(nèi)的變量的默認(rèn)自定義吸氣。
9、reuse:可以是True、None或tf.AUTO_REUSE;如果是True,即可以開始共享變量,變量重構(gòu)用;如果是tf.AUTO_REUSE,則我們創(chuàng)建變量(如果它們不存在),否則返回它們(用于在第一輪創(chuàng)建變量);如果是None,則我們繼承父范圍的重用標(biāo)志。
10、dtype:在此范圍中創(chuàng)建的變量類型。
測試代碼
1、使用reuse=True共享變量
import tensorflow as tf # 初始化第一個v1 with tf.variable_scope("scope1"): v1 = tf.get_variable("v1",[3,3],tf.float32,initializer=tf.constant_initializer(1)) print(v1.name) # 不同的作用域 with tf.variable_scope("scope2"): v1 = tf.get_variable("v1",[3,3],tf.float32,initializer=tf.constant_initializer(1)) print(v1.name) # 開始共享 with tf.variable_scope("scope1",reuse = True): v1_share = tf.get_variable("v1",[3,3],tf.float32,initializer=tf.constant_initializer(1)) print(v1_share.name)
運(yùn)行結(jié)果為:
scope1/v1:0
scope2/v1:0
scope1/v1:0
如果在下部再加上
with tf.variable_scope("scope2"): v1_share = tf.get_variable("v1",[3,3],tf.float32,initializer=tf.constant_initializer(1)) print(v1_share.name)
此時沒有reuse,不能共享,程序報(bào)錯。
2、使用AUTO_REUSE共享變量
import tensorflow as tf # 使用AUTO_REUSE可以直接創(chuàng)建 # 如果reuse = True,初始化第一輪創(chuàng)建的時候會報(bào)錯 def demo(): with tf.variable_scope("demo", reuse=tf.AUTO_REUSE): v = tf.get_variable("v", [1]) return v v1 = demo() v2 = demo() print(v1.name)
運(yùn)行結(jié)果為:
demo/v:0
demo/v:0
以上就是python神經(jīng)網(wǎng)絡(luò)tf.name_scope和tf.variable_scope函數(shù)區(qū)別的詳細(xì)內(nèi)容,更多關(guān)于tf.name_scope和tf.variable_scope的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python自動發(fā)送郵件的方法實(shí)例總結(jié)
這篇文章主要介紹了Python自動發(fā)送郵件的方法,結(jié)合實(shí)例形式總結(jié)分析了Python使用smtplib和email模塊發(fā)送郵件的相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-12-12Python使用pyaudio實(shí)現(xiàn)錄音功能
pyaudio是一個跨平臺的音頻I/O庫,使用PyAudio可以在Python程序中播放和錄制音頻,本文將利用它實(shí)現(xiàn)錄音功能,并做到停止說話時自動結(jié)束2023-05-05