一、name_scope
with tf.name_scope(name):
name_scope: 為了更好地管理變量的命名空間而提出的。比如在 tensorboard 中,因為引入了 name_scope, 我們的 Graph 看起來才井然有序。
name_scope 對 get_variable 創建變量的 name 沒有影響,即 get_variable 創建的變量不在 name_scope 這個命名空間中
二、variable_scope
with tf.variable_scope(name_or_scope, reuse=None):
variable_scope: 大部分情況下,跟 tf.get_variable() 配合使用,實現變量共享的功能
可通過tf.get_variable_scope().reuse == True/False 判斷參變量是否共享
當前變量作用域可以用tf.get_variable_scope()進行檢索并且reuse 標簽可以通過調用tf.get_variable_scope().reuse_variables()設置為True
三、共享參變量
1、方法
使用 tf.Variable() 創建同一個 name 的變量(操作名不同),均不會報錯,但系統會自動修改 name(實質還是不讓共享參變量)
使用 tf.get_varible() 創建同一個 name 的變量(操作名不同),均會報錯(為了避免無意識的參變量復用造成的錯誤)
我們可以在 variable_scope 中使用 tf.get_variable() 創建變量,并通過 with tf.variable_scope(name_or_scope, reuse=True) 來共享參變量:
reuse=True:將只能獲取命名空間中已經創建過的變量,如果變量不存在,則tf.get_variable函數將報錯。
reuse=None / False:tf.get_variable操作將創建新的變量,如果同名的變量已經存在,則tf.get_variable函數將報錯。
2、代碼示例
# 下面是定義一個卷積層的通用方式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)# 定義一個圖片過濾器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])# 實驗一:調用 my_image_filter() 兩次result1 = my_image_filter(image1)result2 = my_image_filter(image2)>>> Raises ValueError(... conv1/weights already exists ...), tf.get_variable()會檢測已經存在的變量是否已經共享# 解決方法一, 可以在設計網絡時加上一個布爾型的 reuse 參數 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: # 下面我們兩次調用 my_image_filter 函數,但是由于引入了變量共享機制 # 可以看到我們只是創建了一遍網絡結構。 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)# 打印出所有的可訓練參變量vs = tf.trainable_variables()print('There are %d trainable_variables in the Graph: ' % len(vs))for v in vs: print(v)# 輸出結果證明確實:參變量共享,因為只有四個變量,沒有創建新的變量。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)
新聞熱點
疑難解答