generated from LufsX/Experiment-LogReg
This commit is contained in:
+9
-2
@@ -37,8 +37,15 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.0):
|
|||||||
y_predicted = 1 / (1 + np.exp(-linear_output))
|
y_predicted = 1 / (1 + np.exp(-linear_output))
|
||||||
error = y - y_predicted
|
error = y - y_predicted
|
||||||
|
|
||||||
# 更新权重和偏置,添加正则化项
|
|
||||||
weights += learning_rate * (error * X.T - regularization * weights) / m
|
# 计算梯度(不包括正则化项)
|
||||||
|
gradient = np.dot(X.T, error) / m
|
||||||
|
|
||||||
|
# 加上正则化项的梯度(注意这里是对weights的每个元素都加上-regularization*weight[i])
|
||||||
|
gradient += -regularization * weights
|
||||||
|
|
||||||
|
# 更新权重
|
||||||
|
weights += learning_rate * gradient
|
||||||
bias += learning_rate * np.sum(error) / m
|
bias += learning_rate * np.sum(error) / m
|
||||||
|
|
||||||
return weights, bias
|
return weights, bias
|
||||||
|
|||||||
Reference in New Issue
Block a user