如何处理几何回归模型目标函数的exp中的运行时溢出?

问题描述 投票:1回答:1

我正在尝试为几何分布回归模型实现Scikit-learn兼容的类。但是,我的目标函数具有exp组件,但出现运行时错误。我的objective_grad函数也出现了类似的错误,该错误会在每个步骤中找到渐变。

我的目标函数代码使用参数计算目标值如下:

        weight = wb[0:-1]
        bias = wb[-1]
        theta = X.dot(weight) + bias
        obj = y.dot(theta)
        obj += np.sum((y + 1).dot((np.log(1 + np.exp(-theta)))))
        obj += self.lam * np.sum(np.square(weight))
        return obj

我正在尝试使用对数和数技巧来解决此问题,但是我不确定如何在此组件中应用:np.log(1+np.exp(-theta))

我也共享了我的渐变函数的代码,该函数也存在类似的溢出问题:

        weight = wb[:-1]
        bias = wb[-1]

        theta = X.dot(weight) + bias
        common_grad = y / (1 + np.exp(-theta)) - 1 / (1 + 
                                                  np.exp(theta))
        dw = X.T.dot(common_grad) - 2 * self.lam * weight
        db = -np.sum(common_grad) - 2 * self.lam * bias
        dwb = np.hstack((dw, db))
        return dwb

在这种情况下,我应该怎样处理exp问题?

python machine-learning regression gradient-descent
1个回答
0
投票

我发现了问题。在这一行中,

obj += np.sum((y + 1).dot((np.log(1 + np.exp(-theta)))))

np.log(1 + np.exp(-theta)正在爆炸。

我使用]解决了>

-np.log(1/(1 + np.exp(theta))

© www.soinside.com 2019 - 2024. All rights reserved.