国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

TensorFLow 變量命名空間實例

2020-02-15 21:16:18
字體:
來源:轉載
供稿:網友

一、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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泗阳县| 张掖市| 化州市| 刚察县| 城口县| 平陆县| 凤城市| 新田县| 东平县| 图们市| 铜鼓县| 安顺市| 宿州市| 屯门区| 丹江口市| 佛冈县| 安达市| 威宁| 临泉县| 梁河县| 台中县| 恩施市| 武山县| 邮箱| 衢州市| 大余县| 许昌县| 那坡县| 万州区| 通许县| 平山县| 裕民县| 赤水市| 竹溪县| 安化县| 玉屏| 满洲里市| 汝南县| 古蔺县| 祥云县| 邵阳县|