From d9385b0323eea575212a3f0c16433a3d3a79a563 Mon Sep 17 00:00:00 2001 From: cy <939601402@qq.com> Date: Thu, 24 Oct 2024 17:21:10 +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 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/function.py b/function.py index ed75430..daa5782 100644 --- a/function.py +++ b/function.py @@ -28,7 +28,7 @@ def sigmoid(z): return 1.0 / (1 + np.exp(-z)) # 这里写自己的代码实现 -def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32): +def train_logreg(X, y, learning_rate=0.01, iterations=1000): """ 对率回归训练函数。 @@ -42,15 +42,15 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32): """ m, n = X.shape weights = np.zeros(n) + # 用于Adagrad的累积平方梯度 + r = np.zeros(n) + epsilon = 1e-8 for i in range(iterations): - for j in range(0, m, batch_size): - end = min(j + batch_size, m) - X_batch = X[j:end] - y_batch = y[j:end] - z = np.dot(X_batch, weights) - h = sigmoid(z) - gradient = np.dot(X_batch.T, (h - y_batch)) / len(X_batch) - weights -= learning_rate * gradient + z = np.dot(X, weights) + h = sigmoid(z) + gradient = np.dot(X.T, (h - y)) / m + r += gradient ** 2 + weights -= learning_rate * gradient / (np.sqrt(r) + epsilon) return weights def predict_logreg(inX, weights):