Fix weird error in histogram computation where mins and maxes are sometimes flipped.
@@ -210,6 +210,10 @@ class TorchHistory(object):
# If we've got zeros to add in, make sure zero is in the hist range.
tmin = 0 if tmin > 0 else tmin
tmax = 0 if tmax < 0 else tmax
+ # Anecdotally, this can somehow happen sometimes. Maybe a precision error
+ # in min()/max() above. Swap here to prevent a runtime error.
+ if tmin > tmax:
+ tmin, tmax = tmax, tmin
tensor = flat.histc(bins=self._num_bins, min=tmin, max=tmax)
tensor = tensor.cpu().clone().detach()
bins = torch.linspace(tmin, tmax, steps=self._num_bins + 1)
