Following up from Value error: truth value ambiguous, I'm editing the logsumexp function from here: https://github.com/scipy/scipy/blob/v0.14.0/scipy/misc/common.py#L18
The reason is because: 1. I want to select the maximum value myself, it's not always just the maximum of the array 2. I want to put a condition to ensure that the difference after subtracting the maximum from each element doesn't go below a certain threshold.
This is my final code. There's nothing wrong with it - except that it still returns inifinities sometimes!
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