2016-07-15 75 views
-1
import numpy as np 
import tensorflow as tf 


#input data: 
x_input=np.linspace(0,10,1000) 
y_input=x_input+np.power(x_input,2) 

#model parameters 
W = tf.Variable(tf.random_normal([2,1]), name='weight') 
#bias 
b = tf.Variable(tf.random_normal([1]), name='bias') 

#placeholders 
#X=tf.placeholder(tf.float32,shape=(None,2)) 
X=tf.placeholder(tf.float32,shape=[None,2]) 
Y=tf.placeholder(tf.float32) 
x_modified=np.zeros([1000,2]) 

x_modified[:,0]=x_input 
x_modified[:,1]=np.power(x_input,2) 
#model 
#x_new=tf.constant([x_input,np.power(x_input,2)]) 
Y_pred=tf.add(tf.matmul(X,W),b) 

#algortihm 
loss = tf.reduce_mean(tf.square(Y_pred -Y)) 
#training algorithm 
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) 
#initializing the variables 
init = tf.initialize_all_variables() 

#starting the session session 
sess = tf.Session() 
sess.run(init) 

epoch=100 

for step in xrange(epoch): 
    # temp=x_input.reshape((1000,1)) 
    #y_input=temp 

    _, c=sess.run([optimizer, loss], feed_dict={X: x_modified, Y: y_input}) 
    if step%50==0 : 
     print c 

print "Model paramters:"  
print sess.run(W) 
print "bias:%f" %sess.run(b) 

我試圖在Tensorflow中實現多項式迴歸(二次)。損失不收斂。任何人都可以請幫我解決這個問題。雖然類似的邏輯正在爲線性迴歸工作!在Tensorflow多項式迴歸中的損失不收斂

回答

1

首先出現的是在你的形狀的問題,爲Y_predY

  • Y具有未知形狀,並且與以陣列形狀的(1000,)
  • Y_pred具有饋送形狀(1000, 1)
  • Y - Y_pred將然後有形狀(1000, 1000)

這個小代碼將證明我的觀點:

a = tf.zeros([1000]) # shape (1000,) 
b = tf.zeros([1000, 1]) # shape (1000, 1) 
print (a-b).get_shape() # prints (1000, 1000) 

你應該使用一致的類型:

y_input = y_input.reshape((1000, 1)) 

Y = tf.placeholder(tf.float32, shape=[None, 1]) 

反正損失正在迅速增長,因爲你有非常高的值之間(輸入0和100,你應該正常化),因此損失非常高(在訓練開始時約爲2000)。
漸變非常高,參數爆炸,損失達到無限。

最快的解決方法是降低您的學習速度(1e-5收斂我,儘管在最後很慢)。損失收斂在1左右後,您可以將其調高。

+0

非常感謝。它像一個魅力:)。而且,在將輸入範圍縮小到0-2之後,其自動收斂爲學習率「0.01」。 –