diff --git a/function.py b/function.py index d8d6277..bf6e2c2 100644 --- a/function.py +++ b/function.py @@ -37,13 +37,12 @@ def compute_gradient(X_batch, y_batch, weights, lambda_reg): z = np.dot(X_batch, weights) h = sigmoid(z) gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch) - # 添加L2正则化 - gradient += (lambda_reg / len(y_batch)) * weights + gradient += (lambda_reg / len(y_batch)) * weights # L2正则化 return gradient -def train_logreg(X, y, learning_rate=0.01, iterations=5000, batch_size=64, lambda_reg=0.01): +def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1): m, n = X.shape - weights = np.random.randn(n) * 0.01 + weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化 best_weights = weights.copy() best_accuracy = 0 @@ -109,7 +108,7 @@ if __name__ == "__main__": X_val = scaler.transform(X_val) # 训练模型 - final_weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000, batch_size=64, lambda_reg=0.1) + final_weights = train_logreg(X_train, y_train, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1) # 验证模型 val_accuracy = accuracy(X_val, y_val, final_weights)