forked from LufsX/Experiment-LogReg
This commit is contained in:
+22
-11
@@ -17,12 +17,14 @@ def compute_gradient(X_batch, y_batch, weights, lambda_reg):
|
||||
gradient += (lambda_reg / len(y_batch)) * weights # L2正则化
|
||||
return gradient
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=20000, batch_size=64, lambda_reg=0.01, patience=5):
|
||||
"""对率回归训练函数。"""
|
||||
m, n = X.shape
|
||||
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
|
||||
print("开始训练对率回归模型")
|
||||
|
||||
best_weights = weights.copy()
|
||||
best_accuracy = 0
|
||||
wait = 0
|
||||
|
||||
for i in range(iterations):
|
||||
indices = np.random.permutation(m)
|
||||
X_shuffled = X[indices]
|
||||
@@ -33,11 +35,20 @@ def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lam
|
||||
y_batch = y_shuffled[j:j + batch_size]
|
||||
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
if i % 1000 == 0: # 每1000次迭代打印一次信息
|
||||
print(f"迭代 {i}: 当前权重形状: {weights.shape}")
|
||||
|
||||
return weights
|
||||
# 早停法:在验证集上评估模型性能
|
||||
accuracy = np.mean([1 if sigmoid(np.dot(X[i], weights)) >= 0.5 and y[i] == 1 or sigmoid(np.dot(X[i], weights)) < 0.5 and y[i] == 0 else 0 for i in range(m)])
|
||||
if accuracy > best_accuracy:
|
||||
best_accuracy = accuracy
|
||||
best_weights = weights.copy()
|
||||
wait = 0
|
||||
else:
|
||||
wait += 1
|
||||
if wait >= patience:
|
||||
print(f"Early stopping at iteration {i+1}")
|
||||
break
|
||||
|
||||
return best_weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""对率回归预测函数。"""
|
||||
@@ -81,7 +92,7 @@ def load_data(filepath):
|
||||
print(f"Data loaded successfully, data shape: {data.shape}, label shape: {target_variable.shape}")
|
||||
return data, target_variable
|
||||
|
||||
def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
|
||||
def k_fold_cross_validation(X, y, k=5, learning_rate=0.01, iterations=20000, batch_size=64, lambda_reg=0.01):
|
||||
"""K折交叉验证。"""
|
||||
m = X.shape[0]
|
||||
fold_size = m // k
|
||||
@@ -117,7 +128,7 @@ if __name__ == "__main__":
|
||||
X_train, mean, std = standardize_data(X_train)
|
||||
|
||||
# K折交叉验证
|
||||
avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=1000)
|
||||
avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=20000)
|
||||
print(f"Average accuracy of K-fold cross-validation: {avg_accuracy:.4f}")
|
||||
|
||||
# 加载测试集
|
||||
@@ -128,10 +139,10 @@ if __name__ == "__main__":
|
||||
exit()
|
||||
|
||||
# 测试集也需要进行相同的标准化
|
||||
X_test = (X_test - mean) / std # 使用训练集的均值和标准差进行标准化
|
||||
X_test = (X_test - mean) / std
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=20000)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
|
||||
Reference in New Issue
Block a user