Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 张量流多项式回归中的不收敛损失_Tensorflow_Non Linear Regression - Fatal编程技术网

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

Tensorflow 张量流多项式回归中的不收敛损失,tensorflow,non-linear-regression,Tensorflow,Non Linear Regression,我试图在Tensorflow中实现多项式回归(二次)。损失并没有收敛。谁能帮我解决这个问题。不过,类似的逻辑也适用于线性回归 首先,对于Y\u pred和Y,您的形状中存在一个问题: Y具有未知形状,并由形状数组(1000,) Y\u pred具有形状(1000,1) Y-Y\u pred将具有形状(10001000) 这段小代码将证明我的观点: a=tf.zeros([1000])#形状(1000,) b=tf.zeros([1000,1])#形状(1000,1) 打印(a-b)。获取_形

我试图在Tensorflow中实现多项式回归(二次)。损失并没有收敛。谁能帮我解决这个问题。不过,类似的逻辑也适用于线性回归

首先,对于
Y\u pred
Y
,您的形状中存在一个问题:

  • Y
    具有未知形状,并由形状数组
    (1000,)
  • Y\u pred
    具有形状
    (1000,1)
  • Y-Y\u pred
    将具有形状
    (10001000)
这段小代码将证明我的观点:

a=tf.zeros([1000])#形状(1000,)
b=tf.zeros([1000,1])#形状(1000,1)
打印(a-b)。获取_形状()#打印(10001000)

您应该使用一致的类型:

y_input=y_input.重塑((1000,1))
Y=tf.placeholder(tf.float32,shape=[None,1])

无论如何,损失是爆炸性的,因为您有非常高的值(输入值在0到100之间,您应该将其正常化),因此损失非常高(大约在培训开始时的
2000

梯度很高,参数爆炸,损耗达到无穷大


最快的解决办法是降低你的学习速度(
1e-5
对我来说是收敛的,尽管最后速度非常慢)。当损失收敛到大约
1

时,您可以将其提高。非常感谢。它就像一个符咒:)。此外,在将输入范围减小到0-2后,它会收敛到学习率“0.01”本身。
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)