Forrest_Chen
蓝阳
2017-12-19 17:17

TensorFlow中的梯度下降函数的疑问

5
  • tensorflow

一段利用TensorFlow学习y=ax函数,其中a=10:

import tensorflow as tf
sess = tf.Session()

a = tf.Variable(tf.constant(0.))
x_val = 5.
x_data = tf.placeholder(dtype=tf.float32)
multiplication = tf.multiply(a, x_data)
loss = tf.square(tf.subtract(multiplication, 50.))
init = tf.global_variables_initializer()
sess.run(init)
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)
print('Optimizing a Multiplication Gate Output to 50.')
for i in range(10):
    sess.run(train_step, feed_dict={x_data: x_val})
    a_val = sess.run(a)
    mult_output = sess.run(multiplication, feed_dict={x_data: x_val})
    print(str(a_val) + ' * ' + str(x_val) + ' = ' + str(mult_output))


打印的结果如下
Optimizing a Multiplication Gate Output to 50.
5.0 * 5.0 = 25.0
7.5 * 5.0 = 37.5
8.75 * 5.0 = 43.75
9.375 * 5.0 = 46.875
9.6875 * 5.0 = 48.4375
9.84375 * 5.0 = 49.2188
9.92188 * 5.0 = 49.6094
9.96094 * 5.0 = 49.8047
9.98047 * 5.0 = 49.9023
9.99023 * 5.0 = 49.9512

但是按照梯度下降函数,第一次计算出来的a_val应该是2.5才对:

a := a + alpha (y - a*x)x
= 0 + 0.01 ( 50 - 0 * 5) 5 = 2.5

但是从输出的结果看第一步完后a=5,感觉TensorFlow的梯度下降函数被改成了
a := a + 2 * alpha (y -a*x)x, 这样第二步a才会变成7.5

求解答

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐