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

This commit is contained in:
2024-10-31 15:12:40 +08:00
parent 356d31e45d
commit b966900456
+5 -2
View File
@@ -50,15 +50,18 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, lambd
weights = np.zeros(n) weights = np.zeros(n)
for _ in range(iterations): for _ in range(iterations):
# 确保 batch_size 不超过 m
effective_batch_size = min(batch_size, m)
# 随机选择一个小批量 # 随机选择一个小批量
indices = np.random.choice(m, batch_size, replace=False) indices = np.random.choice(m, effective_batch_size, replace=False)
X_batch = X[indices] X_batch = X[indices]
y_batch = y[indices] y_batch = y[indices]
# 计算预测和梯度 # 计算预测和梯度
z = np.dot(X_batch, weights) z = np.dot(X_batch, weights)
h = sigmoid(z) h = sigmoid(z)
gradient = np.dot(X_batch.T, (h - y_batch)) / batch_size + lambda_ * weights gradient = np.dot(X_batch.T, (h - y_batch)) / effective_batch_size + lambda_ * weights
weights -= learning_rate * gradient weights -= learning_rate * gradient
return weights return weights