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