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