简体   繁体   English

用于 L2 对数回归的随机梯度良好

[英]Stochastic Gradient Decent for L2 Log Regression

I was trying to code SGD for L2 Log Regression in Python.我试图在 Python 中为 L2 日志回归编写 SGD。 But my avg loss is remaining almost the same for every epoch.但是我的平均损失在每个时期几乎保持不变。 Can some one help me out with the code.有人可以帮我解决代码。 Code: Function to predict the Y代码:预测 Y 的函数

def predict(row, coefficients):
    yhat = coefficients[0]
    for i in range(len(row)-1):
        yhat += coefficients[i + 1] * row[i]
    return 1.0 / (1.0 + exp(-yhat))

Function to calculate the loss计算损失的函数

def loss_func(w,x_i,lam):
    y=x_i['y']
    yhat=predict(x_i[:-1],w)
    loss=(y*np.log(yhat))-((1-y)*np.log(1-yhat))+(lam*np.linalg.norm(w)/2)
    return loss

Function to update weights更新权重的函数

def weights(w,x,lrate,n_epoch,lam):
    total_loss_lst=[0,1]
    for epoch in range(n_epoch):
        sum_error=0
        total_loss=0
        for k in range(len(x)):
            x_i=x.iloc[k]
            total_loss+=loss_func(w,x_i,lam)

        each_row=x.iloc[np.random.randint(len(x))]
        y_pred=predict(each_row[:-1],w)
        error=y_pred-each_row['y']
        w[0]=w[0]+(lrate*error)
        for i in range(0,len(each_row)-1):
            #print(each_row[i])
            w[i+1]=w[i+1]-(lrate*error*y_pred*(1-y_pred)*each_row[i])
            #w[i+1]=w[i+1]+(lrate*error*each_row[i])
        total_loss_lst.append(total_loss/len(x))
        print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, lrate, total_loss/len(x)))
    return w

Initiating the function w vector is all zero at first instance.初始化函数 w 向量在第一个实例中全部为零。

weights(w,x,.0001,10,.0001) 

I am getting the below output.我得到以下输出。

>epoch=0, lrate=0.000, error=0.274
>epoch=1, lrate=0.000, error=0.274
>epoch=2, lrate=0.000, error=0.274
>epoch=3, lrate=0.000, error=0.274
>epoch=4, lrate=0.000, error=0.274
>epoch=5, lrate=0.000, error=0.275
>epoch=6, lrate=0.000, error=0.275
>epoch=7, lrate=0.000, error=0.275
>epoch=8, lrate=0.000, error=0.275
>epoch=9, lrate=0.000, error=0.275

The weights are getting updated but the loss is very similar.权重正在更新,但损失非常相似。 But the sklearn's loss output is ranging from .45 to .37.但是 sklearn 的损失输出范围从 0.45 到 0.37。

Thank you谢谢

I only took a brief look at your code because it is difficult to decipher, but the algorithm may be getting stuck at a local minimum.我只是简单地看了一下你的代码,因为它很难破译,但算法可能会卡在局部最小值上。 Try instantiating to random values instead of a zero vector尝试实例化为随机值而不是零向量

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

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