简体   繁体   English

机器学习逻辑回归 L2 正则化

[英]Machine learning Logistic Regression L2 regularization

I am trying to implement L2 regularization to my data I wrote my function like this我正在尝试对我的数据实施 L2 正则化我像这样编写了我的函数

 def logicalregP3(xtr,ytr,learning_rate,iteration,lamda):
   
    m=xtrain.T.shape[1]
    n=xtrain.T.shape[0]
   
    W= np.zeros((n,1))
  
    B = 0
    cost_list = []
    for i in range (iteration):
        z= np.array(np.dot(W.T, xtr.T),dtype=np.float32)+B
     
        a= 1/(1 + np.exp(-z))

    
        cost=-(1/m)*np.sum(ytr.T*np.log(a)+(1-ytr.T)*np.log(1-a))+(lamda*np.sum(W))
        # Gradient Descent
        
        regular=(lamda/(2*m))*W
        dW=(1/m)*np.dot(a-ytr.T,xtr)+regular
        dB=(1/m)*np.sum(a-ytr.T)      
        W=W-learning_rate*dW.T
        B=B-learning_rate*dB
        print("cost ", i ," ", cost)
   
        cost_list.append(cost)
       
            
    return W,B,cost_list

Then I call the function with these values然后我用这些值调用函数

iterations=2000
learning_rate=0.0015
lamda=0.00001
Wp3,Bp3,cost_listp3=logicalregP3(xtrain,ytrain,learning_rate, iterations,lamda)

the output of my code:我的代码的输出:

cost  0   0.6931471824645996
cost  1   2.6271848720002735
cost  2   2.5287339955351484
cost  3   2.461344902235327
cost  4   2.4144645825946607
cost  5   2.3812474056254485
cost  6   2.357244340813794
cost  7   2.339536539714837
cost  8   2.3261846933514745
cost  9   2.3158827015378978
cost  10   2.307739196600699
cost  11   2.3011379381265
cost  12   2.2956485144978704
cost  13   2.290966932838124
cost  14   2.28687647957537

when I draw the graph: this is the output for the cost:当我绘制图形时:这是成本的输出:在此处输入图片说明

Is the output of my code normal or I have problem at the gradient descent我的代码输出正常还是梯度下降有问题

Actually L2 regularisation is not lambda * np.sum(W) but it is lambda * np.linalg.norm(W, ord=2) .实际上 L2 正则化不是lambda * np.sum(W)而是lambda * np.linalg.norm(W, ord=2) In your case I assume that the gradient descent works well but you can't check it because your costs don't represent the quantity which is minimized here.在您的情况下,我假设梯度下降效果很好,但您无法检查它,因为您的成本不代表此处最小化的数量。

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

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