Implement mixup batch augmentation in NumPy with a reproducible random draw order
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `mixup_batch(X, y, num_classes, alpha, seed)`. Given features `X` of shape `(n, d)` and integer labels `y`, mix each sample with a permuted partner and return `(X_mixed, Y_mixed, lam, perm)`.
Requirements:
- Create `rng = np.random.default_rng(seed)`. Draw `lam = rng.beta(alpha, alpha)` first, then `perm = rng.permutation(n)`.
- If `alpha <= 0`, set `lam = 1.0` with no beta draw (still draw `perm`).
- `X_mixed = lam*X + (1-lam)*X[perm]`, and the same mix for the one-hot targets `Y_mixed` of shape `(n, num_classes)`.
- Return float64 arrays, `lam` as a Python `float` and `perm` as an int array. Do not modify the inputs.
- If `X` and `y` have different row counts, raise `ValueError`.
Example:
```python
X = [[0.0, 2.0], [4.0, 6.0]]
y = [0, 1]
num_classes = 2
# If the draw gives perm == [1, 0], then:
# X_mixed[0] == [4 - 4*lam, 6 - 4*lam]
# Y_mixed[0] == [lam, 1 - lam]
```
```hint Generator state
The tests replay the same seeded generator. Check that every branch consumes exactly the draws the requirements list, in that order.
```
```hint One-hot targets
Mix the targets with the same coefficient and the same permutation as the features. Watch how much memory your one-hot encoding allocates when `num_classes` is large.
```
### Constraints & Assumptions
- `X` has shape `(n, d)` with `n >= 0`, and `y` holds `n` integer labels in `[0, num_classes)`.
- One `lam` is shared by the whole batch.
- `seed` is any value `np.random.default_rng` accepts, such as an int.
### What a Strong Answer Covers
- The exact draw order from the seeded generator, including the `alpha <= 0` branch that skips the beta draw but still draws the permutation.
- One-hot encoding of the labels, with the same coefficient and permutation applied to features and targets.
- Output dtypes and types, and a guarantee that the caller's arrays are never written to.
- The row-count check and behavior on edge cases such as an empty batch or out-of-range labels.
- Why each mixed target row is still a valid probability distribution.
### Follow-up Questions
- How does CutMix differ from mixup?
- How would you compute the loss with mixed targets without materializing one-hot vectors?
- How does the choice of `alpha` change the distribution of `lam`, and what does that do to training?
Overview: Implement mixup augmentation for a batch of features and integer labels in NumPy, blending each sample and its one-hot target with a randomly permuted partner. It tests a reproducible random draw order, memory-aware one-hot encoding, input validation and how mixed targets work with cross-entropy loss.