Tensorflow筆記1-weights already exists, disallowed. did you mean to set reuse=True..
完整報錯資訊:
weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
出錯原因:
tensorflow中,同一個變數在不同地方都被使用了兩次。
解決辦法:
方法一:重用變數,即同一個變數被共用,任何使用此變數的位置,都可對其進行修改。
方法二:更改名稱空間,即將變數名放置在不同的名稱空間下,變數有自身的作用域。
解決此類問題的步驟:
第一步:找到出錯位置
第二步:找到出錯原因
詳細過程:
方法一:
之所以會出現此類報錯資訊的主要原因是同一變數名使用了兩次,在第二次使用的時候就會報錯,tensorflow中預設一個變數名不能重複使用。如下函式:
函式conv_relu定義了一個卷積操作和 啟用函式,並且定義了變數名字為weights,biases兩個變數
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)
函式my_image_fileter定義了一個簡單的網路結構圖,用於對圖片的操作
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 ...)
此時使用同一個函式對圖片進行操作,可以共享變數的使用,因此為了能夠共享變數,必須將變數設定為reuse=True,除了一下方法,還有其他方法,可以看官方文件,或錯誤提示進行修改。
with tf.variable_scope("image_filters") as scope:
result1 = my_image_filter(image1)
scope.reuse_variables()
result2 = my_image_filter(image2)
方法二:方法一中的修改辦法只適用於共享變數,如果變數名字相同,但是使用不同的變數值,兩者互不影響,因此可以定義名稱空間。
for(int i=0;i<10;i++);
for(int i=0;i<5;i++);
//此兩個for迴圈中的i值名字相同,但作用在不同的名稱空間中,因此i值得改變互不影響。
本人當時得程式出錯位置:
depthwise = depthwise_conv2d('depthwise', x=padded, w=None,kernel_size=(3, 1), stride=stride, l2_strength=l2_strength, padding='VALID', bias=bias, dilation_factor= dilation, activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training) depthwise = depthwise_conv2d('depthwise', x=depthwise, w=None,kernel_size=(1, 3), stride=stride, l2_strength=l2_strength, padding='VALID', bias=bias, dilation_factor= dilation, activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training)
由於名稱空間名字都為depthwise,因此,weights在使用時,出現了相同變數在不同位置呼叫了兩次,但我們的目的是不能使用相同的變數,為了解決此類問題,可以將變數名處在不同的名稱空間中。
depthwise = depthwise_conv2d('depthwise', x=padded, w=None,kernel_size=(3, 1), stride=stride, l2_strength=l2_strength, padding='VALID', bias=bias, dilation_factor= dilation, activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training) depthwise = depthwise_conv2d('xxxxxxxx', x=depthwise, w=None,kernel_size=(1, 3), stride=stride, l2_strength=l2_strength, padding='VALID', bias=bias, dilation_factor= dilation, activation=None, batchnorm_enabled=batchnorm_enabled, is_training=is_training)
這樣權重weights名字相同,但一個是在名稱空間depthwise中,一個在名稱空間xxxxxxxx中,更改任意的weights,並不影響其他的weights值。