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
Python TensorFlow:优化器将nan作为输出_Python_Tensorflow_Deep Learning_Python 3.6_Gradient Descent - Fatal编程技术网

Python TensorFlow:优化器将nan作为输出

Python TensorFlow:优化器将nan作为输出,python,tensorflow,deep-learning,python-3.6,gradient-descent,Python,Tensorflow,Deep Learning,Python 3.6,Gradient Descent,我正在运行一个非常简单的tensorflow程序 W = tf.Variable([.3],tf.float32) b = tf.Variable([-.3],tf.float32) x = tf.placeholder(tf.float32) linear_model = W*x + b y = tf.placeholder(tf.float32) squared_error = tf.square(linear_model - y) loss = tf.reduce_sum(squa

我正在运行一个非常简单的tensorflow程序

W = tf.Variable([.3],tf.float32)
b = tf.Variable([-.3],tf.float32)
x = tf.placeholder(tf.float32)

linear_model = W*x + b

y = tf.placeholder(tf.float32)

squared_error = tf.square(linear_model - y)

loss = tf.reduce_sum(squared_error)

optimizer = tf.train.GradientDescentOptimizer(0.1)

train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as s:
    file_writer = tf.summary.FileWriter('../../tfLogs/graph',s.graph)
    s.run(init)
    for i in range(1000):
        s.run(train,{x:[1,2,3,4],y:[0,-1,-2,-3]})
    print(s.run([W,b]))
这给了我

[array([nan],dtype=float32),array([nan],dtype=float32)]


我做错了什么?

您使用的是
loss=tf。减少总和(平方误差)
而不是
reduce\u mean
。使用
reduce\u sum
时,当您有更多数据时,您的损失会更大,即使使用这个小示例,也意味着您的梯度足够大,足以导致您的模型发散


另一个可能导致这种问题的原因是你的学习率太高。在这种情况下,您也可以通过将学习率从0.1更改为0.01来解决此问题,但如果您仍然使用
reduce\u sum
,当您添加更多分数时,它将再次中断。

您使用的是
loss=tf。reduce\u sum(平方误差)
而不是
reduce\u mean
。使用
reduce\u sum
时,当您有更多数据时,您的损失会更大,即使使用这个小示例,也意味着您的梯度足够大,足以导致您的模型发散

另一个可能导致这种问题的原因是你的学习率太高。在这种情况下,您也可以通过将学习率从0.1更改为0.01来解决此问题,但如果您仍然使用
reduce\u sum
,当您添加更多分数时,它将再次中断