0

我試圖建立一個卷積神經網絡,但我絆了一些很奇怪的問題。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. 的權重和過濾器的值不會改變
  2. 變量「成本」是總是1.0
  3. 預測總是會顯示一個0

做一些調試後,我發現,這個問題必須來自優化,因爲成本和預測並非1.0和0之前,我把砝碼槽優化。

我希望這是足夠的信息,並且你能幫助我與我的問題。

PS。我已經嘗試使用tf.truncated_normal而不是tf.random_normal

+0

輸入顯然有問題,以及你如何餵食它們。你在創建會話之前是否檢查過'x'中的內容? –

+0

x是一個充滿numpy數組的列表 –

+0

我明白這個部分,但是你會讓我們知道它的具體尺寸嗎?您可能會錯誤地將它傳遞到網絡。 –

回答

1

我想我得到的問題與代碼。您需要定義佔位符來提供您的輸入,您沒有任何佔位符。您正在向模型傳遞一個常數值(第一個圖像)x [0]的張量流模型。當您在每個時期調用prediction = CNN(...)時,您的代碼每次定義一個新的張量計算圖。總體而言,您每次都定義一個模型,爲其提供一個恆定的圖像。這裏是一個鏈接定義我以前準備MNIST圖像TensorFlow CNN模型: https://github.com/dipendra009/MNIST_TF-Slim/blob/master/MNIST_TensorFlow.ipynb 。我希望它有幫助。此外,請查看TensorFlow文檔的佔位符,這將有助於您更好地理解它。

+0

這是我的改進的訓練碼: '爲電子在範圍(時期): 打印( '時期:',E + 1) for x_i in x: sess.run([cost,train],feed_dict = {x_p:x_i}) print('sess.run(cost,feed_dict = {x_p:x_i}))) print完蛋了!')' 但什麼都沒有改變 –

+0

你必須在圖形中定義的佔位符采取的投入。你看我的共享鏈接:https://github.com/dipendra009/MNIST_TF-Slim/blob/master/MNIST_TensorFlow.ipynb – dipendra009

+0

很抱歉,但我真的不明白你的「定義圖中的佔位符的意思「 –