我試圖建立一個卷積神經網絡,但我絆了一些很奇怪的問題。Tensorflow梯度下降錯誤:權重不發生變化,成本爲1.0
第一件事第一,這裏是我的代碼:
import tensorflow as tf
import numpy as np
import matplotlib.image as mpimg
import glob
x = []
y = 1
for filename in glob.glob('trainig_data/*.jpg'):
im = mpimg.imread(filename)
x.append(im)
if len(x) == 10:
break
epochs = 5
weights = [tf.Variable(tf.random_normal([5,5,3,32],0.1)),
tf.Variable(tf.random_normal([5,5,32,64],0.1)),
tf.Variable(tf.random_normal([5,5,64,128],0.1)),
tf.Variable(tf.random_normal([75*75*128,1064],0.1)),
tf.Variable(tf.random_normal([1064,1],0.1))]
def CNN(x, weights):
output = tf.nn.conv2d([x], weights[0], [1,1,1,1], 'SAME')
output = tf.nn.relu(output)
output = tf.nn.conv2d(output, weights[1], [1,2,2,1], 'SAME')
output = tf.nn.relu(output)
output = tf.nn.conv2d(output, weights[2], [1,2,2,1], 'SAME')
output = tf.nn.relu(output)
output = tf.reshape(output, [-1,75*75*128])
output = tf.matmul(output, weights[3])
output = tf.nn.relu(output)
output = tf.matmul(output, weights[4])
output = tf.reduce_sum(output)
return output
sess = tf.Session()
prediction = CNN(tf.cast(x[0],tf.float32), weights)
cost = tf.reduce_mean(tf.square(prediction-y))
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
init = tf.global_variables_initializer()
sess.run(init)
for e in range(epochs):
print('epoch:',e+1)
for x_i in x:
prediction = CNN(tf.cast(x_i,tf.float32), weights)
sess.run([cost, train])
print(sess.run(cost))
print('optimization finished!')
print(sess.run(prediction))
現在,這裏是我的問題:
- 的權重和過濾器的值不會改變
- 變量「成本」是總是1.0
- 預測總是會顯示一個0
做一些調試後,我發現,這個問題必須來自優化,因爲成本和預測並非1.0和0之前,我把砝碼槽優化。
我希望這是足夠的信息,並且你能幫助我與我的問題。
PS。我已經嘗試使用tf.truncated_normal
而不是tf.random_normal
輸入顯然有問題,以及你如何餵食它們。你在創建會話之前是否檢查過'x'中的內容? –
x是一個充滿numpy數組的列表 –
我明白這個部分,但是你會讓我們知道它的具體尺寸嗎?您可能會錯誤地將它傳遞到網絡。 –