import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20scipy.special%20import%20expit%0A%20%20%20%20from%20sklearn.datasets%20import%20make_classification%0A%20%20%20%20from%20sklearn.metrics%20import%20(%0A%20%20%20%20%20%20%20%20average_precision_score%2C%0A%20%20%20%20%20%20%20%20balanced_accuracy_score%2C%0A%20%20%20%20%20%20%20%20f1_score%2C%0A%20%20%20%20%20%20%20%20precision_score%2C%0A%20%20%20%20%20%20%20%20recall_score%2C%0A%20%20%20%20)%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%20%20%20%20import%20torch%0A%20%20%20%20import%20torch.nn%20as%20nn%0A%20%20%20%20import%20torch.optim%20as%20optim%0A%20%20%20%20from%20torch.utils.data%20import%20DataLoader%2C%20TensorDataset%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DataLoader%2C%0A%20%20%20%20%20%20%20%20TensorDataset%2C%0A%20%20%20%20%20%20%20%20average_precision_score%2C%0A%20%20%20%20%20%20%20%20balanced_accuracy_score%2C%0A%20%20%20%20%20%20%20%20expit%2C%0A%20%20%20%20%20%20%20%20f1_score%2C%0A%20%20%20%20%20%20%20%20go%2C%0A%20%20%20%20%20%20%20%20make_classification%2C%0A%20%20%20%20%20%20%20%20make_subplots%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20nn%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20optim%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20%20%20%20%20precision_score%2C%0A%20%20%20%20%20%20%20%20recall_score%2C%0A%20%20%20%20%20%20%20%20torch%2C%0A%20%20%20%20%20%20%20%20train_test_split%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%5B%E2%86%90%2048%20Temperature%20Scaled%20Softmax%5D(48_temperature_scaled_softmax.py)%20%7C%20%5BIndex%5D(..%2Findex.html)%20%7C%20%5B50%20Scaled%20Dot-Product%20Attention%20%E2%86%92%5D(50_scaled_dot_product_attention.py)%0A%0A%20%20%20%20%23%2049.%20Class-Balanced%20Focal%20Loss%3A%20Dynamically%20Down-Weighting%20Easy%20Negatives%20in%20Imbalanced%20Regimes%0A%0A%20%20%20%20%23%23%23%20Executive%20Summary%0A%0A%20%20%20%20In%20dense%20object%20detection%2C%20rare%20disease%20diagnostics%2C%20and%20financial%20fraud%20surveillance%2C%20class%20distributions%20are%20characterized%20by%20extreme%20skew%2C%20where%20the%20background%20(negative)%20class%20outnumbers%20the%20foreground%20(positive)%20class%20by%20ratios%20of%20%24100%3A1%24%20to%20%24100%7B%2C%7D000%3A1%24.%20While%20individual%20easy%20negative%20examples%20each%20incur%20a%20tiny%20cross-entropy%20penalty%2C%20their%20sheer%20volume%20generates%20a%20cumulative%20gradient%20signal%20that%20drowns%20out%20the%20sparse%20gradient%20contributions%20of%20hard%20positive%20instances%20during%20stochastic%20gradient%20descent.%0A%0A%20%20%20%20**Focal%20Loss**%20(Lin%20et%20al.%2C%202017)%20overcomes%20this%20structural%20failure%20mode%20by%20introducing%20a%20dynamic%20modulating%20factor%20%24(1%20-%20p_t)%5E%5Cgamma%24%20into%20the%20standard%20binary%20cross-entropy%20objective.%20This%20factor%20automatically%20suppresses%20the%20loss%20and%20backpropagated%20gradients%20for%20well-classified%20examples%20(%24p_t%20%5Cgg%200.5%24)%20while%20preserving%20gradients%20for%20misclassified%2C%20ambiguous%2C%20or%20hard%20boundary%20samples.%20Combined%20with%20an%20%24%5Calpha%24-balancing%20scalar%2C%20Focal%20Loss%20re-aligns%20gradient%20dynamics%20toward%20the%20true%20minority%20decision%20boundary%20without%20requiring%20artificial%20sampling%20tricks%20or%20heuristic%20bootstrapping.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20%5Bb%5D%20Mathematical%20Foundations%20and%20Gradient%20Mechanics%0A%0A%20%20%20%20%23%23%23%201.%20The%20Pathology%20of%20Standard%20Binary%20Cross-Entropy%0A%0A%20%20%20%20Consider%20binary%20classification%20with%20target%20%24y%20%5Cin%20%5C%7B0%2C%201%5C%7D%24%20and%20predicted%20probability%20%24p%20%3D%20%5Csigma(z)%20%5Cin%20(0%2C%201)%24%2C%20where%20%24z%20%5Cin%20%5Cmathbb%7BR%7D%24%20is%20the%20unnormalized%20logit%20and%20%24%5Csigma(z)%20%3D%20(1%20%2B%20e%5E%7B-z%7D)%5E%7B-1%7D%24.%20For%20notational%20convenience%2C%20define%20the%20ground-truth%20probability%20%24p_t%24%3A%0A%0A%20%20%20%20%24%24p_t%20%3D%20%5Cbegin%7Bcases%7D%20p%20%26%20%5Ctext%7Bif%20%7D%20y%20%3D%201%20%5C%5C%201%20-%20p%20%26%20%5Ctext%7Bif%20%7D%20y%20%3D%200%20%5Cend%7Bcases%7D%24%24%0A%0A%20%20%20%20The%20conventional%20Binary%20Cross-Entropy%20(BCE)%20loss%20is%20expressed%20concisely%20as%3A%0A%0A%20%20%20%20%24%24%5Ctext%7BCE%7D(p%2C%20y)%20%3D%20%5Ctext%7BCE%7D(p_t)%20%3D%20-%5Cln(p_t)%24%24%0A%0A%20%20%20%20In%20extreme%20imbalance%20regimes%20(e.g.%2C%20%2499%5C%25%24%20negative%20background%20instances)%2C%20the%20vast%20majority%20of%20negative%20samples%20are%20easily%20distinguishable%20(%24p_t%20%5Cge%200.99%24).%20Although%20the%20loss%20for%20a%20single%20easy%20sample%20is%20modest%20(%24-%5Cln(0.99)%20%5Capprox%200.01005%24)%2C%20summing%20over%20%24N_%7B%5Ctext%7Bneg%7D%7D%20%3D%2010%5E5%24%20examples%20yields%3A%0A%0A%20%20%20%20%24%24%5Csum_%7Bi%3D1%7D%5E%7BN_%7B%5Ctext%7Bneg%7D%7D%7D%20%5Ctext%7BCE%7D(p_%7Bt%2C%20i%7D)%20%5Capprox%2010%5E5%20%5Ctimes%200.01005%20%3D%201%7B%2C%7D005%24%24%0A%0A%20%20%20%20Meanwhile%2C%20100%20rare%20positive%20examples%20(%24y%3D1%24)%2C%20even%20if%20completely%20misclassified%20with%20%24p_t%20%3D%200.01%24%2C%20yield%20only%3A%0A%0A%20%20%20%20%24%24%5Csum_%7Bj%3D1%7D%5E%7BN_%7B%5Ctext%7Bpos%7D%7D%7D%20%5Ctext%7BCE%7D(p_%7Bt%2C%20j%7D)%20%5Capprox%20100%20%5Ctimes%20(-%5Cln(0.01))%20%3D%20100%20%5Ctimes%204.605%20%3D%20460.5%24%24%0A%0A%20%20%20%20The%20easy%20negative%20examples%20contribute%20over%20%2468%5C%25%24%20of%20the%20total%20loss%20and%20dominate%20the%20optimization%20surface%2C%20driving%20network%20weights%20toward%20degenerate%20solutions%20that%20predict%20the%20majority%20class.%0A%0A%20%20%20%20%23%23%23%202.%20The%20Focal%20Loss%20Formulation%0A%0A%20%20%20%20To%20neutralize%20easy%20example%20dominance%2C%20Lin%20et%20al.%20(2017)%20introduce%20a%20power-law%20modulating%20term%20%24(1%20-%20p_t)%5E%5Cgamma%24%20parameterized%20by%20the%20focusing%20factor%20%24%5Cgamma%20%5Cge%200%24%2C%20combined%20with%20an%20%24%5Calpha_t%24%20class-weighting%20factor%3A%0A%0A%20%20%20%20%24%24%5Ctext%7BFL%7D(p_t)%20%3D%20-%5Calpha_t%20(1%20-%20p_t)%5E%5Cgamma%20%5Cln(p_t)%24%24%0A%0A%20%20%20%20where%20the%20class%20balance%20scalar%20%24%5Calpha_t%20%5Cin%20%5B0%2C%201%5D%24%20is%20defined%20as%3A%0A%0A%20%20%20%20%24%24%5Calpha_t%20%3D%20%5Cbegin%7Bcases%7D%20%5Calpha%20%26%20%5Ctext%7Bif%20%7D%20y%20%3D%201%20%5C%5C%201%20-%20%5Calpha%20%26%20%5Ctext%7Bif%20%7D%20y%20%3D%200%20%5Cend%7Bcases%7D%24%24%0A%0A%20%20%20%20%23%23%23%23%20Behavior%20Across%20Regimes%3A%0A%20%20%20%20-%20When%20an%20instance%20is%20misclassified%20and%20%24p_t%20%5Cto%200%24%2C%20the%20modulating%20factor%20%24(1%20-%20p_t)%5E%5Cgamma%20%5Cto%201%24%2C%20leaving%20the%20loss%20virtually%20unaltered%20compared%20to%20standard%20cross-entropy.%0A%20%20%20%20-%20As%20an%20instance%20becomes%20well-classified%20(%24p_t%20%5Cto%201%24)%2C%20the%20modulating%20factor%20%24(1%20-%20p_t)%5E%5Cgamma%20%5Cto%200%24%2C%20exponentially%20attenuating%20the%20loss.%0A%20%20%20%20-%20When%20%24%5Cgamma%20%3D%200%24%20and%20%24%5Calpha%20%3D%200.5%24%2C%20Focal%20Loss%20reduces%20identically%20to%20standard%20Binary%20Cross-Entropy.%0A%0A%20%20%20%20%23%23%23%203.%20Quantitative%20Impact%20of%20the%20Focusing%20Parameter%20%24%5Cgamma%24%0A%0A%20%20%20%20Let%20us%20evaluate%20the%20loss%20scaling%20factor%20%24(1%20-%20p_t)%5E%5Cgamma%24%20for%20an%20easy%20example%20with%20%24p_t%20%3D%200.99%24%3A%0A%0A%20%20%20%20-%20For%20%24%5Cgamma%20%3D%200%24%3A%20%24(1%20-%200.99)%5E0%20%3D%201.0%24%20(no%20down-weighting)%0A%20%20%20%20-%20For%20%24%5Cgamma%20%3D%201%24%3A%20%24(1%20-%200.99)%5E1%20%3D%200.01%24%20(%24100%5Ctimes%24%20suppression)%0A%20%20%20%20-%20For%20%24%5Cgamma%20%3D%202%24%3A%20%24(1%20-%200.99)%5E2%20%3D%200.0001%24%20(%2410%7B%2C%7D000%5Ctimes%24%20suppression)%0A%20%20%20%20-%20For%20%24%5Cgamma%20%3D%205%24%3A%20%24(1%20-%200.99)%5E5%20%3D%2010%5E%7B-10%7D%24%20(complete%20suppression)%0A%0A%20%20%20%20Under%20%24%5Cgamma%20%3D%202%24%2C%20the%20%24100%7B%2C%7D000%24%20easy%20background%20negatives%20that%20previously%20accumulated%20%241%7B%2C%7D005%24%20loss%20units%20now%20contribute%20a%20total%20loss%20of%20only%3A%0A%0A%20%20%20%20%24%2410%5E5%20%5Ctimes%200.0001%20%5Ctimes%200.01005%20%5Capprox%200.1005%24%24%0A%0A%20%20%20%20The%20hard%20positives%20now%20dominate%20the%20loss%20(%24460.5%20%5Ctext%7B%20vs%20%7D%200.10%24)%2C%20completely%20redirecting%20the%20model's%20capacity%20toward%20learning%20the%20minority%20class.%0A%0A%20%20%20%20%23%23%23%204.%20Gradient%20Derivation%20with%20Respect%20to%20Logits%0A%0A%20%20%20%20To%20understand%20how%20Focal%20Loss%20impacts%20backpropagation%2C%20we%20compute%20the%20analytical%20gradient%20with%20respect%20to%20the%20input%20logit%20%24z%24.%20Let%20%24y%5E*%20%5Cin%20%5C%7B-1%2C%20%2B1%5C%7D%24%20such%20that%20%24y%5E*%20%3D%202y%20-%201%24.%20Then%20%24p_t%20%3D%20%5Csigma(y%5E*%20z)%20%3D%20(1%20%2B%20e%5E%7B-y%5E*%20z%7D)%5E%7B-1%7D%24.%20The%20derivative%20of%20%24p_t%24%20with%20respect%20to%20%24z%24%20is%3A%0A%0A%20%20%20%20%24%24%5Cfrac%7B%5Cpartial%20p_t%7D%7B%5Cpartial%20z%7D%20%3D%20y%5E*%20p_t%20(1%20-%20p_t)%24%24%0A%0A%20%20%20%20Differentiating%20%24%5Ctext%7BFL%7D(p_t)%20%3D%20-%5Calpha_t%20(1%20-%20p_t)%5E%5Cgamma%20%5Cln(p_t)%24%20with%20respect%20to%20%24z%24%20using%20the%20product%20rule%3A%0A%0A%20%20%20%20%24%24%5Cfrac%7B%5Cpartial%20%5Ctext%7BFL%7D%7D%7B%5Cpartial%20z%7D%20%3D%20%5Cfrac%7B%5Cpartial%20%5Ctext%7BFL%7D%7D%7B%5Cpartial%20p_t%7D%20%5Ccdot%20%5Cfrac%7B%5Cpartial%20p_t%7D%7B%5Cpartial%20z%7D%24%24%0A%0A%20%20%20%20%24%24%5Cfrac%7B%5Cpartial%20%5Ctext%7BFL%7D%7D%7B%5Cpartial%20p_t%7D%20%3D%20-%5Calpha_t%20%5Cleft%5B%20-%5Cgamma%20(1%20-%20p_t)%5E%7B%5Cgamma%20-%201%7D%20%5Cln(p_t)%20%2B%20%5Cfrac%7B(1%20-%20p_t)%5E%5Cgamma%7D%7Bp_t%7D%20%5Cright%5D%24%24%0A%0A%20%20%20%20Multiplying%20by%20%24%5Cfrac%7B%5Cpartial%20p_t%7D%7B%5Cpartial%20z%7D%20%3D%20y%5E*%20p_t%20(1%20-%20p_t)%24%3A%0A%0A%20%20%20%20%24%24%5Cfrac%7B%5Cpartial%20%5Ctext%7BFL%7D%7D%7B%5Cpartial%20z%7D%20%3D%20y%5E*%20%5Calpha_t%20(1%20-%20p_t)%5E%5Cgamma%20%5Cleft%5B%20%5Cgamma%20p_t%20%5Cln(p_t)%20%2B%20p_t%20-%201%20%5Cright%5D%24%24%0A%0A%20%20%20%20For%20%24%5Cgamma%20%3D%200%24%20(Standard%20BCE%20with%20%24%5Calpha_t%20%3D%201%24)%2C%20this%20simplifies%20directly%20to%20the%20classical%20error%20residual%3A%0A%0A%20%20%20%20%24%24%5Cleft.%5Cfrac%7B%5Cpartial%20%5Ctext%7BFL%7D%7D%7B%5Cpartial%20z%7D%5Cright%7C_%7B%5Cgamma%3D0%2C%20%5Calpha_t%3D1%7D%20%3D%20y%5E*%20(p_t%20-%201)%20%3D%20p%20-%20y%24%24%0A%0A%20%20%20%20For%20%24%5Cgamma%20%3E%200%24%2C%20as%20%24p_t%20%5Cto%201%24%20(well-classified%20easy%20samples)%2C%20the%20factor%20%24(1%20-%20p_t)%5E%5Cgamma%20%5Cto%200%24%20aggressively%20forces%20the%20gradient%20to%20zero.%20The%20network%20receives%20negligible%20parameter%20updates%20from%20confident%20predictions%2C%20dedicating%20its%20entire%20gradient%20budget%20to%20unresolved%20boundary%20cases.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20mo%2C%20np)%3A%0A%20%20%20%20%23%20Generate%20probability%20grid%20for%20visualization%0A%20%20%20%20pt_grid%20%3D%20np.linspace(0.001%2C%200.999%2C%20500)%0A%20%20%20%20gamma_values%20%3D%20%5B0.0%2C%200.5%2C%201.0%2C%202.0%2C%205.0%5D%0A%20%20%20%20gamma_colors%20%3D%20%7B%0A%20%20%20%20%20%20%20%200.0%3A%20%22%231D4ED8%22%2C%20%20%23%20Standard%20CE%3A%20Blue%0A%20%20%20%20%20%20%20%200.5%3A%20%22%230D9488%22%2C%20%20%23%20Teal%0A%20%20%20%20%20%20%20%201.0%3A%20%22%23F59E0B%22%2C%20%20%23%20Amber%0A%20%20%20%20%20%20%20%202.0%3A%20%22%23DC2626%22%2C%20%20%23%20Red%20(RetinaNet%20standard)%0A%20%20%20%20%20%20%20%205.0%3A%20%22%237C3AED%22%2C%20%20%23%20Purple%0A%20%20%20%20%7D%0A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EFocal%20Loss%20Curves%20vs%20True%20Probability%20pt%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EAbsolute%20Gradient%20Magnitude%20%7CdFL%2Fdz%7C%20vs%20pt%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.14%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%201%3A%20Focal%20Loss%20curves%0A%20%20%20%20for%20g%20in%20gamma_values%3A%0A%20%20%20%20%20%20%20%20fl_curve%20%3D%20-((1.0%20-%20pt_grid)%20**%20g)%20*%20np.log(pt_grid)%0A%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dpt_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dfl_curve%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3Dgamma_colors%5Bg%5D%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22gamma%20%3D%20%7Bg%7D%22%20%2B%20(%22%20(CE)%22%20if%20g%20%3D%3D%200.0%20else%20%22%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%202%3A%20Gradient%20magnitude%20%7CdFL%2Fdz%7C%20assuming%20y%3D1%20(pt%20%3D%20p)%0A%20%20%20%20for%20g%20in%20gamma_values%3A%0A%20%20%20%20%20%20%20%20%23%20Gradient%20formula%3A%20(1%20-%20pt)%5Egamma%20*%20%7Cgamma%20*%20pt%20*%20ln(pt)%20%2B%20pt%20-%201%7C%0A%20%20%20%20%20%20%20%20grad_mag%20%3D%20((1.0%20-%20pt_grid)%20**%20g)%20*%20np.abs(g%20*%20pt_grid%20*%20np.log(pt_grid)%20%2B%20pt_grid%20-%201.0)%0A%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dpt_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dgrad_mag%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3Dgamma_colors%5Bg%5D%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22grad%20(gamma%20%3D%20%7Bg%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20fig.update_xaxes(title_text%3D%22Probability%20of%20Ground-Truth%20Class%20pt%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22Focal%20Loss%20FL(pt)%22%2C%20range%3D%5B0%2C%205.0%5D%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_xaxes(title_text%3D%22Probability%20of%20Ground-Truth%20Class%20pt%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22Logit%20Gradient%20Magnitude%20%7CdFL%2Fdz%7C%22%2C%20range%3D%5B0%2C%201.05%5D%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D500%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D70%2C%20b%3D50)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.28%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20viz%20%3D%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DataLoader%2C%0A%20%20%20%20TensorDataset%2C%0A%20%20%20%20average_precision_score%2C%0A%20%20%20%20balanced_accuracy_score%2C%0A%20%20%20%20expit%2C%0A%20%20%20%20f1_score%2C%0A%20%20%20%20make_classification%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20nn%2C%0A%20%20%20%20np%2C%0A%20%20%20%20optim%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20precision_score%2C%0A%20%20%20%20recall_score%2C%0A%20%20%20%20torch%2C%0A%20%20%20%20train_test_split%2C%0A)%3A%0A%20%20%20%20%23%20Vectorized%20NumPy%20Focal%20Loss%20implementation%0A%20%20%20%20def%20numpy_focal_loss(y_true%2C%20logits%2C%20gamma%3D2.0%2C%20alpha%3D0.25)%3A%0A%20%20%20%20%20%20%20%20probs%20%3D%20expit(logits)%0A%20%20%20%20%20%20%20%20pt%20%3D%20np.where(y_true%20%3D%3D%201%2C%20probs%2C%201.0%20-%20probs)%0A%20%20%20%20%20%20%20%20alpha_t%20%3D%20np.where(y_true%20%3D%3D%201%2C%20alpha%2C%201.0%20-%20alpha)%0A%20%20%20%20%20%20%20%20loss%20%3D%20-alpha_t%20*%20((1.0%20-%20pt)%20**%20gamma)%20*%20np.log(np.maximum(pt%2C%201e-12))%0A%20%20%20%20%20%20%20%20return%20np.mean(loss)%0A%0A%20%20%20%20def%20numpy_focal_grad(y_true%2C%20logits%2C%20gamma%3D2.0%2C%20alpha%3D0.25)%3A%0A%20%20%20%20%20%20%20%20probs%20%3D%20expit(logits)%0A%20%20%20%20%20%20%20%20pt%20%3D%20np.where(y_true%20%3D%3D%201%2C%20probs%2C%201.0%20-%20probs)%0A%20%20%20%20%20%20%20%20alpha_t%20%3D%20np.where(y_true%20%3D%3D%201%2C%20alpha%2C%201.0%20-%20alpha)%0A%20%20%20%20%20%20%20%20%23%20y_star%20%3D%20%2B1%20for%20y%3D1%2C%20-1%20for%20y%3D0%0A%20%20%20%20%20%20%20%20y_star%20%3D%20np.where(y_true%20%3D%3D%201%2C%201.0%2C%20-1.0)%0A%20%20%20%20%20%20%20%20grad%20%3D%20y_star%20*%20alpha_t%20*%20((1.0%20-%20pt)%20**%20gamma)%20*%20(gamma%20*%20pt%20*%20np.log(np.maximum(pt%2C%201e-12))%20%2B%20pt%20-%201.0)%0A%20%20%20%20%20%20%20%20return%20grad%0A%0A%20%20%20%20%23%20Numerical%20gradient%20check%20using%20finite%20differences%0A%20%20%20%20_eps%20%3D%201e-6%0A%20%20%20%20_test_y%20%3D%20np.array(%5B1%2C%200%2C%201%2C%200%5D)%0A%20%20%20%20_test_z%20%3D%20np.array(%5B1.5%2C%20-2.0%2C%20-0.8%2C%203.2%5D)%0A%20%20%20%20_analytical_grads%20%3D%20numpy_focal_grad(_test_y%2C%20_test_z%2C%20gamma%3D2.0%2C%20alpha%3D0.25)%0A%20%20%20%20_numerical_grads%20%3D%20%5B%5D%0A%20%20%20%20for%20_i%20in%20range(len(_test_z))%3A%0A%20%20%20%20%20%20%20%20_z_plus%20%3D%20_test_z.copy()%0A%20%20%20%20%20%20%20%20_z_plus%5B_i%5D%20%2B%3D%20_eps%0A%20%20%20%20%20%20%20%20_z_minus%20%3D%20_test_z.copy()%0A%20%20%20%20%20%20%20%20_z_minus%5B_i%5D%20-%3D%20_eps%0A%20%20%20%20%20%20%20%20%23%20Scale%20by%20N%20because%20focal_loss%20computes%20mean%0A%20%20%20%20%20%20%20%20_loss_plus%20%3D%20numpy_focal_loss(_test_y%2C%20_z_plus%2C%20gamma%3D2.0%2C%20alpha%3D0.25)%20*%20len(_test_z)%0A%20%20%20%20%20%20%20%20_loss_minus%20%3D%20numpy_focal_loss(_test_y%2C%20_z_minus%2C%20gamma%3D2.0%2C%20alpha%3D0.25)%20*%20len(_test_z)%0A%20%20%20%20%20%20%20%20_num_g%20%3D%20(_loss_plus%20-%20_loss_minus)%20%2F%20(2.0%20*%20_eps)%0A%20%20%20%20%20%20%20%20_numerical_grads.append(_num_g)%0A%0A%20%20%20%20_numerical_grads%20%3D%20np.array(_numerical_grads)%0A%20%20%20%20_grad_errors%20%3D%20np.abs(_analytical_grads%20-%20_numerical_grads)%0A%0A%20%20%20%20df_grad_check%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22True_Label%22%3A%20_test_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Input_Logit%22%3A%20_test_z%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Analytical_Grad%22%3A%20np.round(_analytical_grads%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Numerical_Grad%22%3A%20np.round(_numerical_grads%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Abs_Difference%22%3A%20%5Bf%22%7Berr%3A.2e%7D%22%20for%20err%20in%20_grad_errors%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Status%22%3A%20%5B%22Verified%20Match%22%20if%20err%20%3C%201e-4%20else%20%22Discrepancy%22%20for%20err%20in%20_grad_errors%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Synthetic%20highly%20imbalanced%20classification%20experiment%20(95%25%20Negative%2C%205%25%20Positive)%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20torch.manual_seed(42)%0A%0A%20%20%20%20X_raw%2C%20y_raw%20%3D%20make_classification(%0A%20%20%20%20%20%20%20%20n_samples%3D2500%2C%0A%20%20%20%20%20%20%20%20n_features%3D16%2C%0A%20%20%20%20%20%20%20%20n_informative%3D10%2C%0A%20%20%20%20%20%20%20%20n_classes%3D2%2C%0A%20%20%20%20%20%20%20%20weights%3D%5B0.95%2C%200.05%5D%2C%0A%20%20%20%20%20%20%20%20flip_y%3D0.01%2C%0A%20%20%20%20%20%20%20%20random_state%3D42%2C%0A%20%20%20%20)%0A%0A%20%20%20%20X_train_raw%2C%20X_test_raw%2C%20y_train_raw%2C%20y_test_raw%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20X_raw%2C%20y_raw%2C%20test_size%3D0.3%2C%20stratify%3Dy_raw%2C%20random_state%3D42%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20PyTorch%20Tensors%20and%20Loader%0A%20%20%20%20_X_tr_t%20%3D%20torch.tensor(X_train_raw%2C%20dtype%3Dtorch.float32)%0A%20%20%20%20_y_tr_t%20%3D%20torch.tensor(y_train_raw%2C%20dtype%3Dtorch.float32).unsqueeze(1)%0A%20%20%20%20_X_te_t%20%3D%20torch.tensor(X_test_raw%2C%20dtype%3Dtorch.float32)%0A%0A%20%20%20%20_ds%20%3D%20TensorDataset(_X_tr_t%2C%20_y_tr_t)%0A%20%20%20%20_loader%20%3D%20DataLoader(_ds%2C%20batch_size%3D64%2C%20shuffle%3DTrue)%0A%0A%20%20%20%20%23%20PyTorch%20Model%0A%20%20%20%20class%20SimpleClassifier(nn.Module)%3A%0A%20%20%20%20%20%20%20%20def%20__init__(self%2C%20in_features%3D16)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20super().__init__()%0A%20%20%20%20%20%20%20%20%20%20%20%20self.net%20%3D%20nn.Sequential(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nn.Linear(in_features%2C%2032)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nn.ReLU()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nn.Linear(32%2C%2016)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nn.ReLU()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nn.Linear(16%2C%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20def%20forward(self%2C%20x)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.net(x)%0A%0A%20%20%20%20%23%20PyTorch%20Focal%20Loss%20Module%0A%20%20%20%20class%20PyTorchFocalLoss(nn.Module)%3A%0A%20%20%20%20%20%20%20%20def%20__init__(self%2C%20gamma%3D2.0%2C%20alpha%3D0.25)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20super().__init__()%0A%20%20%20%20%20%20%20%20%20%20%20%20self.gamma%20%3D%20gamma%0A%20%20%20%20%20%20%20%20%20%20%20%20self.alpha%20%3D%20alpha%0A%0A%20%20%20%20%20%20%20%20def%20forward(self%2C%20logits%2C%20targets)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20probs%20%3D%20torch.sigmoid(logits)%0A%20%20%20%20%20%20%20%20%20%20%20%20pt%20%3D%20torch.where(targets%20%3D%3D%201.0%2C%20probs%2C%201.0%20-%20probs)%0A%20%20%20%20%20%20%20%20%20%20%20%20alpha_t%20%3D%20torch.where(targets%20%3D%3D%201.0%2C%20self.alpha%2C%201.0%20-%20self.alpha)%0A%20%20%20%20%20%20%20%20%20%20%20%20loss%20%3D%20-alpha_t%20*%20((1.0%20-%20pt)%20**%20self.gamma)%20*%20torch.log(torch.clamp(pt%2C%20min%3D1e-12))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20torch.mean(loss)%0A%0A%20%20%20%20%23%20Training%20routine%0A%20%20%20%20def%20train_classifier(criterion%2C%20epochs%3D35%2C%20lr%3D0.01)%3A%0A%20%20%20%20%20%20%20%20torch.manual_seed(42)%0A%20%20%20%20%20%20%20%20model%20%3D%20SimpleClassifier()%0A%20%20%20%20%20%20%20%20optimizer%20%3D%20optim.Adam(model.parameters()%2C%20lr%3Dlr)%0A%20%20%20%20%20%20%20%20model.train()%0A%20%20%20%20%20%20%20%20for%20_ep%20in%20range(epochs)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20bx%2C%20by%20in%20_loader%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20optimizer.zero_grad()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20out%20%3D%20model(bx)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20loss%20%3D%20criterion(out%2C%20by)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20loss.backward()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20optimizer.step()%0A%20%20%20%20%20%20%20%20model.eval()%0A%20%20%20%20%20%20%20%20return%20model%0A%0A%20%20%20%20%23%201.%20Standard%20BCE%20Model%0A%20%20%20%20model_bce%20%3D%20train_classifier(nn.BCEWithLogitsLoss())%0A%20%20%20%20%23%202.%20Focal%20Loss%20Model%20(gamma%3D2%2C%20alpha%3D0.25)%0A%20%20%20%20model_fl%20%3D%20train_classifier(PyTorchFocalLoss(gamma%3D2.0%2C%20alpha%3D0.25))%0A%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20test_logits_bce%20%3D%20model_bce(_X_te_t).squeeze().numpy()%0A%20%20%20%20%20%20%20%20test_probs_bce%20%3D%20expit(test_logits_bce)%0A%20%20%20%20%20%20%20%20test_preds_bce%20%3D%20(test_probs_bce%20%3E%3D%200.5).astype(int)%0A%0A%20%20%20%20%20%20%20%20test_logits_fl%20%3D%20model_fl(_X_te_t).squeeze().numpy()%0A%20%20%20%20%20%20%20%20test_probs_fl%20%3D%20expit(test_logits_fl)%0A%20%20%20%20%20%20%20%20test_preds_fl%20%3D%20(test_probs_fl%20%3E%3D%200.5).astype(int)%0A%0A%20%20%20%20def%20evaluate_predictions(y_true%2C%20y_pred%2C%20y_prob)%3A%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Balanced_Accuracy%22%3A%20balanced_accuracy_score(y_true%2C%20y_pred)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Precision_Minority%22%3A%20precision_score(y_true%2C%20y_pred%2C%20zero_division%3D0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Recall_Minority%22%3A%20recall_score(y_true%2C%20y_pred%2C%20zero_division%3D0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22F1_Minority%22%3A%20f1_score(y_true%2C%20y_pred%2C%20zero_division%3D0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22PR_AUC_AP%22%3A%20average_precision_score(y_true%2C%20y_prob)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20bce_metrics%20%3D%20evaluate_predictions(y_test_raw%2C%20test_preds_bce%2C%20test_probs_bce)%0A%20%20%20%20fl_metrics%20%3D%20evaluate_predictions(y_test_raw%2C%20test_preds_fl%2C%20test_probs_fl)%0A%0A%20%20%20%20df_imbalance_eval%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Objective_Function%22%3A%20%22Binary%20Cross-Entropy%20(BCE)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hyperparameters%22%3A%20%22gamma%20%3D%200%2C%20alpha%20%3D%200.5%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Balanced_Accuracy%22%3A%20f%22%7Bbce_metrics%5B'Balanced_Accuracy'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Precision%20(Class%201)%22%3A%20f%22%7Bbce_metrics%5B'Precision_Minority'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Recall%20(Class%201)%22%3A%20f%22%7Bbce_metrics%5B'Recall_Minority'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22F1-Score%20(Class%201)%22%3A%20f%22%7Bbce_metrics%5B'F1_Minority'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22PR-AUC%20(Avg%20Precision)%22%3A%20f%22%7Bbce_metrics%5B'PR_AUC_AP'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Objective_Function%22%3A%20%22Class-Balanced%20Focal%20Loss%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hyperparameters%22%3A%20%22gamma%20%3D%202.0%2C%20alpha%20%3D%200.25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Balanced_Accuracy%22%3A%20f%22%7Bfl_metrics%5B'Balanced_Accuracy'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Precision%20(Class%201)%22%3A%20f%22%7Bfl_metrics%5B'Precision_Minority'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Recall%20(Class%201)%22%3A%20f%22%7Bfl_metrics%5B'Recall_Minority'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22F1-Score%20(Class%201)%22%3A%20f%22%7Bfl_metrics%5B'F1_Minority'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22PR-AUC%20(Avg%20Precision)%22%3A%20f%22%7Bfl_metrics%5B'PR_AUC_AP'%5D%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Example%203%3A%20Gradient%20Breakdown%20Analysis%20(Easy%20Negatives%20vs%20Hard%20Positives)%0A%20%20%20%20%23%20Compute%20cumulative%20gradient%20contribution%20on%20the%20entire%20training%20set%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20tr_logits%20%3D%20model_bce(_X_tr_t).squeeze().numpy()%0A%20%20%20%20%20%20%20%20tr_probs%20%3D%20expit(tr_logits)%0A%20%20%20%20%20%20%20%20tr_pt%20%3D%20np.where(y_train_raw%20%3D%3D%201%2C%20tr_probs%2C%201.0%20-%20tr_probs)%0A%0A%20%20%20%20%20%20%20%20%23%20Gradients%20under%20BCE%20(gamma%3D0%2C%20alpha%3D1.0)%0A%20%20%20%20%20%20%20%20grad_bce%20%3D%20np.abs(tr_probs%20-%20y_train_raw)%0A%0A%20%20%20%20%20%20%20%20%23%20Gradients%20under%20Focal%20Loss%20(gamma%3D2%2C%20alpha%3D0.25)%0A%20%20%20%20%20%20%20%20grad_fl%20%3D%20np.abs(numpy_focal_grad(y_train_raw%2C%20tr_logits%2C%20gamma%3D2.0%2C%20alpha%3D0.25))%0A%0A%20%20%20%20is_easy_neg%20%3D%20(y_train_raw%20%3D%3D%200)%20%26%20(tr_pt%20%3E%3D%200.8)%0A%20%20%20%20is_hard_or_pos%20%3D%20~is_easy_neg%0A%0A%20%20%20%20bce_easy_neg_grad_share%20%3D%20np.sum(grad_bce%5Bis_easy_neg%5D)%20%2F%20np.sum(grad_bce)%20*%20100.0%0A%20%20%20%20fl_easy_neg_grad_share%20%3D%20np.sum(grad_fl%5Bis_easy_neg%5D)%20%2F%20np.sum(grad_fl)%20*%20100.0%0A%0A%20%20%20%20bce_target_grad_share%20%3D%20np.sum(grad_bce%5Bis_hard_or_pos%5D)%20%2F%20np.sum(grad_bce)%20*%20100.0%0A%20%20%20%20fl_target_grad_share%20%3D%20np.sum(grad_fl%5Bis_hard_or_pos%5D)%20%2F%20np.sum(grad_fl)%20*%20100.0%0A%0A%20%20%20%20df_grad_shares%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Loss_Function%22%3A%20%22Standard%20Binary%20Cross-Entropy%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Easy_Negatives_Count%22%3A%20f%22%7Bint(np.sum(is_easy_neg))%7D%20(%7Bnp.mean(is_easy_neg)%20*%20100%3A.1f%7D%25)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Easy_Negatives_Gradient_Share%22%3A%20f%22%7Bbce_easy_neg_grad_share%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hard_%26_Minority_Gradient_Share%22%3A%20f%22%7Bbce_target_grad_share%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimization_Regime%22%3A%20%22Negatives%20Dominate%20Optimization%20Gradient%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Loss_Function%22%3A%20%22Class-Balanced%20Focal%20Loss%20(gamma%3D2)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Easy_Negatives_Count%22%3A%20f%22%7Bint(np.sum(is_easy_neg))%7D%20(%7Bnp.mean(is_easy_neg)%20*%20100%3A.1f%7D%25)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Easy_Negatives_Gradient_Share%22%3A%20f%22%7Bfl_easy_neg_grad_share%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hard_%26_Minority_Gradient_Share%22%3A%20f%22%7Bfl_target_grad_share%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimization_Regime%22%3A%20%22Minority%20%2F%20Hard%20Cases%20Drive%20Updates%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20table_grad_check%20%3D%20mo.ui.table(df_grad_check)%0A%20%20%20%20table_eval%20%3D%20mo.ui.table(df_imbalance_eval)%0A%20%20%20%20table_shares%20%3D%20mo.ui.table(df_grad_shares)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b140dd2d67824331a92267bc001c9889