tensorflow 2.0 随机梯度下降 之 函数优化实例

版权声明:本文为博主([email protected])原创文章,未经博主允许不得转载。 https://blog.csdn.net/z_feng12489/article/details/89918718

Himmelblau 函数

f ( x , y ) = ( x 2 + y 11 ) 2 + ( x + y 2 7 ) 2 f(x,y)=(x^2+y-11)^2+(x+y^2-7)^2
在这里插入图片描述
在这里插入图片描述

Minima 四个最小值点

  • f ( 3.0 , 2.0 ) = 0.0 f(3.0, 2.0) = 0.0
  • f ( 2.805118 , 3.131312 ) = 0.0 f(-2.805118, 3.131312) = 0.0
  • f ( 3.779310 , 3.283186 ) = 0.0 f(-3.779310, -3.283186) = 0.0
  • f ( 3.584428 , 1.848126 ) = 0.0 f(3.584428, -1.848126) = 0.0

打印观测

def himmelblau(x):
    return (x[0] ** 2 + x[1] - 11) ** 2 + (x[0] + x[1] ** 2 - 7) ** 2

x = np.arange(-6, 6, 0.1)
y = np.arange(-6, 6, 0.1)
print('x,y range:', x.shape, y.shape)
X, Y = np.meshgrid(x, y)
print('X,Y maps:', X.shape, Y.shape)
Z = himmelblau([X, Y])

fig = plt.figure('himmelblau')
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z)
ax.view_init(60, -30)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()

在这里插入图片描述

梯度下降

# [1., 0.], [-4, 0.], [-3, 0.]
x = tf.constant([4., 0.])

for step in range(200):

    with tf.GradientTape() as tape:
        tape.watch([x])
        y = himmelblau(x)

    grads = tape.gradient(y, [x])[0] 
    x -= 0.01*grads

    if step % 20 == 0:
        print ('step {}: x = {}, f(x) = {}'
               .format(step, x.numpy(), y.numpy()))
# initial x = 4.0, 0
step 180: x = [ 3.5844283 -1.8481264], f(x) = 1.818989620386291e-12

# initial x = 1.0, 0
step 180: x = [3.0000002 1.9999996], f(x) = 1.818989620386291e-12

# initial x = -4.0, 0
step 180: x = [-3.7793102 -3.283186 ], f(x) = 0.0

# initial x = -3.0, 0
step 180: x = [-2.805118   3.1313126], f(x) = 2.273736618907049e-13

猜你喜欢

转载自blog.csdn.net/z_feng12489/article/details/89918718
今日推荐