梯度下降算法python实现

def fun(x, y, w, b):
    l = 0
    for i in range(4):
        l += (y[i] - (b + w * x[i]))**2
    # l = sum(y - (b + w * x)**2 for x, y in zip(x, y)) / 8
    return l / 8


# 梯度下降法
def gradient_descent():
    times = 100 # 迭代次数
    alpha = 0.001 # 步长
    w = 0 # w的初始值
    b = 0 # b的初始值
    x = [0.0, 1.0, 2.0, 3.0]
    y = [3.1, 4.9, 7.2, 8.9]

    # 梯度下降算法
    for i in range(times):
        w = w - alpha / 4 * sum([x * ((b + w * x) - y) for x, y in zip(x, y)])
        b = b - alpha / 4 * sum([(b + w * x) - y for x, y in zip(x, y)])
        l = fun(x, y, w, b)
        print("第%d次迭代:w=%f,b=%f,l=%f" % (i + 1, w, b, l))


if __name__ == "__main__":
    gradient_descent()

参考:参考文章

上一篇:4


下一篇:如何让百度云里的资源不被和谐掉?