【发布时间】:2014-09-04 15:57:25
【问题描述】:
从Value error: truth value ambiguous 开始,我正在从这里编辑logsumexp 函数:https://github.com/scipy/scipy/blob/v0.14.0/scipy/misc/common.py#L18
原因是: 1.我想自己选择最大值,不总是数组的最大值 2.我想设置一个条件,保证每个元素减去最大值后的差值不低于某个阈值。
这是我的最终代码。它没有任何问题 - 除了它有时仍会返回无穷大!
def mylogsumexp(self, a, is_class, maxaj=None, axis=None, b=None):
threshold = -sys.float_info.max
a = asarray(a)
if axis is None:
a = a.ravel()
else:
a = rollaxis(a, axis)
if is_class == 1:
a_max = a.max(axis=0)
else:
a_max = maxaj
if b is not None:
b = asarray(b)
if axis is None:
b = b.ravel()
else:
b = rollaxis(b, axis)
#out = log(sum(b * exp(threshold if a - a_max < threshold else a - a_max), axis=0))
out = np.log(np.sum(b * np.exp( np.minimum(a - a_max, threshold)), axis=0))
else:
out = np.log(np.sum(np.exp( np.minimum(a - a_max, threshold)), axis=0))
out += a_max
【问题讨论】: