Gradient-Based One-Side Sampling (GOSS)

Quick Overview

Implement gradient-based one-side sampling for a boosting round: keep the rows with the largest absolute gradients, draw a seeded sample of the rest with an amplification weight, and compute the weighted gradient and hessian sums. It tests deterministic top-k selection, reproducible sampling and the reasoning behind the reweighting.

Gradient-Based One-Side Sampling (GOSS)

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Gradient-based one-side sampling (GOSS) picks the training rows a boosting round uses to build its tree. Implement `goss_sample(gradients, top_rate, other_rate, seed)`, returning `(indices, weights)`. Keep the `floor(top_rate * n)` rows with largest `|gradient|` (weight 1.0). From the remaining rows, sorted ascending by index, draw `floor(other_rate * n)` rows (capped at the remaining count) via `np.random.default_rng(seed).choice(rest, size=k, replace=False)`, each weighted `(1 - top_rate) / other_rate`. Also implement `goss_weighted_sums(gradients, hessians, indices, weights)`, returning `(sum(w * g[idx]), sum(w * h[idx]))` as Python floats. The sampler must follow these rules: - Ties in `|gradient|` go to the lower row index. - Return `indices` and `weights` as arrays ordered by ascending row index, each weight aligned with its row. - Raise `ValueError` if either rate is negative or `top_rate + other_rate > 1`. Example: ```python g = [0.1, -5.0, 0.3, 4.0, -0.2, 2.0, -0.7, 0.05, 1.5, -0.4] goss_sample(g, 0.2, 0.0, seed=0) # -> ([1, 3], [1.0, 1.0]) goss_weighted_sums([1.0, -2.0, 3.0, 0.5], [0.25, 0.5, 1.0, 2.0], [0, 2, 3], [1.0, 2.0, 4.0]) # -> (9.0, 10.25) ``` ```hint Reproducing the draw The same seed draws different rows if `rest` is in a different order. Check what order your remaining rows are in at the moment you call `choice`. ``` ```hint Empty sample Consider `other_rate = 0`, or a rate so small that the sample count floors to zero, before you compute the amplification factor. ``` ### Constraints & Assumptions - `gradients` and `hessians` are 1-D numeric arrays of the same length `n`. - `floor(rate * n)` means the floor of the exact decimal product, so `0.29 * 100` gives 29 even though the floating-point product is slightly below 29. - `seed` is passed unchanged to `np.random.default_rng`. ### What a Strong Answer Covers - Top selection by absolute gradient with the lower-index tie rule and floor-based counts that survive floating-point rounding. - A draw from the sorted remainder that reproduces the specified RNG call exactly, with the count capped and the amplification weight applied. - Safe handling of a zero-size sample and of invalid rates. - Output arrays in ascending index order with aligned weights, and weighted sums returned as Python floats. - Why the factor `(1 - top_rate) / other_rate` keeps the gradient sum close to unbiased, and when it is exact. ### Follow-up Questions - Why does GOSS keep every large-gradient row and sample only the small ones, when uniform sampling would be simpler? - With floored counts, `(1 - top_rate) / other_rate` is not always the inverse of a row's inclusion probability. What factor would be exact, and does the difference matter? - How do the weighted gradient and hessian sums feed into the split gain and leaf values of a second-order boosting tree? - Can you select the top rows in O(n) and still keep the tie rule?

Overview: Implement gradient-based one-side sampling for a boosting round: keep the rows with the largest absolute gradients, draw a seeded sample of the rest with an amplification weight, and compute the weighted gradient and hessian sums. It tests deterministic top-k selection, reproducible sampling and the reasoning behind the reweighting.

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

Gradient-based one-side sampling (GOSS) picks the training rows a boosting round uses to build its tree. Implement goss_sample(gradients, top_rate, other_rate, seed), returning (indices, weights). Keep the floor(top_rate * n) rows with largest |gradient| (weight 1.0). From the remaining rows, sorted ascending by index, draw floor(other_rate * n) rows (capped at the remaining count) via np.random.default_rng(seed).choice(rest, size=k, replace=False), each weighted (1 - top_rate) / other_rate. Also implement goss_weighted_sums(gradients, hessians, indices, weights), returning (sum(w * g[idx]), sum(w * h[idx])) as Python floats.

The sampler must follow these rules:

  • Ties in |gradient| go to the lower row index.
  • Return indices and weights as arrays ordered by ascending row index, each weight aligned with its row.
  • Raise ValueError if either rate is negative or top_rate + other_rate > 1 .

Example:

g = [0.1, -5.0, 0.3, 4.0, -0.2, 2.0, -0.7, 0.05, 1.5, -0.4]
goss_sample(g, 0.2, 0.0, seed=0)  # -> ([1, 3], [1.0, 1.0])

goss_weighted_sums([1.0, -2.0, 3.0, 0.5], [0.25, 0.5, 1.0, 2.0],
                   [0, 2, 3], [1.0, 2.0, 4.0])  # -> (9.0, 10.25)

Constraints & Assumptions

  • gradients and hessians are 1-D numeric arrays of the same length n .
  • floor(rate * n) means the floor of the exact decimal product, so 0.29 * 100 gives 29 even though the floating-point product is slightly below 29.
  • seed is passed unchanged to np.random.default_rng .

What a Strong Answer Covers Guidance

  • Top selection by absolute gradient with the lower-index tie rule and floor-based counts that survive floating-point rounding.
  • A draw from the sorted remainder that reproduces the specified RNG call exactly, with the count capped and the amplification weight applied.
  • Safe handling of a zero-size sample and of invalid rates.
  • Output arrays in ascending index order with aligned weights, and weighted sums returned as Python floats.
  • Why the factor (1 - top_rate) / other_rate keeps the gradient sum close to unbiased, and when it is exact.

Follow-up Questions Guidance

  • Why does GOSS keep every large-gradient row and sample only the small ones, when uniform sampling would be simpler?
  • With floored counts, (1 - top_rate) / other_rate is not always the inverse of a row's inclusion probability. What factor would be exact, and does the difference matter?
  • How do the weighted gradient and hessian sums feed into the split gain and leaf values of a second-order boosting tree?
  • Can you select the top rows in O(n) and still keep the tie rule?