|
| 1 | +# Consistent Metric Function Signature |
| 2 | + |
| 3 | +using DecisionFocusedLearningAlgorithms |
| 4 | +using DecisionFocusedLearningBenchmarks |
| 5 | +using MLUtils: splitobs |
| 6 | +using Statistics |
| 7 | + |
| 8 | +b = ArgmaxBenchmark() |
| 9 | +dataset = generate_dataset(b, 100) |
| 10 | +train_instances, val_instances, test_instances = splitobs(dataset; at=(0.3, 0.3, 0.4)) |
| 11 | + |
| 12 | +model = generate_statistical_model(b; seed=0) |
| 13 | +maximizer = generate_maximizer(b) |
| 14 | + |
| 15 | +# ============================================================================ |
| 16 | +# NEW: ALL metric functions have the SAME signature! |
| 17 | +# (model, maximizer, data, context) -> value |
| 18 | +# ============================================================================ |
| 19 | + |
| 20 | +# Simple metric - just uses model, maximizer, and data |
| 21 | +compute_gap = (model, max, data, ctx) -> compute_gap(b, data, model, max) |
| 22 | + |
| 23 | +# Metric that also uses context |
| 24 | +compute_gap_ratio = |
| 25 | + (model, max, data, ctx) -> begin |
| 26 | + # data is the dataset from 'on' parameter |
| 27 | + # context gives access to everything else |
| 28 | + train_gap = compute_gap(b, ctx.train_dataset, model, max) |
| 29 | + data_gap = compute_gap(b, data, model, max) |
| 30 | + return train_gap / data_gap |
| 31 | + end |
| 32 | + |
| 33 | +# Metric that ignores data, just uses context |
| 34 | +get_epoch = (model, max, data, ctx) -> ctx.epoch |
| 35 | + |
| 36 | +# Metric that uses everything |
| 37 | +complex_metric = (model, max, data, ctx) -> begin |
| 38 | + # Can access: |
| 39 | + # - model, max (always provided) |
| 40 | + # - data (the dataset from 'on') |
| 41 | + # - ctx.epoch |
| 42 | + # - ctx.train_dataset, ctx.validation_dataset |
| 43 | + # - ctx.training_loss, ctx.validation_loss |
| 44 | + gap = compute_gap(b, data, model, max) |
| 45 | + return gap * ctx.epoch # silly example, but shows flexibility |
| 46 | +end |
| 47 | + |
| 48 | +# ============================================================================ |
| 49 | +# Usage - Same function signature works everywhere! |
| 50 | +# ============================================================================ |
| 51 | + |
| 52 | +callbacks = [ |
| 53 | + # on=:validation (default) - data will be validation_dataset |
| 54 | + Metric(:gap, compute_gap), |
| 55 | + # Creates: val_gap |
| 56 | + |
| 57 | + # on=:both - function called twice with train and val datasets |
| 58 | + Metric(:gap, compute_gap; on=:both), |
| 59 | + # Creates: train_gap, val_gap |
| 60 | + |
| 61 | + # on=test_instances - data will be test_instances |
| 62 | + Metric(:test_gap, compute_gap; on=test_instances), |
| 63 | + # Creates: test_gap |
| 64 | + |
| 65 | + # Complex metric using context |
| 66 | + Metric(:gap_ratio, compute_gap_ratio; on=:validation), |
| 67 | + # Creates: val_gap_ratio |
| 68 | + |
| 69 | + # Ignore data parameter completely |
| 70 | + Metric(:current_epoch, get_epoch), |
| 71 | + # Creates: val_current_epoch (on=:validation by default) |
| 72 | +] |
| 73 | + |
| 74 | +# ============================================================================ |
| 75 | +# Benefits of Consistent Signature |
| 76 | +# ============================================================================ |
| 77 | + |
| 78 | +# ✅ ALWAYS the same signature: (model, max, data, ctx) -> value |
| 79 | +# ✅ No confusion about what arguments metric_fn receives |
| 80 | +# ✅ Easy to write - just follow one pattern |
| 81 | +# ✅ Easy to compose - all functions compatible |
| 82 | +# ✅ Full flexibility - context gives access to everything |
| 83 | +# ✅ Can ignore unused parameters (data or parts of context) |
| 84 | + |
| 85 | +# ============================================================================ |
| 86 | +# Comparison: OLD vs NEW |
| 87 | +# ============================================================================ |
| 88 | + |
| 89 | +# OLD (inconsistent signatures): |
| 90 | +# on=nothing → metric_fn(context) # 1 arg |
| 91 | +# on=:both → metric_fn(model, maximizer, dataset) # 3 args |
| 92 | +# on=data → metric_fn(model, maximizer, data) # 3 args |
| 93 | +# 😕 Confusing! Different signatures for different modes! |
| 94 | + |
| 95 | +# NEW (consistent signature): |
| 96 | +# Always: metric_fn(model, maximizer, data, context) # 4 args |
| 97 | +# ✨ Clear! Same signature everywhere! |
| 98 | + |
| 99 | +# ============================================================================ |
| 100 | +# Practical Example: Define metrics once, use everywhere |
| 101 | +# ============================================================================ |
| 102 | + |
| 103 | +# Define your metrics library with consistent signature |
| 104 | +module MyMetrics |
| 105 | +gap(model, max, data, ctx) = compute_gap(benchmark, data, model, max) |
| 106 | +regret(model, max, data, ctx) = compute_regret(benchmark, data, model, max) |
| 107 | +accuracy(model, max, data, ctx) = compute_accuracy(benchmark, data, model, max) |
| 108 | + |
| 109 | +# Complex metric using context |
| 110 | +function overfitting_indicator(model, max, data, ctx) |
| 111 | + train_metric = gap(model, max, ctx.train_dataset, ctx) |
| 112 | + val_metric = gap(model, max, ctx.validation_dataset, ctx) |
| 113 | + return val_metric - train_metric |
| 114 | +end |
| 115 | +end |
| 116 | + |
| 117 | +# Use them easily |
| 118 | +callbacks = [ |
| 119 | + Metric(:gap, MyMetrics.gap; on=:both), |
| 120 | + Metric(:regret, MyMetrics.regret; on=:both), |
| 121 | + Metric(:test_accuracy, MyMetrics.accuracy; on=test_instances), |
| 122 | + Metric(:overfitting, MyMetrics.overfitting_indicator), |
| 123 | +] |
| 124 | + |
| 125 | +# ============================================================================ |
| 126 | +# Advanced: Higher-order functions |
| 127 | +# ============================================================================ |
| 128 | + |
| 129 | +# Create a metric factory that returns properly-signed functions |
| 130 | +function dataset_metric(benchmark, compute_fn) |
| 131 | + return (model, max, data, ctx) -> compute_fn(benchmark, data, model, max) |
| 132 | +end |
| 133 | + |
| 134 | +# Use it |
| 135 | +callbacks = [ |
| 136 | + Metric(:gap, dataset_metric(b, compute_gap); on=:both), |
| 137 | + Metric(:regret, dataset_metric(b, compute_regret); on=:both), |
| 138 | +] |
| 139 | + |
| 140 | +# ============================================================================ |
| 141 | +# Migration Helper |
| 142 | +# ============================================================================ |
| 143 | + |
| 144 | +# If you have old-style functions: (model, max, data) -> value |
| 145 | +# Wrap them easily: |
| 146 | +old_compute_gap = (model, max, data) -> compute_gap(b, data, model, max) |
| 147 | + |
| 148 | +# Convert to new signature: |
| 149 | +new_compute_gap = (model, max, data, ctx) -> old_compute_gap(model, max, data) |
| 150 | +# Or more concisely: |
| 151 | +new_compute_gap = (model, max, data, _) -> old_compute_gap(model, max, data) |
| 152 | + |
| 153 | +Metric(:gap, new_compute_gap; on=:both) |
0 commit comments