generated from LufsX/Experiment-LogReg
This commit is contained in:
+8
-2
@@ -24,9 +24,15 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01):
|
|||||||
z = np.dot(X, weights)
|
z = np.dot(X, weights)
|
||||||
predictions = 1 / (1 + np.exp(-z))
|
predictions = 1 / (1 + np.exp(-z))
|
||||||
gradient = np.dot(X.T, (predictions - y)) / m
|
gradient = np.dot(X.T, (predictions - y)) / m
|
||||||
gradient += (lambda_reg / m) * weights # L2正则化
|
|
||||||
|
# L2正则化:将正则项添加到梯度上
|
||||||
|
gradient += (lambda_reg / m) * weights
|
||||||
|
|
||||||
|
# 更新权重
|
||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
learning_rate *= 0.99 # 动态调整学习率
|
|
||||||
|
# 动态调整学习率
|
||||||
|
learning_rate *= 0.99 # 每次迭代将学习率乘以0.99
|
||||||
|
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user