神經網絡入門-迴歸問題(梯度下降法)-python實現

程序實現的功能

給定一些點,擬合出迴歸直線,數據在百度雲鏈接
在這裏插入圖片描述
1.以numpy格式讀取csv文件

 points = np.genfromtxt("data.csv", delimiter=",")
 print(points)

打印一下point看一下numpy格式

在這裏插入圖片描述
2.初始化直線的參數 w,b,直線的形式如圖所示,初始化w和b都爲0
在這裏插入代碼片在這裏插入圖片描述

 initial_b = 0 # initial y-intercept guess
 initial_w = 0 # initial slope guess

3.計算損失函數(loss)

def compute_error_for_line_given_points(b, w, points):
    totalError = 0
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        # computer mean-squared-error
        totalError += (y - (w * x + b)) ** 2
    # average loss for each point
    return totalError / float(len(points))

其中,這兩句是numpy的調用格式

   x = points[i, 0]#相當於points[i][0],表示第i個點的第x座標
   y = points[i, 1]#相當於points[i][1],表示第i個點的y座標

這是我們構建的損失函數的形式,也就是損失平方和,再除以N

totalError += (y - (w * x + b)) ** 2

4.更新 b,w的值,採用梯度下降的方法,如圖所示,w‘代表新的w值
在這裏插入圖片描述

def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
    b = starting_b
    w = starting_w
    # update for several times
    for i in range(num_iterations):
        b, w = step_gradient(b, w, np.array(points), learning_rate)
    return [b, w]

loss函數對b,對w求偏導

 b_gradient += (2/N) * ((w_current * x + b_current) - y)
 w_gradient += (2/N) * x * ((w_current * x + b_current) - y)
def step_gradient(b_current, w_current, points, learningRate):
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        # grad_b = 2(wx+b-y)
        b_gradient += (2/N) * ((w_current * x + b_current) - y)
        # grad_w = 2(wx+b-y)*x
        w_gradient += (2/N) * x * ((w_current * x + b_current) - y)
    # update w'
    new_b = b_current - (learningRate * b_gradient)
    new_w = w_current - (learningRate * w_gradient)
    return [new_b, new_w]
 new_b = b_current - (learningRate * b_gradient)
 new_w = w_current - (learningRate * w_gradient)

完整代碼和數據打包到百度雲:
鏈接:https://pan.baidu.com/s/1vWlsyR9BykcXVM1BYIQJuw
提取碼:pj2z

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章