微信扫一扫打赏支持

TensorFlow2_200729系列---17、函数用梯度下降法求最值实例

TensorFlow2_200729系列---17、函数用梯度下降法求最值实例

一、总结

一句话总结:

从不同的初始点梯度下降,找到的极值点不一样
import  numpy as np
from    mpl_toolkits.mplot3d import Axes3D
from    matplotlib import pyplot as plt
import  tensorflow as tf



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.], [4, 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()))

 

 

二、函数用梯度下降法求最值实例

博客对应课程的视频位置:

 

import  numpy as np
from    mpl_toolkits.mplot3d import Axes3D
from    matplotlib import pyplot as plt
import  tensorflow as tf



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.], [4, 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()))
x,y range: (120,) (120,)
X,Y maps: (120, 120) (120, 120)
step 0: x = [ 3.26       -0.09999999], f(x) = 34.0
step 20: x = [ 3.54679   -1.4388103], f(x) = 2.747403860092163
step 40: x = [ 3.5843565 -1.8473401], f(x) = 1.7569736883160658e-05
step 60: x = [ 3.5844283 -1.8481257], f(x) = 2.3646862246096134e-11
step 80: x = [ 3.5844283 -1.8481264], f(x) = 2.2737367544323206e-13
step 100: x = [ 3.5844283 -1.8481264], f(x) = 2.2737367544323206e-13
step 120: x = [ 3.5844283 -1.8481264], f(x) = 2.2737367544323206e-13
step 140: x = [ 3.5844283 -1.8481264], f(x) = 2.2737367544323206e-13
step 160: x = [ 3.5844283 -1.8481264], f(x) = 2.2737367544323206e-13
step 180: x = [ 3.5844283 -1.8481264], f(x) = 2.2737367544323206e-13

详细步骤

In [2]:
import  numpy as np
from    mpl_toolkits.mplot3d import Axes3D
from    matplotlib import pyplot as plt
import  tensorflow as tf
from matplotlib import pylab
%pylab
Using matplotlib backend: Qt5Agg
Populating the interactive namespace from numpy and matplotlib
D:\software\coding\python_software\Anaconda3\lib\site-packages\IPython\core\magics\pylab.py:160: UserWarning: pylab import has clobbered these variables: ['step']
`%matplotlib` prevents importing * from pylab and numpy
  "\n`%matplotlib` prevents importing * from pylab and numpy"
In [4]:
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()
x,y range: (120,) (120,)
X,Y maps: (120, 120) (120, 120)

极值点(也是最值点)

f(3.0,2.0)=0.0,
f(-2.805118,3.131312)=0.0
f(-3.779310,-3.283186)=0.0
f(3.584428,-1.848126)=0.0
In [5]:
# [1., 0.], [-4, 0.], [4, 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()))
step 0: x = [-2.98       -0.09999999], f(x) = 146.0
step 20: x = [-3.6890159 -3.1276689], f(x) = 6.054703235626221
step 40: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 60: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 80: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 100: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 120: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 140: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 160: x = [-3.7793102 -3.283186 ], f(x) = 0.0
step 180: x = [-3.7793102 -3.283186 ], f(x) = 0.0
In [ ]:
 

 

 
posted @ 2020-08-05 02:41  范仁义  阅读(452)  评论(0编辑  收藏  举报