成本价值不会收敛

时间:2018-04-22 10:13:01

标签: machine-learning logistic-regression

我尝试使用逻辑回归代码,但是我无法获得收敛的COST,任何人都可以帮助我吗?以下是我的代码。谢谢!

#input:
m = 3, n = 4
# we have 3 training examples and each of them has 4 features (Sorry, I know it looks weired here). Y is a label matrix.
X = np.array([[1,2,1],[1,1,0],[1,2,1],[1,0,2]])
Y = np.array([[0,1,0]])


h = 100000  #iterations
alpha = 0.05  #learning rate
b = 0  #scalar bias
W = np.zeros(n).reshape(1,n) #weights
J = np.zeros(h).reshape(1,h) #a vector for holing cost value
Yhat = np.zeros(m).reshape(1,m) #predicted value

def activation(yhat):
    return 1/(1+np.exp(-yhat))

W=W.T
for g in range(h):    
    m = X.T.shape[0]
    Y_hat = activation(X.dot(W)+b)
    cost = -1/m * np.sum(Y*np.log(Y_hat)+(1-Y)*np.log(1-Y_hat))
    current_error = Y.T - Y_hat
    dW = 1/m * np.dot(X.T, current_error)
    db = 1/m * np.sum(current_error)
    W = W + alpha * dW
    b = b + alpha * db  
    J[0][g] = cost

0 个答案:

没有答案