1

I'm trying code a logistic regression but I'm in trouble getting a convergent COST, can anyone help me? Below are my codes. Thank you!

#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
D. Wei
  • 79
  • 1
  • 8

0 Answers0