2017-02-18 16 views
1

我試圖寫一個tensorflow代碼來第一次訓練樣本,但我似乎在每一步訓練後W和b的權重因子總是爲零。Tensorflow:在訓練中使用softmax,得到結果W,b值總是爲零?

訓練數據非常簡單,當00.3,y = 1時,訓練數據爲10000個樣本(x,y)。我從csv文件導入這些數據。如下所示在csv文件sotred 訓練探索數據(總共有10000點的數據):

0.487801884,1; 
    0.457740109,1; 
    0.092949029,-1; 
    0.704023173,1; 
    0.07851864,-1; 

但是,當運行此代碼和打印W和B中的每個步驟中,我發現W,b爲始終爲零,好像他們沒有受過訓練。培訓結果:

W= [[ 0. 0.]] 
    b= [ 0. 0.] 
    Epoch: 0000000001 cost= 0.821999985 W= [[ 0. 0.]] b= [ 0. 0.] 
    Optimization Finished! 
    Accuracy: 1.0 

我很困惑,有誰能幫我找到問題所在嗎?非常感謝! 代碼是在這裏attched:

#coding=utf-8 
import tensorflow as tf 
import numpy 
import os 
import csv 
#training data sotred in csv file 
filename=open('D:\Program Files (x86)\logistic\sample.csv','r') 
reader=csv.reader(filename) 

t_X,t_Y=[],[] 

for i in reader: 
    t_X.append(i[0]) 
    t_Y.append(i[1]) 

t_X=numpy.asarray(t_X) 
t_Y=numpy.asarray(t_Y) 
t_XT=numpy.transpose([t_X]) 
t_YT=numpy.transpose([t_Y]) 

#Parameters 
learning_rate = 0.01 
training_epochs = 1 
batch_size=50 
display_step = 1 

#Input 
n_samples = t_X.shape[0] 

#print "n_samples:",n_samples 
x = tf.placeholder(tf.float32, [None, 1]) 
y = tf.placeholder(tf.float32, [None, 1]) 

#Weight 
W = tf.Variable(tf.zeros([1, 2])) 
b = tf.Variable(tf.zeros([2])) 

#model 
pred = tf.nn.softmax(tf.matmul(x, W) + b) 
cost = tf.reduce_mean(tf.square(y-pred)) 
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 

init = tf.global_variables_initializer() 

with tf.Session() as sess: 
    sess.run(init) 

    for epoch in range(training_epochs): 
     avg_cost=0 
     total_batch=int(n_samples/batch_size) 

     i=0 
     #read training data and transfer it into (m,n) 
     for anc in range(total_batch): 
      m=numpy.asarray([t_X[i],t_X[i+1],t_X[i+2],t_X[i+3],t_X[i+4]]) 
      n=numpy.asarray([t_Y[i],t_Y[i+1],t_Y[i+2],t_Y[i+3],t_Y[i+4]]) 
      m=numpy.transpose([m]) 
      n=numpy.transpose([n]) 

      _,c=sess.run([optimizer,cost], feed_dict={x: m, y: n}) 

      i=i+batch_size 
      avg_cost += c/total_batch 

     if (epoch+1)%display_step==0:   
      print ("Epoch:",'%010d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost),"W=",sess.run(W),"b=",sess.run(b)) 

    print ("Optimization Finished!") 

    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) 
    # Calculate accuracy 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
    print("Accuracy:", accuracy.eval(feed_dict={x: t_XT, y: t_YT}) 

回答

0

JEP,你能想到這個問題,這個重量初始化:

#Weight 
W = tf.Variable(tf.zeros([1, 2])) 
b = tf.Variable(tf.zeros([2])) 

你的權重應隨機初始化;)

+0

謝謝!我用以下代碼替換了這兩個語句:W = tf.Variable(tf.random_normal([1,2],dtype = tf.float32)) b = tf.Variable(tf.random_normal([2],dtype = tf .float32)),它現在似乎正常工作:) –

+0

完美!這是一個常見的錯誤;) – rmeertens

+1

如果你想了解更多關於這個查詢「神經網絡對稱性破壞」 – Aaron