【问题标题】:Curve fitting with gradient descent梯度下降曲线拟合
【发布时间】:2019-10-06 03:44:04
【问题描述】:

我编写了一些代码,对几个数据点执行梯度下降。 由于某种原因,曲线没有正确收敛,但我不知道为什么会这样。我总是以爆炸的尾巴结束。

我做错了其中一个计算吗?我真的陷入了局部最小值还是其他原因?

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

def estimate(weights, x, order):
    est = 0
    for i in range(order):
        est += weights[i] * x ** i
    return est

def cost_function(x, y, weights, m):
    cost = 0
    for i in range(m-1):
        cost += (((weights[i] * x ** i) - y) ** 2)
    return (np.sum(cost ** 2) / ( 2 * m ))

def descent(A, b, iterations, descent_rate, order):
    x = A.T[0]
    y = b.reshape(4)

    # features
    ones = np.vstack(np.ones(len(A)))
    x = np.vstack(A.T[0])
    x2 = np.vstack(A.T[0] ** 2)

    # Our feature matrix
    features = np.concatenate((ones,x,x2), axis = 1).T
    # Initialize our coefficients to zero
    weights = np.zeros(order + 1)
    m = len(y)

    # gradient descent
    for i in range(iterations):
        est = estimate(weights, x, order).T
        difference = est - y
        weights = weights + (-descent_rate * (1/m) * np.matmul(difference, features.T)[0])
        cost = cost_function(x, y, weights, m)
        print(cost)

    plt.scatter(x,y)
    u = np.linspace(0,3,100)
    plt.plot(u, (u ** 2) * weights[2] + u  * weights[1] + weights[0], '-')
    plt.show()

A = np.array(((0,1),
             (1,1),
             (2,1),
             (3,1)))

b = np.array((1,2,0,3), ndmin = 2 ).T

iterations = 150
descent_rate = 0.01
order = 2
descent(A, b, iterations, descent_rate, order)

我想避免陷入这样的最低限度。我尝试将初始权重设置为随机值,但无济于事,有时它会下降一点,但随后又给了我相同的行为。

这是我得到的情节之一:

这是通过最小二乘法得到的预期结果:

【问题讨论】:

    标签: python numpy regression curve-fitting gradient-descent


    【解决方案1】:

    你的estimate函数应该是

    def estimate(weights, x, order):
        est = 0
        for i in range(order+1):
            est += weights[i] * x ** i
        return est
    

    更好的是,由于订单信息已经存在于weights 向量的大小中,因此删除冗余:

    def estimate(weights, x):
        est = 0
        for i in range(len(weights)):
            est += weights[i] * x ** i
        return est
    

    这是我在使用您的代码并运行 2000 次迭代时得到的结果:

    【讨论】:

    • 非常感谢!明天早上你可能为我节省了几个小时的挫败感:D 最终权重 [1.4 -1.1 0.5] 它给我的结果与使用 numpy 的 polyfit 函数时相同。另外,出于好奇,你怎么能这么快找到错误?/
    • 很高兴知道!没有诀窍。在这样的代码中,我可能犯了我可能犯的大多数错误。随着时间的推移,发现问题变得自动。
    猜你喜欢
    • 2017-04-30
    • 2020-08-28
    • 2017-03-01
    • 2015-09-03
    • 1970-01-01
    • 2015-10-19
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多