更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 11s

This commit is contained in:
2024-11-02 23:58:00 +08:00
parent da1762f042
commit 585d1f8b7b
+16 -22
View File
@@ -63,7 +63,7 @@ def load_data(filepath):
import numpy as np
def train_logreg(X, y, numIter=1000, learning_rate=0.01, batch_size=128, lambda_reg=0.01):
def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01):
"""
对率回归训练函数,加入L2正则化。
@@ -71,40 +71,34 @@ def train_logreg(X, y, numIter=1000, learning_rate=0.01, batch_size=128, lambda_
- X (numpy array): 特征数据。
- y (numpy array): 标签数据。
- numIter (int): 迭代次数。默认为1000。
- learning_rate (float): 学习率。默认为0.01。
- learning_rate (float): 学习率。默认为0.06。
- batch_size (int): 批量大小。默认为128。
- lambda_reg (float): L2正则化参数。默认为0.01。
Returns:
- numpy array: 训练得到的权重。
"""
m, n = X.shape
weights = np.zeros(n)
m, n = X.shape # m: 样本数, n: 特征数
weights = np.zeros(n) # 参数初始化
# 计算批次的总数
num_batches = int(np.ceil(m / batch_size))
for _ in range(numIter):
for j in range(numIter):
# 随机打乱样本顺序
dataIndex = np.random.permutation(m)
# 遍历所有批次
for i in range(num_batches):
start = i * batch_size
end = min((i + 1) * batch_size, m)
batch_X = X[dataIndex[start:end]]
batch_y = y[dataIndex[start:end]]
for i in range(0, m, batch_size):
end = min(i + batch_size, m)
# 批量梯度上升
batch_X = X[dataIndex[i:end]]
batch_y = y[dataIndex[i:end]]
# 计算梯度
z = np.dot(batch_X, weights)
h = 1 / (1 + np.exp(-z)) # sigmoid函数
error = batch_y - h
gradient = np.dot(batch_X.T, error) / batch_X.shape[0]
h = 1 / (1 + np.exp(-z)) # 计算h
error = batch_y - h # 计算误差
gradient = np.dot(batch_X.T, error) / len(batch_y) # 计算梯度
# 添加L2正则化
reg_term = lambda_reg * weights
gradient += reg_term
weights -= learning_rate * gradient # 更新权重
reg_term = 2 * lambda_reg * weights
gradient -= reg_term / len(batch_y)
weights += learning_rate * gradient # 更新权重
return weights