更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 12s

This commit is contained in:
cy
2024-10-24 17:18:22 +08:00
parent 64ee170c18
commit 23921f47c6
+9 -5
View File
@@ -28,7 +28,7 @@ def sigmoid(z):
return 1.0 / (1 + np.exp(-z)) return 1.0 / (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):
""" """
对率回归训练函数。 对率回归训练函数。
@@ -43,10 +43,14 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
m, n = X.shape m, n = X.shape
weights = np.zeros(n) weights = np.zeros(n)
for i in range(iterations): for i in range(iterations):
z = np.dot(X, weights) for j in range(0, m, batch_size):
h = sigmoid(z) end = min(j + batch_size, m)
gradient = np.dot(X.T, (h - y)) / m X_batch = X[j:end]
weights -= learning_rate * gradient 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
return weights return weights
def predict_logreg(inX, weights): def predict_logreg(inX, weights):