From b880be4230a183413bfa8d3a225f00461ec9553c Mon Sep 17 00:00:00 2001 From: wzy <675163717@qq.com> Date: Thu, 24 Oct 2024 17:22:19 +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 | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/function.py b/function.py index da19929..b5f0967 100644 --- a/function.py +++ b/function.py @@ -29,16 +29,29 @@ name = "万子玉" def sigmoid(z): return 1 / (1 + np.exp(-z)) -def train_logreg(X, y, learning_rate=0.01, iterations=1000): +def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32, initial_weights=None): X = np.insert(X, 0, 1, axis=1) # 添加偏置项 - weights = np.zeros(X.shape[1]) + if initial_weights is None: + weights = np.zeros(X.shape[1]) + else: + weights = initial_weights + m = X.shape[0] for _ in range(iterations): - z = np.dot(X, weights) - h = sigmoid(z) - gradient = np.dot(X.T, (h - y)) / m - weights -= learning_rate * gradient + indices = np.arange(m) + np.random.shuffle(indices) + X_shuffled = X[indices] + y_shuffled = y[indices] + + for i in range(0, m, batch_size): + end = i + batch_size + X_batch = X_shuffled[i:end] + y_batch = y_shuffled[i:end] + z = np.dot(X_batch, weights) + h = sigmoid(z) + gradient = np.dot(X_batch.T, (h - y_batch)) / batch_size + weights -= learning_rate * gradient return weights """ @@ -105,15 +118,29 @@ def load_data(filepath): if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 # 加载训练集 X_train, y_train = load_data("./data/horse-colic.data") # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") + # 模型调优参数 + learning_rates = [0.01, 0.001, 0.0001] + iterations = [500, 1000, 2000] + batch_sizes = [16, 32, 64] + initial_weights = np.random.randn(X_train.shape[1] + 1) # 随机初始化权重 + + # 选择一组参数进行训练 + chosen_learning_rate = learning_rates[1] + chosen_iterations = iterations[1] + chosen_batch_size = batch_sizes[1] + # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) + weights = train_logreg(X_train, y_train, + learning_rate=chosen_learning_rate, + iterations=chosen_iterations, + batch_size=chosen_batch_size, + initial_weights=initial_weights) # 测试单个数据 y_pred = predict_logreg(X_test[11], weights)