如何完成梯度下降算法代码?

时间:2019-04-28 09:38:51

标签: python machine-learning

我是新生和初学者。

我正在通过开放教程研究机器学习。

我在制作梯度下降算法时遇到麻烦

我必须完成“ for _ in range(max_iter):”,但是我不知道numpy ...所以我不知道应该添加什么代码 你能帮我填补空白吗?

我知道这类问题很无礼...对不起,但我需要您的帮助:( 预先谢谢你。

from sklearn import datasets
import numpy as np
from sklearn.metrics import accuracy_score

X, y = datasets.make_classification(
n_samples = 200, n_features = 2, random_state = 333,
n_informative =2, n_redundant = 0  , n_clusters_per_class= 1)

def sigmoid(s):
     return 1 / (1 + np.exp(-s))

def loss(y, h):
     return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()

def gradient(X, y, w):
     return -(y * X) / (1 + np.exp(-y * np.dot(X, w)))


X_bias = np.append(np.ones((X.shape[0], 1)), X, axis=1)
y = np.array([[1] if label == 0 else [0] for label in y])
w = np.array([[random.uniform(-1, 1)] for _ in range(X.shape[1]+1)])
max_iter = 100
learning_rate = 0.1
threshold = 0.5
for _ in range(max_iter):
#fill in the blank
what code should i add ????


probabilities = sigmoid(np.dot(X_bias, w))
predictions = [[1] if p > threshold else [0] for p in probabilities]
print("loss: %.2f, accuracy: %.2f" %
(loss(y, probabilities), accuracy_score(y, predictions)))

1 个答案:

答案 0 :(得分:0)

for循环中,我们必须首先计算概率。然后找到渐变,然后更新权重。

对于计算概率,您可以使用下面的代码

probs=sigmoid(np.dot(X_bias,w))

np.dot是用于矩阵乘法的numpy命令。然后,我们将计算损耗及其梯度。

J=loss(y,probs)
dJ=gradient(X_bias,y,w)

现在,我们将更新权重。

w=w-learning_rate*dJ

所以最终的代码将是

from sklearn import datasets
import numpy as np
from sklearn.metrics import accuracy_score

X, y = datasets.make_classification(
n_samples = 200, n_features = 2, random_state = 333,
n_informative =2, n_redundant = 0  , n_clusters_per_class= 1)

def sigmoid(s):
    return 1 / (1 + np.exp(-s))

def loss(y, h):
    return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()

def gradient(X, y, w):
    return -(y * X) / (1 + np.exp(-y * np.dot(X, w)))


X_bias = np.append(np.ones((X.shape[0], 1)), X, axis=1)
y = np.array([[1] if label == 0 else [0] for label in y])
w = np.array([[np.random.uniform(-1, 1)] for _ in range(X.shape[1]+1)])
max_iter = 100
learning_rate = 0.1
threshold = 0.5
for _ in range(max_iter):
    probs=sigmoid(np.dot(X_bias,w))
    J=loss(y,probs)
    dJ=gradient(X_bias,y,w)
    w=w-learning_rate*dJ
probabilities = sigmoid(np.dot(X_bias, w))
predictions = [[1] if p > threshold else [0] for p in probabilities]
print("loss: %.2f, accuracy: %.2f" %
(loss(y, probabilities), accuracy_score(y, predictions)))

注意:在for循环中,不需要计算概率和损耗,因为我们只需要渐变来更新权重即可。我这样做是因为它很容易理解。