2016-07-15 77 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”。 –