Skip to content
Rain Hu's Workspace
Go back

[AI] 提升模型的表現

Rain Hu

調整梯度下降的關鍵參數

learning_rate_comparison

學習率比較的程式碼
import numpy as np
import matplotlib.pyplot as plt

def objective_function(x, y):
    return 0.7*x**2 + 1.3*y**2

def gradient(x, y):
    return np.array([1.4*x, 2.6*y])

def gradient_descent(learning_rate, start_point, n_iterations=50):
    path = [start_point]
    point = np.array(start_point)
    
    for _ in range(n_iterations):
        grad = gradient(point[0], point[1])
        point = point - learning_rate * grad
        path.append(point.copy())
        
        if np.linalg.norm(grad) < 1e-6:
            break
    
    return np.array(path)

x = np.linspace(-10, 10, 100)  # 擴大範圍到 ±10
y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(x, y)
Z = objective_function(X, Y)

plt.figure(figsize=(15, 6))

learning_rates = [0.77, 0.1, 0.01]
titles = ['Learning Rate = 0.77', 'Learning Rate = 0.1', 'Learning Rate = 0.01']
start_point = np.array([4.0, 4.0])

for i, (lr, title) in enumerate(zip(learning_rates, titles)):
    plt.subplot(1, 3, i+1)
    
    plt.contour(X, Y, Z, levels=20, cmap='viridis')
    
    path = gradient_descent(lr, start_point)
    plt.plot(path[:, 0], path[:, 1], 'r.-', linewidth=1, markersize=3, 
             label=f'Gradient Descent Path\nIterations: {len(path)}')
    
    plt.plot(path[0, 0], path[0, 1], 'g*', markersize=10, label='Start')
    plt.plot(path[-1, 0], path[-1, 1], 'r*', markersize=10, label='End')
    
    plt.title(title)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.colorbar(label='Function Value')
    plt.grid(True)

plt.tight_layout()
plt.show()

使用不同的架構

提升模型容量(capacity)

capacity_comparison

用 n 次方程式來說明模型容量的差異程式碼
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# 生成數據
np.random.seed(42)
X = np.linspace(0, 1, 20).reshape(-1, 1)
y_true = np.sin(2 * np.pi * X)
y = y_true + np.random.normal(0, 0.1, X.shape)

# 訓練不同容量的模型
degrees = [1, 3, 15]
X_test = np.linspace(0, 1, 100).reshape(-1, 1)

plt.figure(figsize=(15, 5))
titles = ['Underfitting\n(Degree=1)', 'Good Fit\n(Degree=3)', 'Overfitting\n(Degree=15)']

for i, degree in enumerate(degrees):
    plt.subplot(1, 3, i+1)
    
    # 轉換特徵
    poly = PolynomialFeatures(degree)
    X_poly = poly.fit_transform(X)
    X_test_poly = poly.transform(X_test)
    
    # 訓練模型
    model = LinearRegression()
    model.fit(X_poly, y)
    y_pred = model.predict(X_test_poly)
    
    # 繪圖
    plt.scatter(X, y, color='blue', alpha=0.5, label='Training Data')
    plt.plot(X_test, y_pred, 'r-', label=f'Model')
    plt.plot(X_test, np.sin(2 * np.pi * X_test), 'g--', label='True Function')
    
    plt.title(titles[i])
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend()
    plt.grid(True)

plt.tight_layout()
plt.show()

Share this post on:

Previous
[AI] 提高普適化能力
Next
[AI] 評估模型