From 7cba228d95c3936543e0426fdaa8d65e96285892 Mon Sep 17 00:00:00 2001 From: dong <2938690875@qq.com> Date: Thu, 31 Oct 2024 16:47:52 +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 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/function.py b/function.py index fb0fb6d..ddd8604 100644 --- a/function.py +++ b/function.py @@ -47,6 +47,10 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000): z = np.dot(X, weights) predictions = 1 / (1 + np.exp(-z)) gradient = np.dot(X.T, (predictions - y)) / m + + # L2正则化 + gradient += (lambda_reg / m) * weights + weights -= learning_rate * gradient return weights