From 889f4909b2411b1c9e9fb337f1133449505c4588 Mon Sep 17 00:00:00 2001 From: wujunjie <297081150@qq.com> Date: Tue, 29 Oct 2024 08:32:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/function.py b/function.py index 3cbbe90..a6761cd 100644 --- a/function.py +++ b/function.py @@ -37,8 +37,15 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.0): y_predicted = 1 / (1 + np.exp(-linear_output)) error = y - y_predicted - # 更新权重和偏置,添加正则化项 - weights += learning_rate * (error * X.T - regularization * weights) / m + + # 计算梯度(不包括正则化项) + gradient = np.dot(X.T, error) / m + + # 加上正则化项的梯度(注意这里是对weights的每个元素都加上-regularization*weight[i]) + gradient += -regularization * weights + + # 更新权重 + weights += learning_rate * gradient bias += learning_rate * np.sum(error) / m return weights, bias