2017-10-05 58 views
0

我在学习tensorflow,并且修改了tensorflow在其网站上提供的线性回归代码,可以进行二次回归。然而,与其在线性模型中减少损失一样,损失会爆炸,我不知道为什么它会这样做。Tensorflow问题

代码,在蟒蛇2.7.12:

import tensorflow as tf 

# Model parameters 
A = tf.Variable([0], dtype=tf.float32) 
B = tf.Variable([0], dtype=tf.float32) 
C = tf.Variable([0], dtype=tf.float32) 
# Model input and output 
x = tf.placeholder(tf.float32) 
model = A*(x**2)+B*x+C 
y = tf.placeholder(tf.float32) 

# loss 
loss = tf.reduce_sum(tf.square(model - y)) # sum of the squares 
# optimizer 
optimizer = tf.train.GradientDescentOptimizer(0.01) 
train = optimizer.minimize(loss) 

# training data 
x_train = [0, 1, 2, 3] 
y_train = [0, 1, 4, 9] 
# training loop 
init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 
for i in range(100): 
    sess.run(train, {x: x_train, y: y_train}) 

# evaluate training accuracy 
curr_A, curr_B, curr_C, curr_loss = sess.run([A, B, C, loss], {x: x_train, y: y_train}) 
print("A: %s B: %s C: %s loss: %s"%(curr_A, curr_B, curr_C, curr_loss)) 

这里是输出:

A: [ -1.85999073e+10] B: [ -6.90063821e+09] C: [ -2.75790080e+09] loss: 4.55068e+22 

我在做什么错?谢谢。

编辑:发布错误的代码第一

+0

这可能是因为你现在在错误的每个数据点而言过冲。这可能会导致本地最小值在每次迭代中不断增加的数额。仔细看看tf.square(model - y),看看它是否按照您的预期工作可能会有好处。我的ML知识仅限于理论 - 特别是在TensorFlow方面 – Steve

+0

@Steve大部分是正确的。你确实帮助我专注于此。我检查了优化器,我传入的值太大了。使用0.001允许函数收敛。 – Jeff

回答

0

这种学习速度:

optimizer = tf.train.GradientDescentOptimizer(0.01) 

太大。

有:

optimizer = tf.train.GradientDescentOptimizer(0.001) 

输出为:

A: [ 0.99987358] B: [ 0.00042567] C: [-0.00021291] loss: 7.5869e-08