【问题标题】:Numpy where returning error where it shouldn'tNumpy 在不应该返回错误的地方返回错误
【发布时间】:2020-04-24 15:09:05
【问题描述】:

我正在从头开始使用神经网络,当我尝试实现稳定的 sigmoid 函数时,numpy where 似乎表现得很奇怪。这里的两个函数都返回 RuntimeWarning: overflow 在 exp 中遇到

#Original Function

def sigmoid(x):
    return np.where(x >= 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x)))

#Dummy function that is also misbehaving

def sigmoid(x):
    return np.where(x>=0, 1 / (1 + np.exp(-x)), 0)

这是结果:

【问题讨论】:

  • where 不会阻止对整个数组的评估。这是蟒蛇
  • 我该如何解决这个问题?
  • 可以抑制警告。或者ufuncnp.exp 有自己的where 机制。您还可以在传递给函数之前将clip x 值。

标签: python numpy debugging neural-network numpy-ndarray


【解决方案1】:

这是运行时警告而不是错误。您的代码运行良好。警告是因为您试图计算溢出浮点容量的exp(-(-1000))(本质上返回inf)。既然你有它在分母中,我不会担心它,因为它返回1/inf = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多