[Python] My gradient descent function is not working as expected

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    I try to model a logistic function to a dataset. The parameters for this function I am calculating using gradient descent, minimizing a loss function. Using scipy.curve_fit this works just fine, but when I try to recreate, the parameters are really of. Do you see why this is the case?

    def loss(fn, fnargs, target):
    # target is a dict holding the inputs as keys and outputs as values
    # fnargs are the function arguments, I try to compute
    err = 0
    for x in target:
    err += (fn(x, **fnargs) - target[x])**2
    return err / len(target)

    def gradientVector(fn, fnargs, fixedargs=None):
    gradient = {}
    for alpha in fnargs:
    argsPos = fnargs.copy()
    argsPos[alpha] += 0.001
    gradient[alpha] = ((fn(fnargs=argsPos, **fixedargs) - fn(fnargs=fnargs, **fixedargs)) / 0.001)
    return gradient

    # this will be the target function as a dictionary
    target_function = {}
    for i, x in enumerate(time_numeric):
    target_function[x] = y

    def curve_params(function, target):
    sig = signature(function).parameters.copy()
    (k := next(iter(sig)), sig.pop(k))
    for i, param in enumerate(sig):
    sig[param] = 1.0
    # print(function(t=0, **sig))
    error = loss(function, sig, target)

    for i in range(1000):

    g = gradientVector(loss, sig, fixedargs={'fn': function, 'target':target})
    for param in sig:
    sig[param] -= 0.001 * g[param]

    error = loss(function, sig, target)
    print(g, error)

    return sig


    and the code for rendering:

    fig, ax = plt.subplots()
    ax.scatter(times, y, s=10, c='blue') # Increased size for visibility
    ax.plot(times, trend_line, color='red', label='Power Law Fit')
    sig = curve_params(logistic, target_function)
    predicted_function = logistic(time_numeric, **sig)
    ax.plot(times, mytrendline, color='green')


    ax.set_title('...')
    ax.set_xlabel('Date')
    ax.set_ylabel('Volume')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()


    which produces scipy's curve in red and mine in green: Plot of the dataset, the target function in red and my approximation in green

    Continue reading...

Compartilhe esta Página