def sigmoid(z):
# complete the code
z = np.asarray(z)
if z.ndim == 0:
return(1/(1+np.exp(-z)))
else:
d = np.array([])
for item in z:
d= np.append(d,1/(1+np.exp(-item)))
return d
打印(sigmoid([(1,3), (4,30)]))
为什么返回[ 0.73105858 0.95257413 0.98201379 1. ]
因为函数从 0 绑定(bind)到 1
例如 [q= 1/1+np.exp(-30)][1]
返回 1.0000000000000935
为什么会发生这种情况以及如何纠正? 例如image of a weird looking output
最佳答案
您的 sigmoid 实现看起来不错。
print(1/1 + np.exp(-30))
返回 1.0000000000000935
的原因是 operator precedence .
# Your example
1 / 1 + np.exp(-30)
# How it will be computed
(1 / 1) + np.exp(-30)
# What you actually wanted
1 / (1 + np.exp(-30))
附注numpy 支持 broadcasting 。您的功能可以简化为:
def sigmoid(z):
z = np.asarray(z)
return 1 / (1 + np.exp(-z))
使用ravel如果您想要的话,可以使用函数来展平多维数组。
关于python - Sigmoid 越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52455829/