最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)梯度下降法

 更新時間:2020年03月24日 11:56:16   作者:u011021024  
這篇文章主要為大家詳細介紹了python實現(xiàn)梯度下降法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)梯度下降法的具體代碼,供大家參考,具體內(nèi)容如下

使用工具:Python(x,y) 2.6.6
運行環(huán)境:Windows10

問題:求解y=2*x1+x2+3,即使用梯度下降法求解y=a*x1+b*x2+c中參數(shù)a,b,c的最優(yōu)值(監(jiān)督學習)

訓練數(shù)據(jù):

x_train=[1, 2], [2, 1],[2, 3], [3, 5], [1,3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7]

y_train=[7, 8, 10, 14, 8, 13, 20, 16, 28,26]

測試數(shù)據(jù):

x_test = [1, 4],[2, 2],[2, 5],[5, 3],[1,5],[4, 1]

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 16 09:37:03 2016
@author: Jason
"""
 
import numpy as np
import matplotlib.pyplot as plt
 
# y=2 * (x1) + (x2) + 3 
 
rate = 0.001
x_train = np.array([[1, 2], [2, 1],[2, 3], [3, 5], [1, 3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7] ])
y_train = np.array([7, 8, 10, 14, 8, 13, 20, 16, 28, 26])
x_test = np.array([[1, 4],[2, 2],[2, 5],[5, 3],[1, 5],[4, 1]])
 
a = np.random.normal()
b = np.random.normal()
c = np.random.normal()
 
def h(x):
 return a*x[0]+b*x[1]+c
 
for i in range(100):
 sum_a=0
 sum_b=0
 sum_c=0
 
 for x, y in zip(x_train, y_train):  
  for xi in x:
   sum_a = sum_a+ rate*(y-h(x))*xi
   sum_b = sum_b+ rate*(y-h(x))*xi
   #sum_c = sum_c + rate*(y-h(x)) *1   
   
   a = a + sum_a
   b = b + sum_b
   c = c + sum_c
   plt.plot([h(xi) for xi in x_test])
 
 
print(a)
print(b)
print(c)
 
result=[h(xi) for xi in x_train]
print(result)
 
result=[h(xi) for xi in x_test]
print(result)
 
plt.show()

運行結(jié)果:

結(jié)論:線段是在逐漸逼近的,訓練數(shù)據(jù)越多,迭代次數(shù)越多就越逼近真實值。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

郧西县| 宁阳县| 舒兰市| 德昌县| 聂拉木县| 壶关县| 曲周县| 长丰县| 无棣县| 天等县| 潮安县| 哈密市| 诸城市| 佳木斯市| 且末县| 南投县| 略阳县| 湘潭县| 永嘉县| 丹凤县| 固始县| 景宁| 琼海市| 正安县| 石阡县| 增城市| 钦州市| 永年县| 成都市| 乌拉特后旗| 岳池县| 方正县| 平果县| 常山县| 乐山市| 合阳县| 灌云县| 贡嘎县| 禹州市| 滨海县| 仪陇县|