import tensorflow as tf
M = tf.Variable([0.01],tf.float32)
b = tf.Variable([1.0],tf.float32)
#inputs and outputs
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32) # actual value of y which we already know
Yp = M * x + b # y predicted value
#loss
squareR = tf.square(Yp - y)
loss = tf.reduce_sum(squareR)
#optimize
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train,{x:[1,2,3,4,5],y:[1.9,2.4,3.7,4.9,5.1]})
print(sess.run([M,b]))
输出
[array([ 0.88999945], dtype=float32), array([ 0.93000191], dtype=float32)]
问题: 当我将x和y的值更改为
时x:[100,200,300,400,500],y:[19,24,37,49,51]
然后输出是:
[array([ nan], dtype=float32), array([ nan], dtype=float32)]
请帮助我获得线性模型的斜率和y截距。
答案 0 :(得分:0)
在训练循环中添加一些打印语句,我们可以看到训练期间发生了什么:
for i in range(1000):
_, mm, bb = sess.run([train,M,b],{x:[100,200,300,400,500],y:[19,24,37,49,51]})
print(mm, bb)
if np.isnan(mm):
break
print(sess.run([M,b]))
输出:
[ 1118.01000977] [ 4.19999981]
[-12295860.] [-33532.921875]
[ 1.35243170e+11] [ 3.68845632e+08]
[ -1.48755065e+15] [ -4.05696309e+12]
[ 1.63616896e+19] [ 4.46228634e+16]
[ -1.79963571e+23] [ -4.90810521e+20]
[ 1.97943407e+27] [ 5.39846559e+24]
[ -2.17719537e+31] [ -5.93781625e+28]
[ 2.39471499e+35] [ 6.53105210e+32]
[-inf] [-inf]
[ nan] [ nan]
这个输出意味着你的训练是分歧的。在这种情况下,降低学习率是解决问题的可能方法之一。
将学习率降低到0.000001
有效,这些是1000次迭代后学习的M和b:
[array([ 0.11159456], dtype=float32), array([ 1.01534212], dtype=float32)]