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

标签 python machine-learning regression gradient-descent

我正在尝试为几何分布回归模型实现 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 问题?

最佳答案

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

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))

关于python - 如何处理几何回归模型目标函数 exp 中的运行时溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58370732/

相关文章:

python - 在特定日期之间重复 Pandas 中的值

machine-learning - 如何比较数据挖掘分类器?

Python-文本挖掘-TypeError : __hash__ method should return an integer

python - 针对回归问题对连续数据的数据集进行重采样(引导)

r - 如何在 R 中存在恒定和线性时间趋势的情况下运行回归?

c++ - 使用 L1 范数的多项式拟合

python - 修复 Pelican RSS Feed 中的验证错误

python - 如何使用 shell.SendKeys 选择和删除所有(ctrl + shift + 左箭头 + del)?

javascript - 具有一流功能的语言是否一定允许闭包?

python - ModuleNotFoundError : No module named 'pegasus'