[Python] theta values for gradient descent not coherent

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    i made a gradient descent code but it doesnt seem to work well

    import numpy as np
    from random import randint,random
    import matplotlib . pyplot as plt


    def calculh(theta, X):
    h = 0
    h+=theta[0]*X # w*X
    h+= theta[-1] # +b
    return h


    def calculY(sigma, h) :
    return sigma(h) # sigma peut-etre tanh, signoide etc.


    def erreurJ(theta, sigma):
    somme = 0
    somme = 1/4*(sigma(theta[1])**2+sigma(theta[0]+theta[1])**2)
    return somme


    def gradient(X, Y, Ysol, sigmaprime, h):
    return ((Y-Ysol)*sigmaprime(h)*X ,(Y-Ysol)*sigmaprime(h)*1)
    def grad(theta):
    w,b = theta[0],theta[1]
    #print(theta)
    return [2*b**3+3*b**2*w+3*b*w**2-2*b+w**3-w,b**3+3*b**2*w+3*b*w**2-b+w**3-w]
    # *X correspond a 0 ou 1 : nos 2 entrées ; *1 correspond a derivee de b

    def pasfixe(theta, eta, epsilon, X, Y, Ysol, sigma, sigmaprime, h):
    n=0
    while np.linalg.norm(gradient(X, Y, Ysol, sigmaprime, h)) > epsilon and n<10000 :
    for i in range(len(theta)) :
    theta = theta - eta*gradient(X, Y, Ysol, sigmaprime, h)
    h = calculh(theta, X)
    Y = calculY(sigma, h)
    n+=1
    if theta>100 : ### cas de divergence
    return [100,100],Y
    return theta,Y

    sigma = lambda z : z**2-1
    sigmaprime = lambda z : 2*z
    eta = 0.1

    X = 1
    Ysol = 0
    listeY = []
    listetheta = []
    lst = [[3*random()*(-1)**randint(0,1),3*random()*(-1)**randint(0,1)] for i in range(5000)]
    nb = 0
    for i in lst:
    nb+=1
    if nb%50 == 0:
    print(nb)
    theta = i[:]
    h = calculh(theta, X)
    Y = calculY(sigma, h)
    CalculTheta = pasfixe( theta, eta, 10**-4, X,Y, Ysol, sigma, sigmaprime, h)
    listetheta.append(CalculTheta[0])
    listeY.append(CalculTheta[1])


    for i in range (len(listeY)):
    listeY = round(listeY,2)
    print (listeY)

    for i in range (len(listetheta)):
    for j in range(2):
    listetheta[j] = round(listetheta[j],2)
    print (listetheta)

    for i in range(len(lst)):
    if [int(listetheta[0]),int(listetheta[1])] in [[-2,1]]:
    plt.plot(lst[0],lst[1],"bo")
    elif [int(listetheta[0]),int(listetheta[1])] in [[2,-1]]:
    plt.plot(lst[0],lst[1],"co")
    elif [int(listetheta[0]),int(listetheta[1])] in [[0,-1]]:
    plt.plot(lst[0],lst[1],"go")
    elif [int(listetheta[i][0]),int(listetheta[i][1])] in [[0,1]]:
    plt.plot(lst[i][0],lst[i][1],"mo")
    elif int(listetheta[i][0])**2 +int(listetheta[i][1])**2 >= 10:
    plt.plot(lst[i][0],lst[i][1],"ro")

    plt.show()


    in the end i make a graph with the bias and weight values ans each point is colored in function of what the theta (weight,bias) value given at the beginning of the loop is converging to. [URL='https://i.sstatic.net/W5ivHcwX.png']the graph i am supposed to have[/URL]

    i tried to calculate the gradient myself but it didnt work as well. i am supposed to get a graph like this one

    [url="https://stackoverflow.com/questions/79440021/theta-values-for-gradient-descent-not-coherent"]Continue reading...[/url][/i][/i][/i][/i][/i][/i][/i][/i]

Compartilhe esta Página