einops
einops copied to clipboard
How to ignore π½ππ½ in reduce?
Numpy and many other libraries have introduced additional aggregation functions that ignore π½ππ½-values, for instance:
numpy.nan[sum, mean, min, max, argmin, argmax, median, std, var, prod, quantile, percentile]torch.nan[sum, mean, median, quantile]tensorflow.experimental.numpyjax.numpy
- Use-cases This would be mostly a comfort increase. Avoiding aggregation over π½ππ½-values when working with data that has missing values, or when padding (padding with π½ππ½'s instead of 0's has the advantage that any computation that accidentally uses the padding values will result in a π½ππ½ again - thus making it easier to notice such bugs.)
- Implementation. Either, avoid iterating over π½ππ½-values altogether, or chose a masking value appropriate for the chosen reduction, e.g.
- nansum β replace π½ππ½ with 0
- nanprod β replace π½ππ½ with 1
- nanmax β replace π½ππ½ with -πΈππ
- Integrity - does it interplay well with existing operations and notation in einops? It is a simple additional boolean flag
ignore_nanforreduce - Readability. Alternatively, one could have a
nanreducethat does the same thing but is visually more striking.
Similarly, one could consider an additional ignore_infinite-flag.
It is currently supported by providing callables for reductions in einops.reduce. Example:
einops.reduce(array, 'i j k -> (i j)', np.nanmean)