吴裕雄--天生自然TensorFlow2教程:函数优化实战
2024-09-06 08:21:01
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D def himmeblau(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(f'x_shape: {x.shape},y_shape: {y.shape}')
# 生成坐标点
X, Y = np.meshgrid(x, y)
print(f'X_shape: {X.shape},Y_shape: {Y.shape}')
Z = himmeblau([X, Y]) fig = plt.figure('himmelblau')
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z)
ax.view_init(60, -30)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
import tensorflow as tf x = tf.constant([-4.,0.]) for step in range(200):
with tf.GradientTape() as tape:
tape.watch([x])
y = himmeblau(x)
grads = tape.gradient(y,[x])[0]
x -= 0.01 * grads
if step % 20 == 0:
print(f'step: {step}, x: {x}, f(x): {y}')
最新文章
- 常用的shell脚本
- MVVM架构~knockoutjs系列之数组的$index和$data
- Python数据类型之“文本序列(Text Sequence)”
- javascript 创建对象
- [推荐]PaaS技术知识贴
- json-lib 之jsonConfig具体应用
- java_queue
- Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值
- C#操作串口总结
- C#中Internal class与静态类说明
- MongoDB深圳用户组线下活动召集
- 谨慎升级到HTTPS
- iOS原生和H5的相互调用
- JVM的总结
- 手把手教您将 libreoffice 移植到函数计算平台
- Spark基础-scala学习(五、集合)
- 【读书笔记】iOS-对iOS应用进行模糊测试
- 关于linux-Centos 7下mysql 5.7.9的rpm包的安装方式
- Django框架----用户认证auth模块
- 学习笔记37—WIN7系统本地连接没有有效的IP地址 电脑本地连接无有效ip配置怎么办