Page 1 of 1

Theta -Werte für Gradientenabfälle nicht kohärent

Posted: 16 Feb 2025, 15:06
by Anonymous
Ich habe einen Gradient -Abstiegscode erstellt, aber es scheint nicht gut zu funktionieren < /p>
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 n100 : ### 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[i][0],lst[i][1],"co")
elif [int(listetheta[i][0]),int(listetheta[i][1])] in [[0,-1]]:
plt.plot(lst[i][0],lst[i][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()
< /code>
Am Ende erstelle ich einen Diagramm mit den Verzerrungen und Gewichtswerten, und jeder Punkt ist in Funktion dessen, was der zu Beginn der Schleife angegeben ist, konvergieren zu. Ich soll ein Diagramm wie diesen bekommen