Python实现批量梯度下降法:抛物面回归案例
Python实现批量梯度下降法:抛物面回归案例本文将以抛物面回归问题为例,介绍如何使用批量梯度下降法学习模型参数,并提供完整的Python代码实现。### 问题描述假设有一个包含100,000个样本的数据集 ${(x_i,y_i,z_i)}_{i=1}^{100000}$,其中 $z_i$ 为标签。目标是使用批量梯度下降法学习抛物面回归模型 $f(x,y)=//frac{x^2}{ω_1^2}+//frac{y^2}{ω_2^2}$ 的参数 $ω_1$ 和 $ω_2$。训练过程将使用100个mini-batches,迭代5个epochs,并采用均方误差作为损失函数。### 算法推导1. 损失函数: 均方误差 (MSE) 定义为: MSE = //frac{1}{M} //sum_{i=1}^{M} (f(x_i, y_i) - z_i)^2 2. 偏导数: 为了更新参数,我们需要计算损失函数关于 $ω_1$ 和 $ω_2$ 的偏导数: //frac{∂MSE}{∂ω_1} = //frac{-4}{Mω_1^3} //sum_{i=1}^{M} (z_i - f(x_i, y_i))x_i^2 //frac{∂MSE}{∂ω_2} = //frac{-4}{Mω_2^3} //sum_{i=1}^{M} (z_i - f(x_i, y_i))y_i^2 3. 参数更新: 使用以下公式更新参数: ω_1 = ω_1 - learning/_rate * //frac{∂MSE}{∂ω_1} ω_2 = ω_2 - learning/_rate * //frac{∂MSE}{∂ω_2} ### Python代码实现pythonimport numpy as np# 定义抛物面回归函数def f(x, y, w1, w2): return (x2) / (w12) + (y2) / (w22)# 定义均方误差损失函数def mse_loss(predictions, targets): return np.mean((predictions - targets)2)# 定义批量梯度下降算法def batch_gradient_descent(x, y, z, learning_rate, num_epochs, batch_size): # 初始化参数 w1 = 1.0 w2 = 1.0 num_samples = len(x) num_batches = num_samples // batch_size # 迭代训练 for epoch in range(num_epochs): # 打乱数据集的顺序 indices = np.random.permutation(num_samples) shuffled_x = x[indices] shuffled_y = y[indices] shuffled_z = z[indices] # 分批次训练 for batch in range(num_batches): # 获取当前批次的数据 start = batch * batch_size end = start + batch_size batch_x = shuffled_x[start:end] batch_y = shuffled_y[start:end] batch_z = shuffled_z[start:end] # 计算预测值 predictions = f(batch_x, batch_y, w1, w2) # 计算梯度 d_w1 = (-4/(batch_size * w13)) * np.sum((batch_z - predictions) * (batch_x2)) d_w2 = (-4/(batch_size * w23)) * np.sum((batch_z - predictions) * (batch_y2)) # 更新参数 w1 -= learning_rate * d_w1 w2 -= learning_rate * d_w2 # 打印每个epoch的损失 predictions = f(x, y, w1, w2) loss = mse_loss(predictions, z) print('Epoch {}/{} - Loss: {:.4f}'.format(epoch+1, num_epochs, loss)) return w1, w2# 生成示例数据M = 100000x = np.random.uniform(low=-1, high=1, size=M)y = np.random.uniform(low=-1, high=1, size=M)z = (x2) + (y**2) # 生成标签数据# 设置超参数learning_rate = 0.01num_epochs = 5batch_size = 1000# 执行批量梯度下降算法w1, w2 = batch_gradient_descent(x, y, z, learning_rate, num_epochs, batch_size)# 输出训练得到的参数print('ω₁: {:.4f}'.format(w1))print('ω₂: {:.4f}'.format(w2))### 代码解读1. 导入库: 代码首先导入了 numpy 库,用于进行数值计算。2. 定义函数: 定义了三个函数: - f(x, y, w1, w2):抛物面回归函数。 - mse_loss(predictions, targets):计算均方误差。 - batch_gradient_descent(...):实现批量梯度下降算法。3. 生成数据: 使用 np.random.uniform() 生成示例数据,包括特征 x,y 和标签 z。4. 设置超参数: 设置学习率 learning_rate,迭代次数 num_epochs 和批次大小 batch_size。5. 训练模型: 调用 batch_gradient_descent() 函数训练模型,并输出每个epoch的损失值。6. 输出结果: 最后,打印训练得到的参数 ω₁ 和 ω₂。### 总结本文介绍了如何使用批量梯度下降法解决抛物面回归问题,并提供了完整的Python代码实现。你可以根据自己的数据和模型修改代码,以适应不同的应用场景。
原文地址: https://www.cveoy.top/t/topic/byzq 著作权归作者所有。请勿转载和采集!