mixup¶
- mixup(arr: Any, obs_axis: int, alpha: float = 1.0, rng=None) None[source][source]¶
- Replace rows along the observation axis that are “missing” (i.e. contain
- any NaNs) with a random convex combination of two non‐missing rows (the
“mixup”).
This function works for arrays of arbitrary dimension so long as the observation axis (obs_axis) contains the “rows” to mix up and the last axis holds features. In higher dimensions the axes other than obs_axis and the last axis are treated as independent batch indices. (Every such batch is
assumed to have at least one non-NaN row.)
- The mixup coefficient for each missing row is drawn from a beta
- distribution with parameters (alpha, alpha) and then “flipped” if it is
less than 0.5 (so that the coefficient is always >=0.5).
- Parameters:
arr (
np.ndarray) – Array of data. In the 2D case it should have shape (n_obs, n_features). For higher dimensions, the last axis is taken as features and obs_axis (which must not be the last axis) is the observation axis.obs_axis (
int) – The axis along which to look for rows that contain any NaN.alpha (
float, default1.) – The alpha parameter for the beta distribution.rng (
np.random.RandomStateorsimilar, optional) –- A random number generator (if None, one is created using
np.random.RandomState()).
- Return type:
None; arr is modified in-place.
Examples
>>> arr = np.array([[1, 2], ... [4, 5], ... [7, 8], ... [float("nan"), float("nan")]]) >>> mixup(arr, 0, rng=42) >>> arr array([[1. , 2. ], [4. , 5. ], [7. , 8. ], [5.24946679, 6.24946679]])
For a 3D example (here we mix along axis 1): >>> arr3 = np.arange(24, dtype=float).reshape(2, 3, 4) >>> arr3[0, 2, :] = [float(“nan”)] * 4 >>> mixup(arr3, 1, rng=42) >>> arr3 # doctest: +SKIP array([[[ 0. , 1. , 2. , 3. ],
[ 4. , 5. , 6. , 7. ], [ 2.33404428, 3.33404428, 4.33404428, 5.33404428]],
- <BLANKLINE>
- [[12. , 13. , 14. , 15. ],
[16. , 17. , 18. , 19. ], [20. , 21. , 22. , 23. ]]])
>>> np.random.seed(0) >>> group2 = np.random.rand(500, 10, 10, 100).astype("float16") >>> group2[::2, 0, 0, :] = np.nan >>> mixup(group2, 0) >>> group2[:10, 0, 0, :5] array([[0.3274 , 0.2805 , 0.1257 , 0.1256 , 0.3027 ], [0.748 , 0.1802 , 0.389 , 0.0376 , 0.01179], [0.6484 , 0.829 , 0.8213 , 0.2578 , 0.5327 ], [0.7583 , 0.5034 , 0.177 , 0.8325 , 0.5166 ], [0.7397 , 0.857 , 0.449 , 0.5913 , 0.714 ], [0.3076 , 0.062 , 0.989 , 0.719 , 0.758 ], [0.571 , 0.176 , 0.679 , 0.6924 , 0.636 ], [0.6323 , 0.07513, 0.722 , 0.4668 , 0.7417 ], [0.6987 , 0.3787 , 0.4668 , 0.04987, 0.915 ], [0.1912 , 0.05853, 0.4368 , 0.72 , 0.824 ]], dtype=float16) >>> import cupy as cp >>> group3 = cp.random.randn(100, 10, 10, 100) >>> group3[0::2, 0, 0, :] = float("nan") >>> mixup(group3, 0) >>> group3[0, 0, :, :5] array([[0.3274 , 0.2805 , 0.1257 , 0.1256 , 0.3027 ], [0.748 , 0.1802 , 0.389 , 0.0376 , 0.01179], [0.6484 , 0.829 , 0.8213 , 0.2578 , 0.5327 ], [0.7583 , 0.5034 , 0.177 , 0.8325 , 0.5166 ], [0.7397 , 0.857 , 0.449 , 0.5913 , 0.714 ], [0.3076 , 0.062 , 0.989 , 0.719 , 0.758 ], [0.571 , 0.176 , 0.679 , 0.6924 , 0.636 ], [0.6323 , 0.07513, 0.722 , 0.4668 , 0.7417 ], [0.6987 , 0.3787 , 0.4668 , 0.04987, 0.915 ], [0.1912 , 0.05853, 0.4368 , 0.72 , 0.824 ]], dtype=float16)