梯度下降算法求himmelblau极值点

# -*- coding: utf-8 -*-
"""
Created on Sun Mar  6 18:09:37 2022

@author: liufengyun
"""
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

def himmelblau(x):
    # himmelblau 函数实现,传入二维数组list
    return (x[0] ** 2 + x[1] - 11) ** 2 + (x[0] + x[1] ** 2 - 7) ** 2

# mesh grid 可视化
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)
Z = himmelblau([X, Y])

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


# right answers are:
# (3, 2), (-2.805, 3.131), (-3.779, -3.283), (3.584, -1.848)

x = tf.constant([4.0, 0.0])
for step in range(200):
    with tf.GradientTape() as tape:
        tape.watch([x])
        y = himmelblau(x)
    
    grads = tape.gradient(y, [x])
    #print(grads)
    x -= 0.01 * grads[0]
    if step % 20 == 19:
        print ('step={}, x={}, f(x)={}'.format(step, x.numpy(), y.numpy()))
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容