【问题标题】:Linear Regression With Manual Gradient Computation手动梯度计算的线性回归
【发布时间】:2017-04-08 03:13:51
【问题描述】:

我了解代码 1 是使用 tf.train.GradientDescentOptimizer 进行线性回归的代码,属于 TensorFlow 库(黑盒)。

代码 2 是一个代码示例,可以在没有 GradientDescentOptimizer 的情况下执行相同的操作。 是没有黑框的代码。

我想在代码 2 中添加偏差 (# hypothesis = X * W + b)。在这种情况下,代码(梯度、下降、更新等)应该如何?

代码 1

import tensorflow as tf

x_train = [1, 2, 3]
y_train = [1, 2, 3]

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
W = tf.Variable(5.)
b = tf.Variable(5.)
hypothesis = X * W + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
learning_rate = 0.1

optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
gvs = optimizer.compute_gradients(cost, [W, b])
apply_gradients = optimizer.apply_gradients(gvs)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(21):
    gradient_val, cost_val, _ = sess.run(
        [gvs, cost, apply_gradients], feed_dict={X: x_train, Y: y_train})
    print("%3d Cost: %10s, W': %10s, W: %10s, b': %10s, b: %10s" %
          (step, round(cost_val, 5),
           round(gradient_val[0][0] * learning_rate, 5), round(gradient_val[0][1], 5),
           round(gradient_val[1][0] * learning_rate, 5), round(gradient_val[1][1], 5)))

代码2

import tensorflow as tf

x_train = [1, 2, 3]
y_train = [1, 2, 3]

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
W = tf.Variable(5.)
# b = tf.Variable(5.)  # Bias
hypothesis = X * W
# hypothesis = X * W + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
learning_rate = 0.1

gradient = tf.reduce_mean((W * X - Y) * X) * 2
descent = W - learning_rate * gradient
update = tf.assign(W, descent)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(W))
for step in range(21):
    gradient_val, update_val, cost_val = sess.run(
        [gradient, update, cost], feed_dict={X: x_train, Y: y_train})
    print(step, gradient_val * learning_rate, update_val, cost_val)

【问题讨论】:

  • 一个非常有趣的问题!

标签: tensorflow linear-regression


【解决方案1】:

我已推荐An Introduction to Gradient Descent and Linear Regression

代码 2

import tensorflow as tf

x_train = [1, 2, 3]
y_train = [1, 2, 3]

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
W = tf.Variable(5.)
b = tf.Variable(5.)
hypothesis = X * W + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
learning_rate = 0.1

W_gradient = tf.reduce_mean((W * X + b - Y) * X) * 2
b_gradient = tf.reduce_mean(W * X + b - Y) * 2
W_descent = W - learning_rate * W_gradient
b_descent = b - learning_rate * b_gradient
W_update = tf.assign(W, W_descent)
b_update = tf.assign(b, b_descent)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(21):
    cost_val, W_gradient_val, W_update_val, b_gradient_val, b_update_val = sess.run(
        [cost, W_gradient, W_update, b_gradient, b_update],
        feed_dict={X: x_train, Y: y_train})
    print("%3d Cost: %8s, W': %8s, W: %8s, b': %8s, b: %8s" %
          (step, round(cost_val, 5),
           round(W_gradient_val * learning_rate, 5), round(W_update_val, 5),
           round(b_gradient_val * learning_rate, 5), round(b_update_val, 5)))

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多