Implementatie van Gradient Descent in Python
Gradient descent volgt een eenvoudig maar krachtig idee: bewegen in de richting van de steilste afdaling om een functie te minimaliseren.
De wiskundige regel is:
theta = theta - alpha * gradient(theta)
Waarbij:
thetade parameter is die we optimaliseren;alphade leersnelheid (stapgrootte) is;gradient(theta)de gradiënt van de functie bijthetais.
1. Definieer de functie en de afgeleide
We beginnen met een eenvoudige kwadratische functie:
def f(theta):
return theta**2 # Function we want to minimize
De afgeleide (gradiënt) is:
def gradient(theta):
return 2 * theta # Derivative: f'(theta) = 2*theta
f(theta): dit is onze functie, en we willen de waarde van theta vinden die deze minimaliseert;gradient(theta): dit geeft de helling op elk punttheta, wat we gebruiken om de update-richting te bepalen.
2. Initialiseer de parameters voor gradient descent
alpha = 0.3 # Learning rate
theta = 3.0 # Initial starting point
tolerance = 1e-5 # Convergence threshold
max_iterations = 20 # Maximum number of updates
alpha(leersnelheid): bepaalt hoe groot elke stap is;theta(initiële schatting): het startpunt voor de afdaling;tolerance: wanneer de updates zeer klein worden, stoppen we;max_iterations: zorgt ervoor dat we niet oneindig blijven herhalen.
3. Gradëntendaling uitvoeren
for i in range(max_iterations):
grad = gradient(theta) # Compute gradient
new_theta = theta - alpha * grad # Update rule
if abs(new_theta - theta) < tolerance:
print("Converged!")
break
theta = new_theta
- Bereken de gradiënt bij
theta; - Werk
thetabij met behulp van de gradëntendaling-formule; - Stop wanneer de bijwerkingen te klein zijn (convergentie);
- Print elke stap om de voortgang te volgen.
4. Visualisatie van gradëntendaling
123456789101112131415161718192021222324252627282930313233343536373839import matplotlib.pyplot as plt import numpy as np def f(theta): return theta**2 # Function we want to minimize def gradient(theta): return 2 * theta # Derivative: f'(theta) = 2*theta alpha = 0.3 # Learning rate theta = 3.0 # Initial starting point tolerance = 1e-5 # Convergence threshold max_iterations = 20 # Maximum number of updates theta_values = [theta] # Track parameter values output_values = [f(theta)] # Track function values for i in range(max_iterations): grad = gradient(theta) # Compute gradient new_theta = theta - alpha * grad # Update rule if abs(new_theta - theta) < tolerance: break theta = new_theta theta_values.append(theta) output_values.append(f(theta)) # Prepare data for plotting the full function curve theta_range = np.linspace(-4, 4, 100) output_range = f(theta_range) # Plot plt.plot(theta_range, output_range, label="f(θ) = θ²", color='black') plt.scatter(theta_values, output_values, color='red', label="Gradient Descent Steps") plt.title("Gradient Descent Visualization") plt.xlabel("θ") plt.ylabel("f(θ)") plt.legend() plt.grid(True) plt.show()
Deze grafiek toont:
- De functiecurve f(θ)=θ2;
- Rode stippen die elke stap van gradient descent tot convergentie weergeven.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain how the learning rate affects convergence?
What happens if we change the initial value of theta?
Can you summarize the main steps of the gradient descent algorithm?
Awesome!
Completion rate improved to 1.96
Implementatie van Gradient Descent in Python
Veeg om het menu te tonen
Gradient descent volgt een eenvoudig maar krachtig idee: bewegen in de richting van de steilste afdaling om een functie te minimaliseren.
De wiskundige regel is:
theta = theta - alpha * gradient(theta)
Waarbij:
thetade parameter is die we optimaliseren;alphade leersnelheid (stapgrootte) is;gradient(theta)de gradiënt van de functie bijthetais.
1. Definieer de functie en de afgeleide
We beginnen met een eenvoudige kwadratische functie:
def f(theta):
return theta**2 # Function we want to minimize
De afgeleide (gradiënt) is:
def gradient(theta):
return 2 * theta # Derivative: f'(theta) = 2*theta
f(theta): dit is onze functie, en we willen de waarde van theta vinden die deze minimaliseert;gradient(theta): dit geeft de helling op elk punttheta, wat we gebruiken om de update-richting te bepalen.
2. Initialiseer de parameters voor gradient descent
alpha = 0.3 # Learning rate
theta = 3.0 # Initial starting point
tolerance = 1e-5 # Convergence threshold
max_iterations = 20 # Maximum number of updates
alpha(leersnelheid): bepaalt hoe groot elke stap is;theta(initiële schatting): het startpunt voor de afdaling;tolerance: wanneer de updates zeer klein worden, stoppen we;max_iterations: zorgt ervoor dat we niet oneindig blijven herhalen.
3. Gradëntendaling uitvoeren
for i in range(max_iterations):
grad = gradient(theta) # Compute gradient
new_theta = theta - alpha * grad # Update rule
if abs(new_theta - theta) < tolerance:
print("Converged!")
break
theta = new_theta
- Bereken de gradiënt bij
theta; - Werk
thetabij met behulp van de gradëntendaling-formule; - Stop wanneer de bijwerkingen te klein zijn (convergentie);
- Print elke stap om de voortgang te volgen.
4. Visualisatie van gradëntendaling
123456789101112131415161718192021222324252627282930313233343536373839import matplotlib.pyplot as plt import numpy as np def f(theta): return theta**2 # Function we want to minimize def gradient(theta): return 2 * theta # Derivative: f'(theta) = 2*theta alpha = 0.3 # Learning rate theta = 3.0 # Initial starting point tolerance = 1e-5 # Convergence threshold max_iterations = 20 # Maximum number of updates theta_values = [theta] # Track parameter values output_values = [f(theta)] # Track function values for i in range(max_iterations): grad = gradient(theta) # Compute gradient new_theta = theta - alpha * grad # Update rule if abs(new_theta - theta) < tolerance: break theta = new_theta theta_values.append(theta) output_values.append(f(theta)) # Prepare data for plotting the full function curve theta_range = np.linspace(-4, 4, 100) output_range = f(theta_range) # Plot plt.plot(theta_range, output_range, label="f(θ) = θ²", color='black') plt.scatter(theta_values, output_values, color='red', label="Gradient Descent Steps") plt.title("Gradient Descent Visualization") plt.xlabel("θ") plt.ylabel("f(θ)") plt.legend() plt.grid(True) plt.show()
Deze grafiek toont:
- De functiecurve f(θ)=θ2;
- Rode stippen die elke stap van gradient descent tot convergentie weergeven.
Bedankt voor je feedback!