import numpy as np
# 实现Ridge回归模型
def initialize_params(dims):
"""
输入
:param dims: 训练数据变量维度
"""
# 初始化权重系数为零向量
w = np.zeros((dims, 1))
# 初始化偏置参数为零
b = 0
"""
w: 初始化权重系数值
b: 初始化偏置参数值
"""
return w, b
# 定义 Ridge 回归损失函数
def l2_loss(X, y, w, b, alpha):
"""
输入
:param X: 输入变量矩阵
:param y: 输出标签向量
:param w: 变量参数权重矩阵
:param b: 偏置
:param alpha: 正则化系数
"""
# 训练样本量
num_train = X.shape[0]
# 训练特征数
num_features = X.shape[1]
# 回归模型预测输出
y_hat = np.dot(X, w) + b
# L2损失函数
loss = np.sum((y_hat - y) ** 2) / num_train + alpha * np.sum(np.square(w))
# 参数梯度计算
dw = np.dot(X.T, (y_hat - y)) / num_train + 2 * alpha * w
db = np.sum((y_hat - y)) / num_train
"""
y_hat: 线性模型预测输出
loss: 均方损失值
dw: 权重系数一阶偏导
db: 偏置一阶偏导
"""
return y_hat, loss, dw, db
def ridge_train(X, y, learning_rate=0.01, epochs=1000):
"""
输入:
:param X: 输入变脸矩阵
:param y: 输出标签向量
:param learning_rate: 学习率
:param epochs: 训练迭代次数
"""
# 记录训练损失的空列表
loss_his = []
# 初始化模型参数
w, b = initialize_params(dims=X.shape[1])
params, grads = {}, {}
# 迭代训练
for i in range(1, epochs):
# 这里唏嘘该为 L2 Loss
y_hat, loss, dw, db = l2_loss(X=X, y=y, w=w, b=b, alpha=0.1)
# 基于梯度下降的参数更新
w += (-learning_rate * dw)
b += (-learning_rate * db)
# 记录当前迭代的损失
loss_his.append(loss)
# 每 50 次迭代打印当前损失信息
if 0 == (i % 50):
print(f'epoch: {i}, loss: {loss}.')
# 将当前迭代步优化吼的参数保存到字典中
params = {
'w': w,
'b': b
}
grads = {
'dw': dw,
'db': db
}
"""
loss_his: 每次迭代的L2损失列表
params: 优化后的参数字典
grads: 优化后的参数梯度字典
"""
return loss_his, params, grads
量化交易之机器学习篇 - 实现Ridge回归模型
最新推荐文章于 2024-08-14 11:14:24 发布