为什么在使用TensorFlow计算简单的线性回归时会得到[nan]? [英] Why do I get [nan] when using TensorFlow to calculate a simple linear regression?

查看:553
本文介绍了为什么在使用TensorFlow计算简单的线性回归时会得到[nan]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用TensorFlow计算简单的线性回归时,我得到[nan],包括:w,b和损失.

When I use TensorFlow to calculate a simple linear regression I get [nan], including: w, b and loss.

这是我的代码:

import tensorflow as tf

w = tf.Variable(tf.zeros([1]), tf.float32)
b = tf.Variable(tf.zeros([1]), tf.float32)
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

liner = w*x+b

loss = tf.reduce_sum(tf.square(liner-y))

train = tf.train.GradientDescentOptimizer(1).minimize(loss)

sess = tf.Session()

x_data = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000]
y_data = [265000, 324000, 340000, 412000, 436000, 490000, 574000, 585000, 680000]                                                    

sess.run(tf.global_variables_initializer())

for i in range(1000):
    sess.run(train, {x: x_data, y: y_data})

nw, nb, nloss = sess.run([w, b, loss], {x: x_data, y: y_data})

print(nw, nb, nloss)

输出:

[ nan] [ nan] nan

Process finished with exit code 0

为什么会发生这种情况,我该如何解决?

Why does this happen, and how can I fix it?

推荐答案

这给出了我认为的解释:

This gives the explanation I believe:

for i in range(10):
     print(sess.run([train, w, b, loss], {x: x_data, y: y_data}))

给出以下结果:

[None, array([  4.70380012e+10], dtype=float32), array([ 8212000.], dtype=float32), 2.0248419e+12] 
[None, array([ -2.68116614e+19], dtype=float32), array([ -4.23342041e+15], dtype=float32),
6.3058345e+29] 
[None, array([  1.52826476e+28], dtype=float32), array([  2.41304958e+24], dtype=float32), inf] [None, array([
-8.71110858e+36], dtype=float32), array([ -1.37543819e+33], dtype=float32), inf] 
[None, array([ inf], dtype=float32), array([ inf], dtype=float32), inf]

您的学习速度实在太大了,因此您在每次迭代中都会过正确"调整w的值(请参见其在负值和正值之间波动,并且绝对值会增加).您将获得越来越高的值,直到某些事物达到无穷大,从而产生Nan值.只是降低(很多)学习率.

Your learning rate is simply too big, so you "overcorrect" the value of w at each iteration (see as it oscillates between negative and positive, with increasing absolute value). You get higher and higher values, until something reaches infinity, which creates Nan values. Just lower (a lot) the learning rate.

这篇关于为什么在使用TensorFlow计算简单的线性回归时会得到[nan]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆