Temperature-Controlled Class-Aware Sampling

Quick Overview

Implement temperature-controlled class-aware sampling, where a class is drawn with probability proportional to its count raised to a power q. It covers class probabilities, per-row sampling weights and a reproducible epoch of indices drawn by inverse CDF, testing NumPy skills and how sampling choices handle class imbalance.

Temperature-Controlled Class-Aware Sampling

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Given labels `y` and temperature `q`, class `c` is sampled with probability $p_c \propto n_c^{q}$, where $n_c$ is the number of rows with label `c`. Setting `q = 1` reproduces ordinary instance sampling, and `q = 0` gives every class the same probability. Implement three functions: - `class_sampling_probabilities(y, q)` returns `(classes, probs)` with classes sorted ascending. - `instance_sampling_weights(y, q)` returns each row's draw probability `p_c / n_c`. - `class_aware_epoch_indices(y, q, n_samples=None, seed=0)` returns an `int64` index array drawn with this exact procedure: ```python n = len(y) if n_samples is None else n_samples rng = np.random.default_rng(seed) u = rng.random(n) # drawn first v = rng.random(n) # drawn second # class i = min(searchsorted(cumsum(probs), u[i], side="right"), k-1) # members = row indices of that class, ascending # index i = members[min(floor(v[i] * n_c), n_c - 1)] ``` Example: ```python y = [0]*16 + [1]*4 + [2] # any order class_sampling_probabilities(y, 0.5) # ([0, 1, 2], [4/7, 2/7, 1/7]) class_sampling_probabilities(y, 0.0) # ([0, 1, 2], [1/3, 1/3, 1/3]) instance_sampling_weights(y, 1.0) # every row 1/21 ``` With `q = 0.5` the class weights are the square roots of the counts 16, 4 and 1, which are 4, 2 and 1, and they sum to 7. With `q = 1` each class gets `n_c / 21`, so every row gets `1/21`. ```hint Two-stage draw Each draw picks a class first and then a row inside it. Work out what probability that gives one particular row. ``` ```hint Floating-point edge `cumsum(probs)` can end slightly below 1. Check what `searchsorted` returns for a `u` that lands above the last value. ``` ### Constraints & Assumptions - Labels may be non-contiguous integers. - Raise `ValueError` if `y` is empty or not 1-D, if `q` is negative or non-finite (all three functions), or if `n_samples` is negative. - Instance weights sum to 1 over all rows. ### What a Strong Answer Covers - Class probabilities built from sorted unique classes and their counts, checked against the `q = 0` and `q = 1` cases. - Per-row weights `p_c / n_c` mapped back to the original row order, and why they sum to 1. - The epoch draw in the exact RNG order, including both clips that guard against floating-point overshoot. - Input validation in all three functions, plus the `n_samples = 0` case. - The time and memory cost of building the per-class member lists. ### Follow-up Questions - A model trained with `q < 1` sees a different class prior than the one it will meet in production. How would you correct its predicted probabilities? - How would you choose `q`, and would you keep it fixed across training? - How does sampling with these weights compare with keeping uniform sampling and reweighting the loss by the same amounts? - How would you build this sampler for a dataset too large to keep every row index in memory on one machine?

Overview: Implement temperature-controlled class-aware sampling, where a class is drawn with probability proportional to its count raised to a power q. It covers class probabilities, per-row sampling weights and a reproducible epoch of indices drawn by inverse CDF, testing NumPy skills and how sampling choices handle class imbalance.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Given labels y and temperature q, class c is sampled with probability pc∝ncqp_c \propto n_c^{q}, where ncn_c is the number of rows with label c. Setting q = 1 reproduces ordinary instance sampling, and q = 0 gives every class the same probability.

Implement three functions:

  • class_sampling_probabilities(y, q) returns (classes, probs) with classes sorted ascending.
  • instance_sampling_weights(y, q) returns each row's draw probability p_c / n_c .
  • class_aware_epoch_indices(y, q, n_samples=None, seed=0) returns an int64 index array drawn with this exact procedure:
n = len(y) if n_samples is None else n_samples
rng = np.random.default_rng(seed)
u = rng.random(n)   # drawn first
v = rng.random(n)   # drawn second
# class i  = min(searchsorted(cumsum(probs), u[i], side="right"), k-1)
# members  = row indices of that class, ascending
# index i  = members[min(floor(v[i] * n_c), n_c - 1)]

Example:

y = [0]*16 + [1]*4 + [2]   # any order
class_sampling_probabilities(y, 0.5)  # ([0, 1, 2], [4/7, 2/7, 1/7])
class_sampling_probabilities(y, 0.0)  # ([0, 1, 2], [1/3, 1/3, 1/3])
instance_sampling_weights(y, 1.0)     # every row 1/21

With q = 0.5 the class weights are the square roots of the counts 16, 4 and 1, which are 4, 2 and 1, and they sum to 7. With q = 1 each class gets n_c / 21, so every row gets 1/21.

Constraints & Assumptions

  • Labels may be non-contiguous integers.
  • Raise ValueError if y is empty or not 1-D, if q is negative or non-finite (all three functions), or if n_samples is negative.
  • Instance weights sum to 1 over all rows.

What a Strong Answer Covers Guidance

  • Class probabilities built from sorted unique classes and their counts, checked against the q = 0 and q = 1 cases.
  • Per-row weights p_c / n_c mapped back to the original row order, and why they sum to 1.
  • The epoch draw in the exact RNG order, including both clips that guard against floating-point overshoot.
  • Input validation in all three functions, plus the n_samples = 0 case.
  • The time and memory cost of building the per-class member lists.

Follow-up Questions Guidance

  • A model trained with q < 1 sees a different class prior than the one it will meet in production. How would you correct its predicted probabilities?
  • How would you choose q , and would you keep it fixed across training?
  • How does sampling with these weights compare with keeping uniform sampling and reweighting the loss by the same amounts?
  • How would you build this sampler for a dataset too large to keep every row index in memory on one machine?