优化算法详解与代码示例
优化算法是深度学习中的关键组成部分,用于调整神经网络的权重和偏置,以最小化损失函数的值。以下是常见的优化算法及其详细介绍和代码示例:
1. 梯度下降法 (Gradient Descent)
原理:
通过计算损失函数对参数的梯度,按照梯度下降的方向更新参数。
更新公式:
:学习率,控制步长大小。
:损失函数对参数的梯度。
类型:
- 批量梯度下降 (Batch Gradient Descent):
- 使用所有训练数据计算梯度。
- 优点:收敛稳定。
- 缺点:计算代价高,尤其在数据量大时。
- 随机梯度下降 (Stochastic Gradient Descent, SGD):
- 使用单个样本计算梯度。
- 优点:计算快,适用于大规模数据。
- 缺点:更新不稳定,容易震荡。
- 小批量梯度下降 (Mini-Batch Gradient Descent):
- 使用一小批样本计算梯度。
- 优点:权衡计算效率和收敛稳定性。
代码示例:
import numpy as np
# 损失函数 J(theta) = theta^2
def loss_function(theta):
return theta ** 2
# 损失函数的梯度
def gradient(theta):
return 2 * theta
# 梯度下降
def gradient_descent(initial_theta, learning_rate, epochs):
theta = initial_theta
for epoch in range(epochs):
grad = gradient(theta)
theta = theta - learning_rate * grad
print(f"Epoch {epoch + 1}, Theta: {theta}, Loss: {loss_function(theta)}")
return theta
gradient_descent(initial_theta=10, learning_rate=0.1, epochs=20)
运行结果:
Epoch 1, Theta: 8.0, Loss: 64.0
Epoch 2, Theta: 6.4, Loss: 40.96000000000001
Epoch 3, Theta: 5.12, Loss: 26.2144
Epoch 4, Theta: 4.096, Loss: 16.777216
Epoch 5, Theta: 3.2768, Loss: 10.73741824
Epoch 6, Theta: 2.62144, Loss: 6.871947673600001
Epoch 7, Theta: 2.0971520000000003, Loss: 4.398046511104002
Epoch 8, Theta: 1.6777216000000004, Loss: 2.8147497671065613
Epoch 9, Theta: 1.3421772800000003, Loss: 1.801439850948199
Epoch 10, Theta: 1.0737418240000003, Loss: 1.1529215046068475
Epoch 11, Theta: 0.8589934592000003, Loss: 0.7378697629483825
Epoch 12, Theta: 0.6871947673600002, Loss: 0.47223664828696477
Epoch 13, Theta: 0.5497558138880001, Loss: 0.3022314549036574
Epoch 14, Theta: 0.43980465111040007, Loss: 0.19342813113834073
Epoch 15, Theta: 0.35184372088832006, Loss: 0.12379400392853807
Epoch 16, Theta: 0.281474976710656, Loss: 0.07922816251426434
Epoch 17, Theta: 0.22517998136852482, Loss: 0.050706024009129186
Epoch 18, Theta: 0.18014398509481985, Loss: 0.03245185536584268
Epoch 19, Theta: 0.14411518807585588, Loss: 0.