zoukankan      html  css  js  c++  java
  • 函数优化实战

    Himmelblau function

    • f(x,y)=(x2+y11)2+(x+y27)2f(x,y)=(x2+y−11)2+(x+y2−7)2

    26-函数优化实战-2函数图像.jpg

    Minima

    • f(3.0,2.0)=0.0
    • f(-2.8,3.1)=0.0
    • f(-3.7,-3.2)=0.0
    • f(3.5,-1.84)=0.0

    Plot

    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()
    
    x_shape: (120,),y_shape: (120,)
    X_shape: (120, 120),Y_shape: (120, 120)
    

    png

    Gradient Descent

    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}')
    
    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
  • 相关阅读:
    mysql 数据库优化
    eclipse 自动 注释
    rpc 小例子
    几种基于HTTP协议的RPC性能比较
    spring 两个 properties
    xxx.properties获取方法
    Mysql 操作
    Java对象初始化详解
    Tomcat自动启动脚本
    数学工具 在 当代 和 未来 的 进化革命 的 可能性
  • 原文地址:https://www.cnblogs.com/abdm-989/p/14123334.html
Copyright © 2011-2022 走看看