【问题标题】:Negative Infinity or positive infinity in loss function in Regression回归中损失函数的负无穷大或正无穷大
【发布时间】:2021-01-12 10:58:52
【问题描述】:

我的输出显示损失函数的无穷大或负无穷大。它应该是 0 或 1。

w = np.random.randn(6)
x = np.random.randn(6)
b =1
z = np.dot(w,x) + b
# a is sigmoid 
a = 1/1+np.exp(-z)
loss_function = -(np.dot(a,np.log(y) + np.dot((1-a),np.log(1-y))))

当我使用 y 的任何值时,如果是 y。它显示了

-inf

inf

谁能解释一下?

【问题讨论】:

  • 不可能对y 使用any 值:np.log(1-y) 意味着1 - y > 0,所以y < 1。您是否传递了小于 1 的 y
  • 请提供出现问题的一些值。对于y=0.5,我得到loss_function0.6420817786893799-2363.635754984989-188.1582335139518、...(取决于wx)。
  • 没有,我首先尝试将 y 的值设为 0。但后来我尝试了其他值,例如 1,2,.... 对于小于零的值,它显示 nan。
  • @ajay,你不能有y >= 1,因为那样1 - y 将小于0,并且未定义非正数的对数。
  • @ajay, log_b(x)=y for any two positive real numbers b and x, where b is not equal to 1, is always a unique real number y,所以是的,只要0 < y < 1(在你的代码中,而不是在这个例子中),你就会得到一些实数。

标签: python-3.x deep-learning logistic-regression


【解决方案1】:

您正在向后应用交叉熵损失函数的公式,这就是为什么您只能得到无穷大。

您的真实标签y01 这永远不会起作用,而是应该将a 放在日志中:

loss_function = -(np.dot(y,np.log(a) + np.dot((1-y),np.log(1-a))))

【讨论】:

  • 是的,我对前向和后向实现感到困惑。
猜你喜欢
  • 2015-03-22
  • 1970-01-01
  • 2012-02-09
  • 2013-11-29
  • 2022-08-12
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多