Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Implementatie van Gradient Descent in Python | Wiskundige Analyse
Wiskunde voor Data Science

bookImplementatie 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:

  • theta de parameter is die we optimaliseren;
  • alpha de leersnelheid (stapgrootte) is;
  • gradient(theta) de gradiënt van de functie bij theta is.

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 punt theta, 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 theta bij 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

123456789101112131415161718192021222324252627282930313233343536373839
import 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()
copy

Deze grafiek toont:

  • De functiecurve f(θ)=θ2f(θ) = θ^2;
  • Rode stippen die elke stap van gradient descent tot convergentie weergeven.
question mark

Wat is de update-regel voor gradient descent voor functie f?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 10

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 1.96

bookImplementatie 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:

  • theta de parameter is die we optimaliseren;
  • alpha de leersnelheid (stapgrootte) is;
  • gradient(theta) de gradiënt van de functie bij theta is.

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 punt theta, 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 theta bij 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

123456789101112131415161718192021222324252627282930313233343536373839
import 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()
copy

Deze grafiek toont:

  • De functiecurve f(θ)=θ2f(θ) = θ^2;
  • Rode stippen die elke stap van gradient descent tot convergentie weergeven.
question mark

Wat is de update-regel voor gradient descent voor functie f?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 10
some-alt