GCE Detector API¶
The GCEDetector class identifies minority/bias-conflicting samples by training a linear classifier with Generalized Cross Entropy loss and flagging high-loss samples.
Class Reference¶
GCEDetector
¶
GCEDetector(
q: float = 0.7,
loss_percentile_threshold: float = 90.0,
max_iter: int = 500,
random_state: int | None = 42,
)
Detect minority/bias-conflicting samples via Generalized Cross Entropy.
Trains a linear classifier on embeddings with GCE loss (q ≈ 0.7). Samples with high per-sample GCE loss are flagged as minority or bias-conflicting, as they are harder for the biased classifier to fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
float
|
GCE parameter in (0, 1]. q≈0.7 downweights easy samples and emphasizes hard/minority ones. Smaller q is more robust but harder to optimize. |
0.7
|
loss_percentile_threshold
|
float
|
Samples with loss >= this percentile (0–100) are labeled as minority/bias-conflicting. Default 90. |
90.0
|
max_iter
|
int
|
Maximum iterations for training the linear classifier. |
500
|
random_state
|
int | None
|
Random seed for reproducibility. |
42
|
Source code in shortcut_detect/gce/gce_detector.py
Functions¶
fit
¶
Fit a GCE classifier and flag high-loss (minority/bias-conflicting) samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
ndarray
|
(n_samples, n_features) embedding matrix |
required |
labels
|
ndarray
|
(n_samples,) integer or binary labels |
required |
Returns:
| Type | Description |
|---|---|
GCEDetector
|
self |
Source code in shortcut_detect/gce/gce_detector.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
predict
¶
Predict class labels from embeddings.
Source code in shortcut_detect/gce/gce_detector.py
get_minority_indices
¶
Return indices of samples flagged as minority/bias-conflicting.
Quick Reference¶
Constructor¶
GCEDetector(
q: float = 0.7,
loss_percentile_threshold: float = 90.0,
max_iter: int = 500,
random_state: int | None = 42,
)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
q |
float | 0.7 | GCE parameter in (0, 1] |
loss_percentile_threshold |
float | 90.0 | Percentile threshold for flagging minority samples |
max_iter |
int | 500 | Maximum L-BFGS-B iterations |
random_state |
int or None | 42 | Random seed |
Methods¶
fit()¶
Train a GCE classifier and flag high-loss (minority/bias-conflicting) samples.
Parameters:
| Parameter | Type | Description |
|---|---|---|
embeddings |
ndarray | Shape (n_samples, n_features), 2D array |
labels |
ndarray | Shape (n_samples,), integer or binary labels |
Returns: self
Raises:
ValueErrorif embeddings is not 2D or labels is not 1DValueErrorif embeddings and labels have different lengthsValueErrorif fewer than 2 distinct labels
predict()¶
Predict class labels from embeddings using the fitted linear classifier.
get_minority_indices()¶
Return indices of samples flagged as minority/bias-conflicting.
Attributes (after fit)¶
| Attribute | Type | Description |
|---|---|---|
coef_ |
ndarray | Fitted weight matrix (n_features, n_classes) |
intercept_ |
ndarray | Fitted bias vector (n_classes,) |
classes_ |
ndarray | Unique class labels |
per_sample_losses_ |
ndarray | Per-sample GCE losses |
is_minority_ |
ndarray | Boolean mask of flagged samples |
loss_threshold_ |
float | Computed loss threshold |
report_ |
GCEDetectorReport | Detailed report dataclass |
Usage Examples¶
Basic Usage¶
from shortcut_detect.gce import GCEDetector
detector = GCEDetector()
detector.fit(embeddings, labels)
print(detector.report_.risk_level)
print(detector.report_.n_minority)
Custom Parameters¶
detector = GCEDetector(
q=0.5,
loss_percentile_threshold=85.0,
max_iter=1000,
)
detector.fit(embeddings, labels)
minority_idx = detector.get_minority_indices()
Via Unified ShortcutDetector¶
from shortcut_detect import ShortcutDetector
detector = ShortcutDetector(methods=["gce"])
detector.fit(embeddings, labels)
print(detector.summary())