【发布时间】: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_function的0.6420817786893799、-2363.635754984989、-188.1582335139518、...(取决于w、x)。 -
没有,我首先尝试将 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