generated from LufsX/Experiment-LogReg
This commit is contained in:
+5
-2
@@ -50,15 +50,18 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, lambd
|
||||
weights = np.zeros(n)
|
||||
|
||||
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]
|
||||
y_batch = y[indices]
|
||||
|
||||
# 计算预测和梯度
|
||||
z = np.dot(X_batch, weights)
|
||||
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
|
||||
|
||||
return weights
|
||||
|
||||
Reference in New Issue
Block a user