Tensorflow 多项式回归中的损失不收敛

Loss not converging in Polynomial regression in Tensorflow

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 中实现多项式回归(二次)。损失没有收敛。谁能帮我解决这个问题。不过,类似的逻辑也适用于线性回归!

首先你的形状有问题,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 对我来说收敛了,尽管最后收敛得非常慢)。你可以在loss收敛到1.

左右后调高