1. 程式人生 > >tensorflow+Python ValueError以及解決方法(後續繼續更新)

tensorflow+Python ValueError以及解決方法(後續繼續更新)

使用python 怎麼可能沒有ValueError,下面羅列我見到的幾種錯誤,雖然很低階,但是不試過還是很難找到錯誤的

ValueError: Incompatible type conversion requested to type ‘float32’ for variable of type ‘int32_ref’

使用tensorflow時,出現在卷積層,原因在於初始化卷積權重或者偏置時, 採用整數進行初始化,導致卷積權重或者偏置為整形變數。
下面貼出我錯誤的程式碼

def bias_variable(shape, name):
    with tf.name_scope('Bias'):
        initial = tf.constant(0, shape=shape)
        biases = tf.Variable(initial)
        tf.summary.histogram(name+'/Biases_function', biases)
    return biases

修改結果

def bias_variable(shape, name):
    with tf.name_scope('Bias'):
        initial = tf.constant(0.0, shape=shape)
        biases = tf.Variable(initial)
        tf.summary.histogram(name+'/Biases_function', biases)
        return biases

也就是0於0.0的問題,在修改初始化引數時,一不留神就改錯了,然後還不知道發生了什麼。

ValueError: Incompatible shapes between op input and calculated input gradient.

我錯誤的原因在於使用tensorflow的反捲積tf.nn.conv2d_transpose是制定的output_shape出錯了,其實函式根據輸入和卷積核是可以計算輸出尺寸的,也就是tf.nn.conv2d_transpose的資訊是冗餘的,因此,如果冗餘的資訊對應不上,導致誤差無法反向傳遞。