zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    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}')
  • 相关阅读:
    ios version和build
    协议
    masonry
    加密
    coredata
    随机附魔笔记
    Mac下搭建AzerothCore遇到的坑
    cocospods 私服搭建
    网络营销工具
    WKWebView不能重定向打开新界面,解决办法
  • 原文地址:https://www.cnblogs.com/tszr/p/12228291.html
Copyright © 2011-2022 走看看