日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python學習記錄2——利用matplotlib動態(tài)顯示梯度下降法中的參數(shù)

系統(tǒng) 1961 0

python學習記錄2——利用matplotlib動態(tài)顯示梯度下降法中的參數(shù)

主要參考博客
利用matplotlib繪制多個實時刷新的動態(tài)圖表
https://blog.csdn.net/u013950379/article/details/87936999
【python】matplotlib動態(tài)顯示
https://blog.csdn.net/zyxhangiian123456789/article/details/89159530
深入淺出–梯度下降法及其實現(xiàn)
https://www.jianshu.com/p/c7e642877b0e
深度解讀最流行的優(yōu)化算法:梯度下降
https://www.cnblogs.com/shixiangwan/p/7532858.html

            
              import numpy as np
import matplotlib.pyplot as plt

# Size of the points dataset.
m = 20

# Points x-coordinate and dummy value (x0, x1).
X0 = np.ones((m, 1))
X1 = np.arange(1, m+1).reshape(m, 1)
X = np.hstack((X0, X1))

# Points y-coordinate
y = np.array([
    3, 4, 5, 5, 2, 4, 7, 8, 11, 8, 12,
    11, 13, 13, 16, 17, 18, 17, 19, 21
]).reshape(m, 1)

# The Learning Rate alpha.
alpha = 0.01314

def error_function(theta, X, y):
    '''Error function J definition.'''
    diff = np.dot(X, theta) - y
    return (1./2*m) * np.dot(np.transpose(diff), diff)

def gradient_function(theta, X, y):
    '''Gradient of the function J definition.'''
    diff = np.dot(X, theta) - y
    return (1./m) * np.dot(np.transpose(X), diff)

def gradient_descent(X, y, alpha):
    '''Perform gradient descent.'''
    theta = np.array([1, 1]).reshape(2, 1)
    plt.ion()
    gradient = gradient_function(theta, X, y)
    num = 0
    while not np.all(np.absolute(gradient) <= 1e-5):
        num += 1
        theta = theta - alpha * gradient
        gradient = gradient_function(theta, X, y)
        plt.clf()
        plt.suptitle(str(num) + "--" + str(theta), fontsize=9)
        plt.plot(X1, y, "r*")
        plt.plot([0, 20], [0, 20] * theta[1] + theta[0])
        plt.ylim((0, 20))
        plt.pause(0.4)
    plt.show()
    plt.ioff()
    return theta

optimal = gradient_descent(X, y, alpha)
print('optimal:', optimal)
print('error function:', error_function(optimal, X, y)[0,0])

            
          

更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 桐庐县| 申扎县| 昌平区| 孙吴县| 临夏县| 赤壁市| 永宁县| 贵定县| 渭南市| 大新县| 靖安县| 当涂县| 墨江| 丹江口市| 凤城市| 新安县| 新竹市| 阜阳市| 南川市| 和顺县| 同江市| 佛山市| 新晃| 瑞安市| 潜江市| 安陆市| 汶川县| 晋宁县| 扎鲁特旗| 当涂县| 怀宁县| 奉化市| 屏南县| 霍邱县| 江都市| 崇礼县| 昆明市| 监利县| 长汀县| 陵川县| 榆林市|