TensorFlow梯度下降

梯度的整个计算过程分为三步:

  • 计算梯度
  • 处理梯度
  • 应用梯度
import tensorflow as tf

x = tf.Variable(1.0)
y = x**2
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.1)
global_step = tf.Variable(0, trainable = False)
grads = optimizer.compute_gradients(loss = y) #计算梯度
grads_and_vars = []
#处理梯度
for i, (g, v) in enumerate(grads):
  if g is not None:
    grads_and_vars.append((tf.clip_by_norm(g, 1), v))
#应用梯度
train_op = optimizer.apply_gradients(grads_and_vars = grads_and_vars, global_step = global_step)
#train_op = optimizer.minimize(loss = y, global_step = global_step)
with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  while global_step.eval() < 50:
    sess.run(train_op)
    print('step : %d, y = %.4f, x = %.4f' % (global_step.eval(), y.eval(), x.eval()))
    print(g.eval())

你可能感兴趣的:(TensorFlow,梯度下降)