From 5fcc553c832e6c9ead9c1b8dae073aa5cda7a42e Mon Sep 17 00:00:00 2001 From: zhangchenghong <1598349064@qq.com> Date: Tue, 12 Nov 2024 03:06:40 +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 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/function.py b/function.py index 03a582a..224365e 100644 --- a/function.py +++ b/function.py @@ -31,7 +31,7 @@ import numpy as np def sigmoid(z): return 1 / (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, batch_size=32, init_weights=None): """ 对率回归训练函数。 @@ -41,12 +41,16 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=32): - learning_rate (float): 学习率,默认为 0.01。 - iterations (int): 迭代次数,默认为 1000。 - batch_size (int): 小批量大小,默认为 32。 + - init_weights (numpy array): 权重初始值,默认为 None(即全零初始化)。 Returns: - numpy array: 训练得到的权重。 """ m, n = X.shape - weights = np.zeros(n) + if init_weights is None: + weights = np.zeros(n) + else: + weights = init_weights bias = 0 for _ in range(iterations): indices = np.random.permutation(m) @@ -113,9 +117,9 @@ if __name__ == "__main__": X_test, y_test = load_data("./data/horse-colic.test") # 训练对率回归模型 - weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32) # 修改批量大小 + init_weights = np.random.randn(X_train.shape[1]) # 随机初始化权重 + weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=32, init_weights=init_weights) # 测试单个数据 y_pred = predict_logreg(X_test[11], weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") -