流程:給定麥子可以產(chǎn)出一定量的面包揭鳞,然后做一元回歸炕贵,進(jìn)行預(yù)測(cè)。
程序:在迭代次數(shù)中不斷求解成本函數(shù)(cost function)對(duì)b和m的偏導(dǎo)野崇,然后根據(jù)偏導(dǎo)更新b和m称开,來(lái)使得b和m 達(dá)到合適的位置,盡量使的cost function足夠小乓梨。
如圖
代碼
wheat_and_bread = [[0.5, 5], [0.6, 5.5], [0.8, 6], [1.1, 6.8], [1.4, 7]]#麥子產(chǎn)生出面包量的數(shù)據(jù)
#y=m*x+b
#梯度下降 給定b 和 m 訓(xùn)練數(shù)據(jù) 學(xué)習(xí)率
def step_gradient(b_current, m_current, points, learningRate):
b_gradient = 0
m_gradient = 0
N = float(len(points))
for i in range(0, len(points)):
x = points[i][0]
y = points[i][1]
#loss function (y - ((m_current * x) + b_current))^2
#分別對(duì) b 和 m 求偏導(dǎo),然后求平均 所有訓(xùn)練數(shù)據(jù)對(duì)b扶镀,m產(chǎn)生梯度的平均
b_gradient += -(2 / N) * (y - ((m_current * x) + b_current))
m_gradient += -(2 / N) * x * (y - ((m_current * x) + b_current))
#梯度下降 沿著梯度找最合適的 b和m
new_b = b_current - (learningRate * b_gradient)
new_m = m_current - (learningRate * m_gradient)
return [new_b, new_m]
#數(shù)據(jù) 初始 b蕴侣,m 學(xué)習(xí)率 迭代次數(shù)
def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
b = starting_b
m = starting_m
for i in range(num_iterations):
b, m = step_gradient(b, m, points, learning_rate)
return [b, m]
b,m=gradient_descent_runner(wheat_and_bread, 1, 1, 0.01, 100)
畫(huà)出損失函數(shù)和擬合函數(shù)
完整版
這里用了plotly包,作圖流程類(lèi)似R語(yǔ)言中的ggplot臭觉,便于人的理解昆雀。
wheat_and_bread = [[0.5, 5], [0.6, 5.5], [0.8, 6], [1.1, 6.8], [1.4, 7]]#麥子產(chǎn)生出面包量的數(shù)據(jù)
#y=m*x+b
#梯度下降 給定b 和 m 訓(xùn)練數(shù)據(jù) 學(xué)習(xí)率
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
plotly.tools.set_credentials_file(username='username', api_key='api_key')
def step_gradient(b_current, m_current, points, learningRate):
b_gradient = 0
m_gradient = 0
N = float(len(points))
for i in range(0, len(points)):
x = points[i][0]
y = points[i][1]
#loss function (y - ((m_current * x) + b_current))^2
#分別對(duì) b 和 m 求偏導(dǎo)辱志,然后求平均 所有訓(xùn)練數(shù)據(jù)對(duì)b,m產(chǎn)生梯度的平均
b_gradient += -(2 / N) * (y - ((m_current * x) + b_current))
m_gradient += -(2 / N) * x * (y - ((m_current * x) + b_current))
#梯度下降 沿著梯度找最合適的 b和m
new_b = b_current - (learningRate * b_gradient)
new_m = m_current - (learningRate * m_gradient)
return [new_b, new_m]
#數(shù)據(jù) 初始 b狞膘,m 學(xué)習(xí)率 迭代次數(shù)
def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
b = starting_b
m = starting_m
cost_list=[]
for i in range(num_iterations):
b, m = step_gradient(b, m, points, learning_rate)
cost_function = 0.0
for x,y in points:
cost_function+=(y-m*x-b)*(y-m*x-b)/len(points)
cost_list.append(cost_function)
#cost_function 不斷下降的散點(diǎn)圖
trace = go.Scatter(
x = list(range(num_iterations)),
y = cost_list,
mode = 'markers'
)
data=[trace]
py.iplot(data, filename='basic-scatter')
return [b, m]
b,m=gradient_descent_runner(wheat_and_bread, 1, 1, 0.01, 100)
#畫(huà)預(yù)測(cè)的圖
x=[]
y=[]
for i in range(0, len(wheat_and_bread)):
x.append(wheat_and_bread[i][0])
y.append(wheat_and_bread[i][1])
trace0=go.Scatter(
x = x,
y = y,
mode = 'markers',
name = 'markers'
)
x_predict=[]
y_predict=[]
for i in range(0, len(wheat_and_bread)):
x_predict.append(wheat_and_bread[i][0])
y_predict.append((wheat_and_bread[i][0])*m+b)
trace1=go.Scatter(
x=x_predict,
y=y_predict,
mode='lines+markers',
name='lines+markers'
)
data = [trace0, trace1]
py.iplot(data, filename='combine')