简体   繁体   English

Python中的逻辑回归

[英]Logistic Regression in Python

I'm trying to build a Logistic regression model using numPy and training it on TensorFlow "Getting Started" example: {x: [1, 2, 3, 4], y: [0, -1, -2, -3]} using the same learning rate and epochs as the one on tensorFlow example but for some reason it cant learn the correct weight and bias. 我正在尝试使用numPy构建Logistic回归模型并在TensorFlow“入门”示例中对其进行训练: {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}使用与tensorFlow示例相同的学习率和时期,但由于某种原因,它无法学习正确的权重和偏见。 Any help? 有什么帮助吗? I'm new to AI. 我是AI的新手。

Code: 码:

# Compute cost and gradient
def propagate(w, b, X, Y):
    m = X.shape[0]
    A = sigmoid(np.multiply(w,X) + b)
    arr = (np.multiply(w,X) + b) - Y
    cost = np.dot(arr, arr)
    cost = np.squeeze(cost)
    dw = 1/m * X.dot((A-Y).T)
    db = 1/m * np.sum(A-Y)

    return {"db": db, "dw": dw}, cost

# Gradient Descnet
def optimize(w, b, X, Y, epochs, learning_rate):
    costs = []

    for i in range(epochs):
        grads, cost = propagate(w, b, X, Y)
        dw = grads['dw']
        db = grads['db']
        w = w - learning_rate * dw
        b = b - learning_rate * db
        if i % 100 == 0:
            costs.append(cost)

    return {"w":w, "b":b}, {"db": db, "dw": dw}, costs

Output: 输出:

w, b, X, Y = np.array([0.3]), -0.3, np.array([1, 2, 3, 4]), np.array([0, -1, -2, -3])

grads, cost = propagate(w, b, X, Y)

print ("dw = " + str(grads["dw"])) # dw = 6.6074129907
print ("db = " + str(grads["db"])) # db = 2.10776208142
print ("cost = " + str(cost))      # cost = 23.66

params, grads, costs = optimize(w, b, X, Y, epochs= 100, learning_rate = 0.01)

print ("w = " + str(params["w"])) # w = [-4.85038348] (supposed to be about -0.9999969)
print ("b = " + str(params["b"])) # b = -1.86763966366 (supposed to be about 0.99999082)

You can just use a linear function as activation for your last layer. 您可以仅使用线性函数作为最后一层的激活。 And it is better to normalize your data to have better results. 而且最好对数据进行规范化以获得更好的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM