TensorFlow梯度下降求解最小值

# tensorflow 求解f(x)=(x-1)^2+(y-2)^2+10的最小值

import tensorflow as tf

# 定义变量
w = tf.Variable(tf.constant([10.]));
b = tf.Variable(tf.constant([10.]));

sum = tf.add(tf.square(tf.add(w, tf.constant([-1.0]))), tf.square(tf.add(b, tf.constant([-2.0]))));
min = tf.add(sum, tf.constant([10.]));

optimizer = tf.train.GradientDescentOptimizer(0.5)

train = optimizer.minimize(min)

# 初始化变量
init = tf.initialize_all_variables()

# 启动图 (graph)
sess = tf.Session()
sess.run(init)

sess.run(train)
print(sess.run(w))
print(sess.run(b))
print(sess.run(min))
TensorFlow梯度下降求解最小值_第1张图片
不如

你可能感兴趣的:(TensorFlow梯度下降求解最小值)