import%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20for%20var%20in%20(%22PROJ_LIB%22%2C%20%22PROJ_DATA%22%2C%20%22GDAL_DATA%22)%3A%0A%20%20%20%20%20%20%20%20os.environ.pop(var%2C%20None)%20%20%23%20a%20conda%20PROJ%20install%20on%20the%20path%20breaks%20rasterio's%20bundled%20one%0A%0A%20%20%20%20import%20json%0A%20%20%20%20import%20tempfile%0A%20%20%20%20import%20time%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20torch%0A%20%20%20%20import%20torch.nn%20as%20nn%0A%20%20%20%20import%20torch.nn.functional%20as%20F%0A%0A%20%20%20%20from%20labkit.show%20import%20show%20%20%23%20a%20function's%20real%20source%2C%20linked%20to%20the%20pinned%20commit%0A%0A%20%20%20%20WORK%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22nb06_%22))%0A%20%20%20%20torch.manual_seed(0)%0A%20%20%20%20torch.set_num_threads(4)%0A%0A%20%20%20%20print(%22torch%22%2C%20torch.__version__%2C%20%22%7C%20numpy%22%2C%20np.__version__)%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20from%20maize_subannual%20import%20model%20as%20maize_model%0A%20%20%20%20from%20maize_subannual.backbone.pixel_classification_head%20import%20PixelClassificationHead%0A%20%20%20%20from%20maize_subannual.backbone.temporal_transformer%20import%20(%0A%20%20%20%20%20%20%20%20BandNorm%2C%0A%20%20%20%20%20%20%20%20TemporalTransformerEncoder%2C%0A%20%20%20%20%20%20%20%20Time2Vec%2C%0A%20%20%20%20)%0A%20%20%20%20from%20sklearn.ensemble%20import%20HistGradientBoostingClassifier%0A%20%20%20%20from%20sklearn.linear_model%20import%20LogisticRegression%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%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%20Temporal%20transformers%20for%20crop%20time%20series%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20The%20PyTorch%20building%20blocks%20a%20temporal%20transformer%20is%20made%20of%20(%60nn.Module%60%2C%20%60nn.TransformerEncoderLayer%60%2C%20padding%20masks%2C%20attention%20pooling)%2C%20the%20way%20the%20official%20documentation%20introduces%20them.%0A%20%20%20%202.%20How%20model-foundry's%20production%20%60TemporalTransformerEncoder%60%20turns%20an%20irregular%2C%20cloud-gapped%20stack%20of%20satellite%20observations%20into%20one%20embedding%20per%20pixel%2C%20with%20the%20tensor%20shape%20at%20every%20step.%0A%20%20%20%203.%20Why%20a%20transformer%20suits%20crop%20phenology%2C%20what%20it%20learns%20to%20look%20at%2C%20and%20what%20simpler%20paths%20(fixed%20composites%20plus%20gradient%20boosting)%20would%20give%20you%20instead.%0A%20%20%20%204.%20What%20an%20embedding%20is%2C%20how%20a%20frozen%20foundation%20model%20is%20reused%2C%20and%20why%20drift%20between%20years%20and%20in-sample%20stacked%20features%20mislead.%0A%20%20%20%205.%20How%20a%20segmentation%20network%20is%20trained%20on%20rare%20classes%20with%20a%20masked%20loss%2C%20why%20one%20NaN%20pixel%20can%20silently%20zero%20the%20loss%2C%20and%20how%20tiles%20are%20stitched%20without%20seams.%0A%20%20%20%206.%20How%20to%20learn%20from%20imperfect%20labels%3A%20abstention%2C%20consensus%20pseudo-labels%2C%20trust%20weights%20and%20calibration.%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20%5Bthe%20set-up%20in%20the%20project%20README%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md%23setting-up)%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%203-model-families%2F3.2-temporal-transformers%2Fnotebook.py%60%3B%20marimo%20runs%20the%20cells%20in%20the%20order%20their%20results%20are%20needed%2C%20and%20re-runs%20the%20cells%20that%20depend%20on%20any%20cell%20you%20change.%20Everything%20runs%20offline%20on%20a%20CPU%20in%20about%20seven%20minutes%2C%20most%20of%20it%20training%20small%20models%20on%20three%20random%20seeds.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied%3A%20%60maize-subannual%60%20is%20installed%20from%20model-foundry%20at%20%60f69eef9%60%2C%20and%20every%20%60show(...)%60%20cell%20prints%20the%20real%20source%20with%20a%20permalink%20to%20that%20commit.%20The%20same%20classes%20exist%2C%20identical%2C%20in%20%60crop_classifier_s2%60%20and%20in%20tf-deep-landcover%3B%20%60fr_crop_type%60%20took%20a%20different%20design%20on%20purpose.%20Those%20aren't%20installed%20here%2C%20so%20they%20appear%20as%20quoted%20excerpts%20with%20permalinks.%0A%20%20%20%20%22%22%22)%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%20Part%201%3A%20the%20PyTorch%20building%20blocks%0A%0A%20%20%20%20%23%23%201.1%20%60nn.Module%60%3A%20a%20layer%20is%20a%20class%20with%20parameters%20and%20a%20%60forward%60%0A%0A%20%20%20%20Every%20PyTorch%20model%20is%20an%20%5B%60nn.Module%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.Module.html).%20You%20declare%20its%20parameters%20(or%20sub-modules)%20in%20%60__init__%60%2C%20and%20say%20what%20it%20computes%20in%20%60forward%60.%20Calling%20the%20module%20runs%20%60forward%60%20and%20records%20the%20operations%20so%20gradients%20can%20flow%20back.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_nn_module_layer()%3A%0A%20%20%20%20class%20Scale(nn.Module)%3A%0A%20%20%20%20%20%20%20%20def%20__init__(self%2C%20n_features%3A%20int)%20-%3E%20None%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.weight%20%3D%20nn.Parameter(torch.ones(n_features))%20%20%23%20learnable%0A%20%20%20%20%20%20%20%20%20%20%20%20self.register_buffer(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22offset%22%2C%20torch.zeros(n_features)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%23%20saved%20with%20the%20model%2C%20never%20trained%0A%0A%20%20%20%20%20%20%20%20def%20forward(self%2C%20x%3A%20torch.Tensor)%20-%3E%20torch.Tensor%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20(x%20-%20self.offset)%20*%20self.weight%0A%0A%20%20%20%20layer%20%3D%20Scale(3)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22parameters%3A%22%2C%0A%20%20%20%20%20%20%20%20%5Bname%20for%20name%2C%20_%20in%20layer.named_parameters()%5D%2C%0A%20%20%20%20%20%20%20%20%22%7C%20buffers%3A%22%2C%0A%20%20%20%20%20%20%20%20%5Bname%20for%20name%2C%20_%20in%20layer.named_buffers()%5D%2C%0A%20%20%20%20)%0A%20%20%20%20print(layer(torch.tensor(%5B%5B1.0%2C%202.0%2C%203.0%5D%5D)))%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%20The%20distinction%20between%20a%20**parameter**%20(trained)%20and%20a%20**buffer**%20(stored%2C%20not%20trained)%20matters%20below%3A%20production's%20%60BandNorm%60%20keeps%20its%20band%20means%20and%20standard%20deviations%20as%20buffers%2C%20so%20they%20travel%20inside%20the%20checkpoint%20but%20are%20never%20changed%20by%20training.%0A%0A%20%20%20%20%23%23%201.2%20One%20transformer%20encoder%20layer%0A%0A%20%20%20%20The%20%5B%60nn.TransformerEncoderLayer%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.TransformerEncoderLayer.html)%20docs%20start%20with%20exactly%20this%3A%20a%20batch%20of%20sequences%20of%20vectors%20goes%20in%2C%20a%20batch%20of%20sequences%20of%20the%20same%20shape%20comes%20out.%20Inside%2C%20every%20position%20attends%20to%20every%20other%20position%20(self-attention)%2C%20then%20passes%20through%20a%20small%20feed-forward%20network.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_transformer_encoder_layer()%3A%0A%20%20%20%20encoder_layer%20%3D%20nn.TransformerEncoderLayer(d_model%3D16%2C%20nhead%3D4%2C%20batch_first%3DTrue)%0A%20%20%20%20src%20%3D%20torch.rand(2%2C%205%2C%2016)%20%20%23%20(batch%2C%20sequence%20length%2C%20d_model)%0A%20%20%20%20print(%22in%20%22%2C%20tuple(src.shape)%2C%20%22-%3E%20out%22%2C%20tuple(encoder_layer(src).shape))%0A%20%20%20%20return%20(encoder_layer%2C)%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%20Shape%20in%20equals%20shape%20out%3A%20an%20encoder%20layer%20*rewrites*%20each%20position's%20vector%20in%20the%20light%20of%20all%20the%20others.%20Stacking%20layers%20with%20%5B%60nn.TransformerEncoder%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.TransformerEncoder.html)%20repeats%20that.%0A%0A%20%20%20%20%23%23%201.3%20Padding%20masks%3A%20how%20a%20batch%20holds%20sequences%20of%20different%20lengths%0A%0A%20%20%20%20A%20pixel%20under%20the%20rainy%20season%20might%20have%2020%20clear%20observations%3B%20a%20pixel%20in%20the%20dry%20season%2060.%20To%20batch%20them%2C%20we%20pad%20every%20sequence%20to%20a%20common%20length%20%60T%60%20and%20pass%20%60src_key_padding_mask%60%2C%20where%20**%60True%60%20means%20%22ignore%20this%20position%22**.%20The%20test%20of%20a%20correct%20mask%3A%20changing%20the%20padded%20values%20must%20not%20change%20the%20outputs%20at%20real%20positions.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_padding_masks_batch(encoder_layer)%3A%0A%20%20%20%20encoder_layer.eval()%20%20%23%20no%20dropout%2C%20so%20outputs%20are%20deterministic%0A%20%20%20%20_x%20%3D%20torch.rand(1%2C%205%2C%2016)%0A%20%20%20%20pad%20%3D%20torch.tensor(%5B%5BFalse%2C%20False%2C%20False%2C%20True%2C%20True%5D%5D)%20%20%23%20last%20two%20positions%20are%20padding%0A%0A%20%20%20%20garbage%20%3D%20_x.clone()%0A%20%20%20%20garbage%5B0%2C%203%3A%5D%20%3D%20100.0%20%20%23%20change%20only%20the%20padded%20positions%0A%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20masked_a%20%3D%20encoder_layer(_x%2C%20src_key_padding_mask%3Dpad)%5B0%2C%20%3A3%5D%0A%20%20%20%20%20%20%20%20masked_b%20%3D%20encoder_layer(garbage%2C%20src_key_padding_mask%3Dpad)%5B0%2C%20%3A3%5D%0A%20%20%20%20%20%20%20%20unmasked_b%20%3D%20encoder_layer(garbage)%5B0%2C%20%3A3%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22with%20the%20mask%2C%20real%20positions%20unchanged%20by%20padding%20values%3A%22%2C%0A%20%20%20%20%20%20%20%20torch.allclose(masked_a%2C%20masked_b%2C%20atol%3D1e-6)%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22without%20the%20mask%2C%20they%20change%3A%22%2C%20not%20torch.allclose(masked_a%2C%20unmasked_b%2C%20atol%3D1e-3))%0A%20%20%20%20return%20(pad%2C)%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%201.4%20Attention%20pooling%3A%20one%20learned%20question%2C%20asked%20of%20the%20whole%20sequence%0A%0A%20%20%20%20The%20encoder%20gives%20one%20vector%20per%20observation%2C%20but%20a%20classifier%20wants%20one%20vector%20per%20pixel.%20One%20answer%20is%20to%20average.%20Production%20does%20something%20more%20flexible%3A%20a%20single%20learned%20**query**%20vector%20attends%20over%20the%20sequence%20with%20%5B%60nn.MultiheadAttention%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.MultiheadAttention.html)%2C%20producing%20a%20weighted%20average%20whose%20weights%20the%20model%20learns.%20The%20weights%20are%20a%20softmax%2C%20so%20they%20sum%20to%201%2C%20and%20padded%20positions%20get%20exactly%200.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_attention_pooling_learned(pad)%3A%0A%20%20%20%20torch.manual_seed(0)%20%20%23%20this%20cell's%20own%20draws%2C%20the%20same%20whichever%20cells%20ran%20before%0A%20%20%20%20pool%20%3D%20nn.MultiheadAttention(embed_dim%3D16%2C%20num_heads%3D1%2C%20batch_first%3DTrue)%0A%20%20%20%20query%20%3D%20torch.randn(1%2C%201%2C%2016)%20%20%23%20(batch%2C%20one%20query%2C%20d_model)%0A%20%20%20%20_seq%20%3D%20torch.rand(1%2C%205%2C%2016)%0A%20%20%20%20_pooled%2C%20_weights%20%3D%20pool(query%2C%20_seq%2C%20_seq%2C%20key_padding_mask%3Dpad%2C%20need_weights%3DTrue)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22pooled%22%2C%0A%20%20%20%20%20%20%20%20tuple(_pooled.shape)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20weights%22%2C%0A%20%20%20%20%20%20%20%20_weights%5B0%2C%200%5D.detach().numpy().round(3)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20sum%22%2C%0A%20%20%20%20%20%20%20%20float(_weights.detach().sum())%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%23%20Part%202%3A%20how%20our%20production%20code%20does%20it%0A%0A%20%20%20%20%60maize_subannual%60%20is%20a%20Kenya%20model%20that%20says%2C%20for%20each%2010%20m%20pixel%20and%20any%20day%20of%20the%20season%2C%20whether%20it's%20non-crop%2C%20another%20crop%20or%20maize.%20Its%20encoder%20lives%20in%20%5B%60backbone%2Ftemporal_transformer.py%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2Fsrc%2Fmaize_subannual%2Fbackbone%2Ftemporal_transformer.py)%2C%20and%20the%20model%20is%20assembled%20in%20%60model.py%60%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_production_code()%3A%0A%20%20%20%20show(maize_model.build_model)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22N_BANDS%20%3D%22%2C%0A%20%20%20%20%20%20%20%20maize_model.N_BANDS%2C%0A%20%20%20%20%20%20%20%20%22%7C%20N_STATIC%20%3D%22%2C%0A%20%20%20%20%20%20%20%20maize_model.N_STATIC%2C%0A%20%20%20%20%20%20%20%20%22%7C%20N_CLASSES%20%3D%22%2C%0A%20%20%20%20%20%20%20%20maize_model.N_CLASSES%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22band%20stats%20file%3A%22%2C%20maize_model.BAND_STATS.name)%0A%20%20%20%20print(%22note%3A%22%2C%20json.loads(maize_model.BAND_STATS.read_text())%5B%22note%22%5D)%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%20Each%20**token**%20is%20one%20observation%3A%20a%20Sentinel-2%20image%20(10%20reflectance%20bands)%2C%20a%20Sentinel-1%20image%20(VV%20and%20VH%20backscatter)%20or%20a%205-day%20weather%20summary%20(rain%2C%20temperature%2C%20soil%20moisture%2C%20vapour-pressure%20deficit)%2C%20packed%20into%2016%20measurement%20features%20plus%20three%20sensor%20flags%20saying%20which%20sensor%20it%20came%20from%2C%20for%2019%20features%20in%20total.%20The%20**static**%20features%20are%204%20terrain%20values%20per%20pixel.%0A%0A%20%20%20%20%23%23%202.1%20%60BandNorm%60%3A%20put%20every%20band%20on%20the%20same%20scale%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_bandnorm_put_every()%3A%0A%20%20%20%20show(BandNorm)%0A%20%20%20%20stats%20%3D%20json.loads(maize_model.BAND_STATS.read_text())%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22first%20S2%20band%20mean%2Fstd%3A%22%2C%0A%20%20%20%20%20%20%20%20round(stats%5B%22mean%22%5D%5B0%5D%2C%204)%2C%0A%20%20%20%20%20%20%20%20round(stats%5B%22std%22%5D%5B0%5D%2C%204)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20VV%20backscatter%20mean%2Fstd%20(dB)%3A%22%2C%0A%20%20%20%20%20%20%20%20round(stats%5B%22mean%22%5D%5B10%5D%2C%202)%2C%0A%20%20%20%20%20%20%20%20round(stats%5B%22std%22%5D%5B10%5D%2C%202)%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20s2_1_bandnorm_put_every_2()%3A%0A%20%20%20%20%23%20Production's%20statistics%3A%20what%20a%200%20in%20each%20feature%20becomes%20after%20BandNorm%2C%20clipped%20to%20%C2%B15.%0A%20%20%20%20stats_f19%20%3D%20json.loads(Path(maize_model.BAND_STATS).read_text())%0A%20%20%20%20names%20%3D%20%5Bf%22S2%20band%20%7Bi%20%2B%201%7D%22%20for%20i%20in%20range(10)%5D%20%2B%20%5B%22VV%22%2C%20%22VH%22%2C%20%22rain%22%2C%20%22tas%22%2C%20%22soil%22%2C%20%22vpd%22%5D%0A%20%20%20%20zero_z%20%3D%20(0%20-%20np.array(stats_f19%5B%22mean%22%5D))%20%2F%20np.array(stats_f19%5B%22std%22%5D)%0A%20%20%20%20print(stats_f19%5B%22note%22%5D)%0A%20%20%20%20for%20_name%2C%20z%20in%20zip(names%2C%20zero_z%5B%3A16%5D%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20%7B_name%3A%3E9%7D%3A%20a%200%20becomes%20z%20%3D%20%7Bz%3A%2B6.2f%7D%22%20%2B%20(%22%20%20(clipped%20to%20%C2%B15)%22%20if%20abs(z)%20%3E%205%20else%20%22%22)%0A%20%20%20%20%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**A%20zero%20isn't%20neutral%20after%20%60BandNorm%60.**%20A%20Sentinel-2%20token%20has%20no%20radar%20or%20weather%20values%2C%20so%20those%20cells%20are%20filled%20with%200%20before%20normalising%2C%20and%20a%20weather%20token's%20ten%20reflectance%20cells%20likewise.%20%60BandNorm%60%20then%20z-scores%20the%20zeros%20with%20each%20feature's%20own%20statistics%2C%20as%20the%20cell%20above%20prints%20from%20production's%20%60band_stats_mm_f19.json%60%3A%20an%20empty%20VV%20cell%20becomes%20%2B3.17%2C%20VH%20%2B4.75%2C%20soil%20moisture%20%E2%88%924.05%2C%20and%20temperature%20%E2%88%927.22%2C%20clipped%20to%20%E2%88%925.%20So%20every%20non-weather%20token%20reaches%20the%20%C2%B15%20clip%20by%20design%2C%20and%20each%20sensor%20carries%20a%20large%20constant%20signature%20beside%20its%20flag.%20The%20flags%20make%20that%20learnable%2C%20but%20it%20isn't%20%22missing%22%3A%20the%20model%20sees%20a%20value.%20The%20same%20happens%20to%20a%20NaN%20inside%20a%20valid%20scene%2C%20which%20%60forward%60%20turns%20into%200%20and%20then%20into%20%E2%88%92mean%2Fstd%2C%20so%20a%20missing%20VV%20reads%20as%20very%20bright%20backscatter.%0A%0A%20%20%20%20Reflectance%20sits%20near%200.1%3B%20backscatter%20near%20%E2%88%929%20dB%3B%20rainfall%20in%20tens%20of%20millimetres.%20%60BandNorm%60%20subtracts%20each%20feature's%20mean%20and%20divides%20by%20its%20standard%20deviation%2C%20using%20statistics%20**computed%20once%20from%20the%20training%20data%20and%20stored%20as%20buffers**%2C%20then%20clips%20to%20%C2%B15%20so%20a%20single%20wild%20value%20can't%20overflow%2016-bit%20arithmetic%20on%20the%20GPU%20(the%20comment%20on%20line%2023%20says%20so).%0A%0A%20%20%20%20**Why%20not%20%60nn.LayerNorm%60%3F**%20The%20encoder%20falls%20back%20to%20%60nn.LayerNorm(n_input_bands)%60%20when%20no%20stats%20file%20is%20given.%20LayerNorm%20normalises%20*each%20token%20across%20its%20own%20features*%2C%20which%20erases%20absolute%20brightness.%20With%20two%20bands%20it%20is%20catastrophic%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_bandnorm_put_every_3()%3A%0A%20%20%20%20red_nir%20%3D%20torch.tensor(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.05%2C%200.45%5D%2C%20%20%23%20dense%20canopy%3A%20NIR%20far%20above%20red%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.10%2C%200.30%5D%2C%20%20%23%20sparse%20crop%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.12%2C%200.20%5D%2C%20%20%23%20near-bare%20soil%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.30%2C%200.10%5D%2C%20%20%23%20red%20above%20NIR%20(water%2C%20or%20a%20burnt%20field)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.20%2C%200.20%5D%2C%20%20%23%20equal%20bands%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.200%2C%200.210%5D%2C%20%20%23%20a%200.01%20difference%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0.200%2C%200.201%5D%2C%20%20%23%20a%200.001%20difference%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20layer_norm%20%3D%20nn.LayerNorm(2)%0A%20%20%20%20print(layer_norm(red_nir).detach().round(decimals%3D3))%0A%20%20%20%20_d%20%3D%20red_nir%5B%3A%2C%201%5D%20-%20red_nir%5B%3A%2C%200%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%7Cd%7C%20%2F%20sqrt(d%C2%B2%20%2B%204%C2%B7eps)%3A%22%2C%0A%20%20%20%20%20%20%20%20(_d.abs()%20%2F%20torch.sqrt(_d**2%20%2B%204%20*%20layer_norm.eps)).round(decimals%3D3)%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%20Look%20at%20what%20survives.%20NIR%20above%20red%20gives%20about%20(%E2%88%921%2C%20%2B1)%2C%20red%20above%20NIR%20about%20(%2B1%2C%20%E2%88%921)%2C%20and%20equal%20bands%20(0%2C%200).%20The%20common%20brightness%20is%20removed%2C%20and%20the%20difference%20d%20%3D%20NIR%20%E2%88%92%20red%20is%20**compressed**%2C%20not%20kept%3A%20for%20two%20bands%20each%20output%20is%20%C2%B1%7Cd%7C%20%2F%20%E2%88%9A(d%C2%B2%20%2B%204%C2%B7eps)%2C%20as%20the%20second%20printed%20line%20confirms.%20With%20PyTorch's%20default%20eps%20of%2010%E2%81%BB%E2%81%B5%20that%20saturates%20quickly.%200.08%20apart%20already%20gives%20%C2%B10.997%20and%200.01%20gives%20%C2%B10.845%3B%20only%20differences%20near%202%E2%88%9Aeps%20%E2%89%88%200.006%20stay%20well%20apart%20(0.001%20gives%20%C2%B10.156).%20Real%20vegetation%20differences%20of%200.1%20or%20more%20all%20land%20within%20a%20hair%20of%20%C2%B11%2C%20so%20the%20dense%20canopy%2C%20the%20sparse%20crop%20and%20the%20near-bare%20soil%20become%20nearly%20identical%20tokens.%20Part%203%20trains%20the%20model%20this%20way%2C%20live%3A%20its%20loss%20never%20leaves%20ln%203%20%E2%89%88%201.10%2C%20the%20loss%20of%20a%20model%20guessing%20one%20of%20three%20classes%20at%20random.%20With%20production's%2019%20features%20the%20fallback%20fails%20differently%3A%20each%20token's%20statistics%20are%20then%20dominated%20by%20temperature%20(about%2020)%20and%20rain%20(tens%20of%20millimetres)%2C%20which%20squash%20the%20reflectances%20instead.%20%60BandNorm%60%20normalises%20each%20feature%20*across%20the%20data*%20instead%2C%20so%20a%20bright%20pixel%20stays%20brighter%20than%20a%20dark%20one.%0A%0A%20%20%20%20%23%23%202.2%20%60Time2Vec%60%3A%20telling%20the%20model%20*when*%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_time2vec_telling_model()%3A%0A%20%20%20%20show(Time2Vec)%0A%20%20%20%20t2v%20%3D%20Time2Vec(out_dim%3D32)%0A%20%20%20%20days_back%20%3D%20torch.tensor(%5B%5B0.0%2C%2030.0%2C%20182.625%2C%20365.25%5D%5D)%0A%20%20%20%20encoded%20%3D%20t2v(days_back)%0A%20%20%20%20print(%22in%22%2C%20tuple(days_back.shape)%2C%20%22-%3E%20out%22%2C%20tuple(encoded.shape))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22periods%20of%20the%20first%204%20periodic%20features%20(days)%3A%22%2C%0A%20%20%20%20%20%20%20%20(2%20*%20np.pi%20%2F%20t2v.periodic_w%5B%3A4%5D.detach().numpy()).round(2)%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22day%200%20vs%20day%20365.25%2C%20periodic%20part%20identical%3A%22%2C%0A%20%20%20%20%20%20%20%20torch.allclose(encoded%5B0%2C%200%2C%201%3A%5D%2C%20encoded%5B0%2C%203%2C%201%3A%5D%2C%20atol%3D1e-4)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20linear%20part%3A%22%2C%0A%20%20%20%20%20%20%20%20round(float(encoded%5B0%2C%200%2C%200%5D.detach())%2C%203)%2C%0A%20%20%20%20%20%20%20%20%22vs%22%2C%0A%20%20%20%20%20%20%20%20round(float(encoded%5B0%2C%203%2C%200%5D.detach())%2C%203)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(t2v%2C)%0A%0A%0A%40app.cell%0Adef%20s2_2_time2vec_telling_model_2(t2v)%3A%0A%20%20%20%20days%20%3D%20torch.arange(0%2C%20400%2C%20dtype%3Dtorch.float32).unsqueeze(0)%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20feats%20%3D%20t2v(days)%5B0%5D.numpy()%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.2))%0A%20%20%20%20_ax.plot(days%5B0%5D%2C%20feats%5B%3A%2C%200%5D%2C%20%22k%22%2C%20lw%3D2%2C%20label%3D%22linear%20term%22)%0A%20%20%20%20for%20k%20in%20(1%2C%202%2C%204)%3A%0A%20%20%20%20%20%20%20%20_ax.plot(days%5B0%5D%2C%20feats%5B%3A%2C%20k%5D%2C%20label%3Df%22sin%20harmonic%20%7Bk%7D%20(period%20%7B365.25%20%2F%20k%3A.0f%7D%20d)%22)%0A%20%20%20%20_ax.set_xlabel(%22days%20back%20from%20the%20prediction%20date%22)%0A%20%20%20%20_ax.set_ylabel(%22feature%20value%22)%0A%20%20%20%20_ax.legend(loc%3D%22lower%20left%22%2C%20fontsize%3D8)%0A%20%20%20%20_ax.set_title(%22Time2Vec%20at%20initialisation%22)%0A%20%20%20%20plt.show()%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%5BTime2Vec%5D(https%3A%2F%2Farxiv.org%2Fabs%2F1907.05321)%20encodes%20a%20real%20number%20%60t%60%20as%20one%20linear%20term%20plus%20%60k%60%20sine%20waves%3A%20%60%5Bw0%C2%B7t%20%2B%20b0%2C%20sin(w1%C2%B7t%20%2B%20b1)%2C%20%E2%80%A6%2C%20sin(wk%C2%B7t%20%2B%20bk)%5D%60.%20Production%20initialises%20the%20sine%20frequencies%20as%20the%20**harmonics%20of%20one%20year**%20(periods%20365.25%2C%20182.6%2C%20121.8%2C%2091.3%20days%20%E2%80%A6)%2C%20so%20day%200%20and%20day%20365.25%20get%20identical%20periodic%20features%3A%20the%20encoding%20knows%20about%20seasons.%20All%20the%20%60w%60%20and%20%60b%60%20are%20%60nn.Parameter%60s%2C%20so%20training%20can%20move%20them.%0A%0A%20%20%20%20Note%20what%20%60t%60%20is%3A%20**%60days_back%60**%2C%20the%20number%20of%20days%20between%20the%20observation%20and%20the%20prediction%20date%2C%20not%20the%20calendar%20date.%20The%20comment%20on%20line%20110%20explains%20why%20there%20is%20no%20year%20embedding%3A%20a%20held-out%20year%20would%20be%20an%20index%20the%20model%20has%20never%20seen.%0A%0A%20%20%20%20%23%23%202.3%20The%20encoder%2C%20one%20step%20at%20a%20time%0A%0A%20%20%20%20Here%20is%20the%20whole%20class%2C%20then%20the%20production%20geometry%20instantiated%20and%20run%20on%20a%20toy%20batch.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_encoder_step_time()%3A%0A%20%20%20%20show(TemporalTransformerEncoder)%0A%20%20%20%20production%20%3D%20maize_model.build_model(dropout%3D0.0).eval()%0A%20%20%20%20print(f%22production%20model%20parameters%3A%20%7Bsum(p.numel()%20for%20p%20in%20production.parameters())%3A%2C%7D%22)%0A%20%20%20%20return%20(production%2C)%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%20Now%20the%20same%20%60forward%60%2C%20line%20by%20line%2C%20on%20a%20batch%20of%202%20pixels%20with%206%20token%20slots%20each.%20Pixel%200%20has%204%20real%20observations%20and%202%20padded%20slots%3B%20pixel%201%20has%206.%20We%20print%20every%20shape%2C%20then%20check%20the%20hand-run%20result%20equals%20the%20module's%20own%20output.%20The%20model%20is%20**untrained**%2C%20so%20its%20pooling%20weights%20are%20close%20to%20an%20even%20split%3B%20that's%20expected%20at%20initialisation.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_encoder_step_time_2(production)%3A%0A%20%20%20%20torch.manual_seed(0)%20%20%23%20this%20cell's%20own%20draws%2C%20the%20same%20whichever%20cells%20ran%20before%0A%20%20%20%20B%2C%20T%20%3D%202%2C%206%0A%20%20%20%20bands%20%3D%20torch.rand(B%2C%20T%2C%20maize_model.N_BANDS)%0A%20%20%20%20days_back_1%20%3D%20torch.randint(0%2C%20365%2C%20(B%2C%20T))%0A%20%20%20%20static%20%3D%20torch.randn(B%2C%20maize_model.N_STATIC)%0A%20%20%20%20attn_mask%20%3D%20torch.tensor(%5B%5B1%2C%201%2C%201%2C%201%2C%200%2C%200%5D%2C%20%5B1%2C%201%2C%201%2C%201%2C%201%2C%201%5D%5D%2C%20dtype%3Dtorch.bool)%0A%20%20%20%20enc%20%3D%20production%5B%22enc%22%5D%0A%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20reference%20%3D%20enc(bands%3Dbands%2C%20days_back%3Ddays_back_1%2C%20static%3Dstatic%2C%20attn_mask%3Dattn_mask)%0A%0A%20%20%20%20%20%20%20%20_x%20%3D%20enc.input_norm(torch.nan_to_num(bands))%20*%20attn_mask.unsqueeze(-1)%0A%20%20%20%20%20%20%20%20print(%221%20normalised%2C%20padding%20zeroed%20%20%22%2C%20tuple(_x.shape))%0A%20%20%20%20%20%20%20%20band_emb%20%3D%20enc.band_proj(_x)%0A%20%20%20%20%20%20%20%20t2v_emb%20%3D%20enc.time2vec(days_back_1)%0A%20%20%20%20%20%20%20%20print(%222%20band%20embedding%20%2B%20Time2Vec%20%20%20%20%22%2C%20tuple(band_emb.shape)%2C%20%22%2B%22%2C%20tuple(t2v_emb.shape))%0A%20%20%20%20%20%20%20%20_tokens%20%3D%20enc.fuse_proj(torch.cat(%5Bband_emb%2C%20t2v_emb%5D%2C%20dim%3D-1))%0A%20%20%20%20%20%20%20%20print(%223%20fused%20tokens%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2C%20tuple(_tokens.shape))%0A%20%20%20%20%20%20%20%20_static_token%20%3D%20(enc.static_proj(static)%20%2B%20enc.static_bias).unsqueeze(1)%0A%20%20%20%20%20%20%20%20_seq%20%3D%20torch.cat(%5B_static_token%2C%20_tokens%5D%2C%20dim%3D1)%0A%20%20%20%20%20%20%20%20print(%224%20static%20token%20prepended%20%20%20%20%20%20%20%22%2C%20tuple(_seq.shape))%0A%20%20%20%20%20%20%20%20_key_padding%20%3D%20~torch.cat(%5Btorch.ones(B%2C%201%2C%20dtype%3Dtorch.bool)%2C%20attn_mask%5D%2C%20dim%3D1)%0A%20%20%20%20%20%20%20%20_out%20%3D%20enc.out_norm(enc.encoder(_seq%2C%20src_key_padding_mask%3D_key_padding))%0A%20%20%20%20%20%20%20%20print(%225%20after%204%20encoder%20layers%20%20%20%20%20%20%20%22%2C%20tuple(_out.shape))%0A%20%20%20%20%20%20%20%20_pooled%2C%20_weights%20%3D%20enc.pool_attn(%0A%20%20%20%20%20%20%20%20%20%20%20%20enc.pool_query.expand(B%2C%20-1%2C%20-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20key_padding_mask%3D_key_padding%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20need_weights%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(%226%20attention-pooled%20embedding%20%20%20%22%2C%20tuple(_pooled.squeeze(1).shape))%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20pooling%20weights%2C%20pixel%200%20%20%20%20%20%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_weights%5B0%2C%200%5D.numpy().round(3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22(static%2C%20obs%201-4%2C%202%20padded)%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_logits%20%3D%20production%5B%22head%22%5D(_pooled.squeeze(1))%0A%20%20%20%20%20%20%20%20print(%227%20head%20logits%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2C%20tuple(_logits.shape))%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22hand-run%20forward%20equals%20the%20module%3A%22%2C%0A%20%20%20%20%20%20%20%20torch.allclose(_pooled.squeeze(1)%2C%20reference%2C%20atol%3D1e-6)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20attn_mask%2C%20bands%2C%20days_back_1%2C%20enc%2C%20reference%2C%20static%0A%0A%0A%40app.cell%0Adef%20s2_3_encoder_step_time_3(%0A%20%20%20%20attn_mask%2C%0A%20%20%20%20bands%2C%0A%20%20%20%20days_back_1%2C%0A%20%20%20%20enc%2C%0A%20%20%20%20reference%2C%0A%20%20%20%20static%2C%0A)%3A%0A%20%20%20%20%23%20Reorder%20pixel%200's%20four%20real%20tokens%2C%20with%20their%20days_back%3B%20the%20padding%20stays%20where%20it%20is.%0A%20%20%20%20order%20%3D%20torch.tensor(%5B3%2C%201%2C%200%2C%202%2C%204%2C%205%5D)%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20reordered%20%3D%20enc(%0A%20%20%20%20%20%20%20%20%20%20%20%20bands%3Dbands%5B%3A%2C%20order%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20days_back%3Ddays_back_1%5B%3A%2C%20order%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20static%3Dstatic%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20attn_mask%3Dattn_mask%5B%3A%2C%20order%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22same%20embedding%20after%20reordering%20the%20tokens%3A%22%2C%0A%20%20%20%20%20%20%20%20torch.allclose(reordered%2C%20reference%2C%20atol%3D1e-5)%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%20Three%20details%20worth%20noticing%3A%0A%0A%20%20%20%20-%20**The%20static%20token%20is%20never%20masked.**%20It%20sits%20at%20position%200%20for%20every%20pixel%2C%20so%20even%20a%20pixel%20whose%20every%20observation%20is%20padding%20still%20has%20one%20valid%20token%2C%20and%20attention%20never%20divides%20by%20zero.%20%60fr_crop_type%60%20solves%20the%20same%20problem%20with%20an%20explicit%20guard%20instead%20(quoted%20in%20%60code.md%60).%0A%20%20%20%20-%20**Padding%20gets%20exactly%20zero%20pooling%20weight**%2C%20as%20in%20section%201.4%2C%20so%20the%20embedding%20depends%20only%20on%20what%20was%20actually%20observed.%0A%20%20%20%20-%20**Order%20doesn't%20matter.**%20Reordering%20a%20pixel's%20tokens%2C%20each%20with%20its%20own%20%60days_back%60%2C%20leaves%20the%20embedding%20unchanged%3A%20self-attention%20is%20permutation-equivariant%2C%20and%20the%20pooling%20then%20sums.%20That's%20why%20production%20can%20list%20tokens%20sensor%20by%20sensor%2C%20not%20by%20date%20(%60dataset.py%60%20L112-126)%3A%20time%20enters%20only%20through%20%60days_back%60.%0A%0A%20%20%20%20%23%20Part%203%3A%20the%20science%2C%20on%20synthetic%20Kenyan-style%20pixels%0A%0A%20%20%20%20We%20don't%20have%20the%20Kenya%20training%20data%20here%2C%20so%20we%20build%20a%20realistic%20stand-in%3A%20three%20classes%20of%20pixel%20whose%20greenness%20follows%20different%20calendars%2C%20observed%20every%205%20days%20by%20a%20sensor%20that%20clouds%20out%20far%20more%20often%20in%20the%20rainy%20season.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_science_synthetic_kenyan()%3A%0A%20%20%20%20from%20types%20import%20SimpleNamespace%0A%0A%20%20%20%20CLASSES%20%3D%20(%22non-crop%22%2C%20%22other%20crop%22%2C%20%22maize%22)%0A%0A%20%20%20%20def%20greenness(doy%2C%20kind)%3A%0A%20%20%20%20%20%20%20%20%23%20A%20smooth%20seasonal%20curve%20per%20class%3A%20flat%2C%20early%20and%20short%2C%20or%20later%2C%20longer%20and%20greener.%0A%20%20%20%20%20%20%20%20if%20kind%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200.25%20%2B%200.03%20*%20np.sin(2%20*%20np.pi%20*%20doy%20%2F%20365)%0A%20%20%20%20%20%20%20%20centre%2C%20width%2C%20amplitude%20%3D%20(150%2C%2025%2C%200.5)%20if%20kind%20%3D%3D%201%20else%20(205%2C%2035%2C%200.6)%0A%20%20%20%20%20%20%20%20return%200.2%20%2B%20amplitude%20*%20np.exp(-0.5%20*%20((doy%20-%20centre)%20%2F%20width)%20**%202)%0A%0A%20%20%20%20def%20simulate_pixel(kind%2C%20rng)%3A%0A%20%20%20%20%20%20%20%20%23%20Observe%20every%205%20days%2C%20drop%20cloudy%20dates%20(cloudier%20around%20day%20120)%2C%20return%20(doys%2C%20%5Bred%2C%20nir%5D).%0A%20%20%20%20%20%20%20%20doys%20%3D%20np.arange(rng.integers(0%2C%205)%2C%20365%2C%205)%0A%20%20%20%20%20%20%20%20cloudy%20%3D%20rng.random(len(doys))%20%3C%200.35%20%2B%200.3%20*%20np.exp(-0.5%20*%20((doys%20-%20120)%20%2F%2040)%20**%202)%0A%20%20%20%20%20%20%20%20doys%20%3D%20doys%5B~cloudy%5D%0A%20%20%20%20%20%20%20%20g%20%3D%20greenness(doys%2C%20kind)%20%2B%20rng.normal(0%2C%200.03%2C%20len(doys))%0A%20%20%20%20%20%20%20%20nir%20%3D%200.18%20%2B%200.35%20*%20g%20%2B%20rng.normal(0%2C%200.01%2C%20len(doys))%0A%20%20%20%20%20%20%20%20red%20%3D%200.14%20-%200.12%20*%20g%20%2B%20rng.normal(0%2C%200.01%2C%20len(doys))%0A%20%20%20%20%20%20%20%20return%20doys%2C%20np.stack(%5Bred%2C%20nir%5D%2C%20axis%3D1).astype(np.float32)%0A%0A%20%20%20%20N%20%3D%203000%0A%0A%20%20%20%20def%20make_data(seed)%3A%0A%20%20%20%20%20%20%20%20%23%20One%20synthetic%20draw%3A%20class%20labels%2C%20then%20each%20pixel's%20cloud-gapped%20observations.%0A%20%20%20%20%20%20%20%20rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20kinds%20%3D%20rng.integers(0%2C%203%2C%20N)%0A%20%20%20%20%20%20%20%20return%20%5Bsimulate_pixel(k%2C%20rng)%20for%20k%20in%20kinds%5D%2C%20kinds%0A%0A%20%20%20%20%23%20The%20active%20seed's%20data.%20use_data()%20swaps%20in%20another%20seed's%20draw%20by%20changing%20these%0A%20%20%20%20%23%20attributes%3A%20marimo%20lets%20only%20one%20cell%20define%20a%20name%2C%20so%20the%20names%20themselves%20can't%20be%20reassigned.%0A%20%20%20%20draw%20%3D%20SimpleNamespace()%0A%20%20%20%20draw.pixels%2C%20draw.kinds%20%3D%20make_data(0)%0A%20%20%20%20train_idx%2C%20test_idx%20%3D%20np.arange(0%2C%202400)%2C%20np.arange(2400%2C%20N)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22clear%20observations%20per%20pixel%3A%20median%22%2C%0A%20%20%20%20%20%20%20%20int(np.median(%5Blen(d)%20for%20d%2C%20_%20in%20draw.pixels%5D))%2C%0A%20%20%20%20%20%20%20%20%22of%2073%20possible%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.2))%0A%20%20%20%20for%20kind%2C%20_colour%20in%20zip(range(3)%2C%20(%22grey%22%2C%20%22tab%3Aorange%22%2C%20%22tab%3Agreen%22)%2C%20strict%3DFalse)%3A%0A%20%20%20%20%20%20%20%20doys%2C%20_x%20%3D%20draw.pixels%5Bint(np.flatnonzero(draw.kinds%20%3D%3D%20kind)%5B0%5D)%5D%0A%20%20%20%20%20%20%20%20ndvi%20%3D%20(_x%5B%3A%2C%201%5D%20-%20_x%5B%3A%2C%200%5D)%20%2F%20(_x%5B%3A%2C%201%5D%20%2B%20_x%5B%3A%2C%200%5D)%0A%20%20%20%20%20%20%20%20_ax.plot(doys%2C%20ndvi%2C%20%22o-%22%2C%20ms%3D3%2C%20color%3D_colour%2C%20label%3DCLASSES%5Bkind%5D)%0A%20%20%20%20_ax.set_xlabel(%22day%20of%20year%22)%0A%20%20%20%20_ax.set_ylabel(%22NDVI%22)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20_ax.set_title(%22One%20simulated%20pixel%20per%20class%3A%20irregular%2C%20cloud-gapped%20observations%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20CLASSES%2C%20draw%2C%20greenness%2C%20make_data%2C%20test_idx%2C%20train_idx%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%20The%20model%20must%20decide%20the%20class%20**part-way%20through%20the%20season**%2C%20using%20only%20observations%20up%20to%20a%20prediction%20day%2C%20exactly%20like%20%60maize_subannual%60's%20%60days_back%60%20window%20(%5Bdataset.py%3A95-144%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2Fsrc%2Fmaize_subannual%2Fdataset.py%23L95-L144)).%20This%20function%20builds%20those%20padded%20batches%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_science_synthetic_kenyan_2(draw%2C%20make_data%2C%20train_idx)%3A%0A%20%20%20%20T_MAX%20%3D%2080%0A%20%20%20%20PRED_DOYS%20%3D%20np.array(%5B120%2C%20150%2C%20180%2C%20210%2C%20240%2C%20270%2C%20300%2C%20330%2C%20365%5D)%0A%0A%20%20%20%20def%20make_batch(idx%2C%20pred_doys%2C%20zero_time%3DFalse)%3A%0A%20%20%20%20%20%20%20%20%23%20Tokens%20are%20observations%20on%20or%20before%20each%20pixel's%20prediction%20day%3B%20days_back%20%3D%20pred_doy%20-%20doy.%0A%20%20%20%20%20%20%20%20bands%20%3D%20np.zeros((len(idx)%2C%20T_MAX%2C%202)%2C%20np.float32)%0A%20%20%20%20%20%20%20%20days_back%20%3D%20np.zeros((len(idx)%2C%20T_MAX)%2C%20np.int64)%0A%20%20%20%20%20%20%20%20mask%20%3D%20np.zeros((len(idx)%2C%20T_MAX)%2C%20bool)%0A%20%20%20%20%20%20%20%20for%20row%2C%20(i%2C%20pred)%20in%20enumerate(zip(idx%2C%20pred_doys%2C%20strict%3DFalse))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20doys%2C%20x%20%3D%20draw.pixels%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20keep%20%3D%20doys%20%3C%3D%20pred%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20min(int(keep.sum())%2C%20T_MAX)%0A%20%20%20%20%20%20%20%20%20%20%20%20bands%5Brow%2C%20%3An%5D%20%3D%20x%5Bkeep%5D%5B%3An%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20days_back%5Brow%2C%20%3An%5D%20%3D%200%20if%20zero_time%20else%20(pred%20-%20doys%5Bkeep%5D)%5B%3An%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20mask%5Brow%2C%20%3An%5D%20%3D%20True%0A%20%20%20%20%20%20%20%20return%20torch.from_numpy(bands)%2C%20torch.from_numpy(days_back)%2C%20torch.from_numpy(mask)%0A%0A%20%20%20%20def%20use_data(seed)%3A%0A%20%20%20%20%20%20%20%20%23%20Make%20one%20seed's%20draw%20the%20data%20every%20helper%20reads%2C%20with%20band%20stats%20from%20its%20own%20training%20pixels.%0A%20%20%20%20%20%20%20%20%23%20Statistics%20from%20the%20test%20pixels%20too%20would%20leak%20them%20into%20the%20normalisation%20(production's%20F13).%0A%20%20%20%20%20%20%20%20draw.pixels%2C%20draw.kinds%20%3D%20make_data(seed)%0A%20%20%20%20%20%20%20%20draw.stats_path%20%3D%20WORK%20%2F%20f%22band_stats_%7Bseed%7D.json%22%0A%20%20%20%20%20%20%20%20train_obs%20%3D%20np.concatenate(%5Bdraw.pixels%5Bi%5D%5B1%5D%20for%20i%20in%20train_idx%5D)%0A%20%20%20%20%20%20%20%20draw.stats_path.write_text(%0A%20%20%20%20%20%20%20%20%20%20%20%20json.dumps(%7B%22mean%22%3A%20train_obs.mean(0).tolist()%2C%20%22std%22%3A%20train_obs.std(0).tolist()%7D)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20use_data(0)%0A%20%20%20%20print(%22band%20stats%20from%20the%20training%20pixels%3A%22%2C%20json.loads(draw.stats_path.read_text()))%0A%20%20%20%20return%20PRED_DOYS%2C%20T_MAX%2C%20make_batch%2C%20use_data%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%20Now%20train%20the%20**production%20classes**%20at%20a%20small%20size%20(d_model%2032%2C%202%20layers)%2C%20with%20%60BandNorm%60%20fitted%20to%20the%20training%20pixels%2C%20for%2012%20epochs%20on%20the%20CPU%2C%20with%20production's%20one-cycle%20learning-rate%20schedule%3A%20a%20warm-up%20over%20the%20first%2010%20%25%20of%20steps%2C%20then%20cosine%20annealing%2C%20with%20Adam's%20momentum%20cycled%20the%20opposite%20way%20(%5B%60maize_subannual%2Ftrain.py%60%20L122-127%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2Fsrc%2Fmaize_subannual%2Ftrain.py%23L122-L127)).%20Section%204.5%20shows%20what%20the%20schedule%20buys.%20The%20loss%20is%20unweighted%20cross-entropy%20because%20the%20synthetic%20classes%20are%20balanced%3B%20production%20weights%20both%20the%20classes%20(1%2C%204%2C%203)%20and%20each%20sample%2C%20and%20divides%20by%20the%20summed%20sample%20weights%20(%5BL154-157%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2Fsrc%2Fmaize_subannual%2Ftrain.py%23L154-L157)).%20Its%20head%20also%20offers%20a%20focal%20loss%2C%20which%20neither%20production%20model%20trains%20with%20(%60code.md%60%20section%207).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_science_synthetic_kenyan_3(%0A%20%20%20%20PRED_DOYS%2C%0A%20%20%20%20draw%2C%0A%20%20%20%20make_batch%2C%0A%20%20%20%20test_idx%2C%0A%20%20%20%20train_idx%2C%0A)%3A%0A%20%20%20%20def%20train(%0A%20%20%20%20%20%20%20%20zero_time%3DFalse%2C%0A%20%20%20%20%20%20%20%20epochs%3D12%2C%0A%20%20%20%20%20%20%20%20seed%3D0%2C%0A%20%20%20%20%20%20%20%20use_date%3DFalse%2C%0A%20%20%20%20%20%20%20%20verbose%3DTrue%2C%0A%20%20%20%20%20%20%20%20one_cycle%3DTrue%2C%0A%20%20%20%20%20%20%20%20band_stats%3DTrue%2C%0A%20%20%20%20%20%20%20%20train_on%3DNone%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20torch.manual_seed(seed)%0A%20%20%20%20%20%20%20%20train_on%20%3D%20train_idx%20if%20train_on%20is%20None%20else%20train_on%20%20%23%20the%20pixels%20to%20train%20on%0A%20%20%20%20%20%20%20%20batch_rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20model%20%3D%20nn.ModuleDict(%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%22enc%22%3A%20TemporalTransformerEncoder(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n_input_bands%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20d_model%3D32%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n_layers%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n_heads%3D4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ff_dim%3D64%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dropout%3D0.1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20time2vec_dim%3D8%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n_static_features%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20band_stats_path%3Dstr(draw.stats_path)%20if%20band_stats%20else%20None%2C%0A%20%20%20%20%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%20%20%20%20%20%22head%22%3A%20PixelClassificationHead(d_model%3D32%2C%20n_classes%3D3%2C%20dropout%3D0.1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20optimiser%20%3D%20torch.optim.AdamW(model.parameters()%2C%20lr%3D3e-3)%0A%20%20%20%20%20%20%20%20batches%20%3D%20-(-len(train_on)%20%2F%2F%2064)%0A%20%20%20%20%20%20%20%20%23%20Production's%20one-cycle%20schedule%20(maize_subannual%20train.py%20L125-127)%3A%20a%20warm-up%20over%20the%20first%2010%20%25%20of%20steps%2C%0A%20%20%20%20%20%20%20%20%23%20cosine%20annealing%2C%20and%20Adam's%20beta1%20cycled%20between%200.95%20and%200.85%20(OneCycleLR's%20defaults).%0A%20%20%20%20%20%20%20%20schedule%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20torch.optim.lr_scheduler.OneCycleLR(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20optimiser%2C%20max_lr%3D3e-3%2C%20total_steps%3Depochs%20*%20batches%2C%20pct_start%3D0.1%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20one_cycle%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20None%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20epoch%20in%20range(epochs)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20model.train()%0A%20%20%20%20%20%20%20%20%20%20%20%20order%20%3D%20batch_rng.permutation(train_on)%0A%20%20%20%20%20%20%20%20%20%20%20%20total%20%3D%200.0%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20start%20in%20range(0%2C%20len(order)%2C%2064)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20idx%20%3D%20order%5Bstart%20%3A%20start%20%2B%2064%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20preds%20%3D%20batch_rng.choice(PRED_DOYS%2C%20len(idx))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bands%2C%20days_back%2C%20mask%20%3D%20make_batch(idx%2C%20preds%2C%20zero_time)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20static%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20torch.from_numpy(preds%20%2F%20365).float().unsqueeze(1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20use_date%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20torch.zeros(len(idx)%2C%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20embedding%20%3D%20model%5B%22enc%22%5D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bands%3Dbands%2C%20days_back%3Ddays_back%2C%20static%3Dstatic%2C%20attn_mask%3Dmask%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20loss%20%3D%20F.cross_entropy(model%5B%22head%22%5D(embedding)%2C%20torch.from_numpy(draw.kinds%5Bidx%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20optimiser.zero_grad()%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%20optimiser.step()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20schedule%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20schedule.step()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20total%20%2B%3D%20loss.item()%20*%20len(idx)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20verbose%20and%20epoch%20in%20(0%2C%203%2C%20epochs%20-%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%20%20epoch%20%7Bepoch%3A2d%7D%20%20mean%20loss%20%7Btotal%20%2F%20len(order)%3A.3f%7D%22)%0A%20%20%20%20%20%20%20%20return%20model.eval()%0A%0A%20%20%20%20def%20accuracy(model%2C%20pred_doy%2C%20zero_time%3DFalse%2C%20ignore_mask%3DFalse%2C%20use_date%3DFalse)%3A%0A%20%20%20%20%20%20%20%20bands%2C%20days_back%2C%20mask%20%3D%20make_batch(test_idx%2C%20np.full(len(test_idx)%2C%20pred_doy)%2C%20zero_time)%0A%20%20%20%20%20%20%20%20if%20ignore_mask%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20mask%20%3D%20torch.ones_like(mask)%0A%20%20%20%20%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20static%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20torch.full((len(test_idx)%2C%201)%2C%20pred_doy%20%2F%20365)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20use_date%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20torch.zeros(len(test_idx)%2C%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20embedding%20%3D%20model%5B%22enc%22%5D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bands%3Dbands%2C%20days_back%3Ddays_back%2C%20static%3Dstatic%2C%20attn_mask%3Dmask%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20float(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(model%5B%22head%22%5D(embedding).argmax(1).numpy()%20%3D%3D%20draw.kinds%5Btest_idx%5D).mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20model%20%3D%20train()%0A%20%20%20%20print(f%22trained%20in%20%7Btime.perf_counter()%20-%20_t0%3A.0f%7D%20s%22)%0A%20%20%20%20EVAL_DOYS%20%3D%20(90%2C%20120%2C%20150%2C%20180%2C%20210%2C%20300)%0A%20%20%20%20transformer_acc%20%3D%20%7Bd%3A%20accuracy(model%2C%20d)%20for%20d%20in%20EVAL_DOYS%7D%0A%20%20%20%20for%20_d%2C%20a%20in%20transformer_acc.items()%3A%0A%20%20%20%20%20%20%20%20print(f%22prediction%20day%20%7B_d%3A3d%7D%3A%20held-out%20accuracy%20%7Ba%3A.3f%7D%22)%0A%20%20%20%20return%20EVAL_DOYS%2C%20accuracy%2C%20model%2C%20train%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%23%20The%20LayerNorm%20fallback%2C%20trained%0A%0A%20%20%20%20Section%202.1%20showed%20what%20the%20LayerNorm%20fallback%20does%20to%20a%20(red%2C%20NIR)%20pair.%20Here%20is%20what%20it%20does%20to%20training%3A%20the%20same%20model%2C%20the%20same%20data%20and%20the%20same%20schedule%2C%20with%20no%20band-stats%20file.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20layernorm_fallback_trained(accuracy%2C%20train)%3A%0A%20%20%20%20fallback%20%3D%20train(band_stats%3DFalse)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22held-out%20accuracy%20on%20day%20300%3A%22%2C%0A%20%20%20%20%20%20%20%20round(accuracy(fallback%2C%20300)%2C%203)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20ln%203%20%3D%22%2C%0A%20%20%20%20%20%20%20%20round(float(np.log(3))%2C%203)%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%20Accuracy%20climbs%20through%20the%20season%2C%20from%200.673%20on%20day%2090%20to%200.975%20on%20day%20150%20and%200.997%20on%20day%20300.%20Day%2090%20is%20earlier%20than%20any%20prediction%20day%20the%20model%20trained%20on%20(they%20start%20at%20day%20120)%2C%20so%20that%20score%20also%20tests%20an%20unseen%20cut-off%2C%20not%20only%20crops%20that%20haven't%20separated.%20Early%20on%2C%20maize%20and%20the%20other%20crop%20haven't%20yet%20separated%2C%20so%20the%20model%20can%20only%20partly%20tell%20them%20apart%3B%20once%20the%20maize%20canopy%20develops%2C%20the%20decision%20is%20easy.%20That%20**in-season%20curve**%20is%20the%20scientific%20product%20of%20a%20sub-annual%20model%2C%20and%20it's%20why%20%60maize_subannual%60%20trains%20on%20randomly%20chosen%20prediction%20days%20(%60DEFAULT_TRAIN_DOYS%60).%0A%0A%20%20%20%20The%20**LayerNorm%20fallback**%20above%2C%20trained%20the%20same%20way%2C%20barely%20moves%3A%20its%20loss%20goes%20from%201.115%20to%201.098%2C%20ln%203%20to%20three%20decimals%2C%20and%20it%20scores%200.352%20on%20day%20300%2C%20chance%20for%20three%20balanced%20classes.%20That's%20section%202.1's%20compression%20at%20work%3A%20every%20vegetated%20token%20looks%20the%20same.%0A%0A%20%20%20%20That%20is%20**one%20run**%3A%20one%20draw%20of%20synthetic%20pixels%20and%20one%20initialisation%20of%20the%20weights.%20Part%204%20repeats%20it%20on%20three%20seeds%20before%20drawing%20any%20conclusion.%0A%0A%20%20%20%20%23%23%203.1%20What%20did%20the%20pooling%20learn%20to%20look%20at%3F%0A%0A%20%20%20%20Rerun%20the%20forward%20pass%20for%20the%20test%20maize%20pixels%20at%20prediction%20day%20300%20and%20read%20the%20attention-pooling%20weights%2C%20then%20average%20them%20by%20calendar%20date.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_did_pooling_learn(draw%2C%20greenness%2C%20make_batch%2C%20model%2C%20test_idx)%3A%0A%20%20%20%20maize_idx%20%3D%20test_idx%5Bdraw.kinds%5Btest_idx%5D%20%3D%3D%202%5D%5B%3A200%5D%0A%20%20%20%20bands_1%2C%20days_back_2%2C%20_mask%20%3D%20make_batch(maize_idx%2C%20np.full(len(maize_idx)%2C%20300))%0A%20%20%20%20enc_1%20%3D%20model%5B%22enc%22%5D%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20_x%20%3D%20enc_1.input_norm(bands_1)%20*%20_mask.unsqueeze(-1)%0A%20%20%20%20%20%20%20%20_tokens%20%3D%20enc_1.fuse_proj(%0A%20%20%20%20%20%20%20%20%20%20%20%20torch.cat(%5Benc_1.band_proj(_x)%2C%20enc_1.time2vec(days_back_2)%5D%2C%20dim%3D-1)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_static_token%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20enc_1.static_proj(torch.zeros(len(maize_idx)%2C%201))%20%2B%20enc_1.static_bias%0A%20%20%20%20%20%20%20%20).unsqueeze(1)%0A%20%20%20%20%20%20%20%20_key_padding%20%3D%20~torch.cat(%5Btorch.ones(len(maize_idx)%2C%201%2C%20dtype%3Dtorch.bool)%2C%20_mask%5D%2C%20dim%3D1)%0A%20%20%20%20%20%20%20%20_out%20%3D%20enc_1.out_norm(%0A%20%20%20%20%20%20%20%20%20%20%20%20enc_1.encoder(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20torch.cat(%5B_static_token%2C%20_tokens%5D%2C%20dim%3D1)%2C%20src_key_padding_mask%3D_key_padding%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_%2C%20_weights%20%3D%20enc_1.pool_attn(%0A%20%20%20%20%20%20%20%20%20%20%20%20enc_1.pool_query.expand(len(maize_idx)%2C%20-1%2C%20-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20key_padding_mask%3D_key_padding%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20need_weights%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20w%20%3D%20_weights%5B%3A%2C%200%2C%201%3A%5D.numpy()%0A%20%20%20%20obs_doy%20%3D%20300%20-%20days_back_2.numpy()%0A%20%20%20%20valid%20%3D%20_mask.numpy()%0A%20%20%20%20bins%20%3D%20%5B(lo%2C%20lo%20%2B%2029)%20for%20lo%20in%20range(0%2C%20270%2C%2030)%5D%20%2B%20%5B%0A%20%20%20%20%20%20%20%20(270%2C%20300)%0A%20%20%20%20%5D%20%20%23%20the%20last%20bin%20includes%20the%20prediction%20day%20itself%0A%20%20%20%20mean_w%20%3D%20%5Bw%5Bvalid%20%26%20(obs_doy%20%3E%3D%20lo)%20%26%20(obs_doy%20%3C%3D%20hi)%5D.mean()%20for%20lo%2C%20hi%20in%20bins%5D%0A%20%20%20%20for%20(lo%2C%20hi)%2C%20mw%20in%20zip(bins%2C%20mean_w%2C%20strict%3DFalse)%3A%0A%20%20%20%20%20%20%20%20print(f%22days%20%7Blo%3A3d%7D-%7Bhi%3A3d%7D%3A%20mean%20pooling%20weight%20per%20observation%20%7Bmw%3A.4f%7D%22)%0A%0A%20%20%20%20%23%20Bin%20means%20can%20hide%20uneven%20weights%20inside%20a%20pixel%2C%20so%20compare%20each%20pixel's%20weights%20with%20an%20even%20split.%0A%20%20%20%20even_share%20%3D%201%20%2F%20(valid.sum(1)%20%2B%201)%20%20%23%20its%20observations%20plus%20the%20static%20token%0A%20%20%20%20ratio%20%3D%20np.where(valid%2C%20w%20%2F%20even_share%5B%3A%2C%20None%5D%2C%20np.nan)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22observation%20weight%20%C3%B7%20the%20pixel's%20even%20share%3A%20median%20%7Bnp.nanmedian(ratio)%3A.2f%7D%2C%20%22%0A%20%20%20%20%20%20%20%20f%225th%E2%80%9395th%20percentile%20%7Bnp.nanpercentile(ratio%2C%205)%3A.2f%7D%E2%80%93%7Bnp.nanpercentile(ratio%2C%2095)%3A.2f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22static%20token%20weight%3A%20mean%20%7B_weights%5B%3A%2C%200%2C%200%5D.mean()%3A.3f%7D%2C%20against%20an%20even%20share%20of%20%7Beven_share.mean()%3A.3f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.2))%0A%20%20%20%20_ax.bar(%0A%20%20%20%20%20%20%20%20%5B(lo%20%2B%20hi)%20%2F%202%20for%20lo%2C%20hi%20in%20bins%5D%2C%0A%20%20%20%20%20%20%20%20mean_w%2C%0A%20%20%20%20%20%20%20%20width%3D26%2C%0A%20%20%20%20%20%20%20%20color%3D%22tab%3Agreen%22%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.7%2C%0A%20%20%20%20%20%20%20%20label%3D%22mean%20pooling%20weight%20per%20observation%22%2C%0A%20%20%20%20)%0A%20%20%20%20ax2%20%3D%20_ax.twinx()%0A%20%20%20%20ax2.plot(np.arange(301)%2C%20greenness(np.arange(301)%2C%202)%2C%20%22k--%22%2C%20label%3D%22maize%20greenness%20curve%22)%0A%20%20%20%20_ax.set_xlabel(%22day%20of%20year%22)%0A%20%20%20%20_ax.legend(loc%3D%22upper%20left%22%2C%20fontsize%3D8)%0A%20%20%20%20ax2.legend(loc%3D%22upper%20right%22%2C%20fontsize%3D8)%0A%20%20%20%20_ax.set_title(%22Where%20the%20trained%20pooling%20looks%2C%20for%20maize%20pixels%20predicted%20on%20day%20300%22)%0A%20%20%20%20plt.show()%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**The%20pooling%20leans%20on%20maize's%20peak.**%20Observations%20from%20days%20180%E2%80%93209%20get%200.076%20each%20and%20days%20210%E2%80%93239%20get%200.060%2C%20against%200.006%20to%200.025%20in%20every%20other%20window%3B%20maize's%20greenness%20peaks%20near%20day%20205.%20Bin%20means%20could%20hide%20uneven%20weights%20inside%20a%20pixel%2C%20so%20the%20cell%20also%20compares%20each%20pixel's%20weights%20with%20an%20even%20split%20over%20its%20tokens%3A%20the%20median%20observation%20gets%200.79%20times%20its%20even%20share%2C%20but%20the%205th%E2%80%9395th%20percentile%20spans%200.06%20to%202.94%2C%20so%20within%20a%20pixel%20some%20observations%20count%20fifty%20times%20more%20than%20others.%20The%20static%20token%2C%20which%20carries%20nothing%20here%2C%20gets%200.005%20against%20an%20even%20share%20of%200.029.%0A%0A%20%20%20%20So%20in%20this%20run%20the%20pooling%20did%20what%20a%20fixed%2016-day%20composite%20can't%3A%20it%20learned%20which%20part%20of%20the%20season%20separates%20the%20classes%20and%20weighted%20those%20observations%20up.%20Treat%20that%20as%20one%20model's%20routing%2C%20not%20as%20an%20explanation%20of%20its%20decision.%20An%20earlier%20version%20of%20this%20notebook%2C%20trained%20with%20a%20constant%20learning%20rate%2C%20learned%20nearly%20flat%20pooling%20weights%20and%20classified%20almost%20as%20well%2C%20so%20the%20discriminating%20can%20also%20happen%20inside%20the%20encoder%20layers.%20Attention%20weights%20show%20where%20a%20model%20*routed*%20information%20on%20one%20training%20run%2C%20not%20*why*%20it%20decided%2C%20and%20how%20far%20they%20can%20be%20trusted%20as%20explanations%20is%20debated%20(%5BJain%20and%20Wallace%2C%202019%5D(https%3A%2F%2Farxiv.org%2Fabs%2F1902.10186)%3B%20%5BWiegreffe%20and%20Pinter%2C%202019%5D(https%3A%2F%2Farxiv.org%2Fabs%2F1908.04626)).%20%22Learned%20compositing%22%20is%20what%20the%20architecture%20*allows*%3B%20whether%20a%20given%20model%20does%20it%20has%20to%20be%20checked%2C%20as%20here.%0A%0A%20%20%20%20%23%20Part%204%3A%20paths%20not%20travelled%2C%20tried%20live%0A%20%20%20%20%22%22%22)%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%204.0%20One%20run%20is%20an%20anecdote%0A%0A%20%20%20%20Everything%20in%20Part%203%20came%20from%20one%20training%20run%20on%20one%20synthetic%20draw.%20A%20different%20draw%20of%20pixels%2C%20or%20a%20different%20initialisation%20of%20the%20weights%2C%20gives%20a%20different%20model%2C%20and%20on%20600%20test%20pixels%20a%20few%20percentage%20points%20can%20move%20by%20chance%20alone.%20So%20every%20comparison%20in%20this%20part%20is%20repeated%20on%20**three%20seeds**%3A%20each%20seed%20draws%20its%20own%20pixels%20and%20initialises%20its%20own%20models%2C%20and%20the%20tables%20show%20the%20mean%20%C2%B1%20standard%20deviation%20across%20the%20three.%20Where%20two%20variants%20are%20compared%2C%20the%20last%20column%20is%20the%20*paired*%20difference%20(both%20scored%20on%20the%20same%20seed's%20test%20pixels)%2C%20listed%20per%20seed%2C%20so%20you%20can%20see%20whether%20a%20gap%20points%20the%20same%20way%20every%20time.%0A%0A%20%20%20%20Three%20seeds%20is%20still%20few.%20Below%2C%20a%20difference%20is%20called%20**consistent**%20when%20it%20has%20the%20same%20sign%20in%20all%20three%20seeds.%20That%20is%20weak%20evidence%2C%20not%20proof%3A%20with%20no%20real%20effect%20and%20either%20sign%20equally%20likely%2C%20three%20seeds%20agree%20by%20chance%20a%20quarter%20of%20the%20time%2C%20and%20this%20part%20makes%20about%20two%20dozen%20day-by-day%20comparisons%2C%20so%20several%20%22consistent%22%20results%20would%20appear%20by%20luck%20alone.%20Two%20more%20yardsticks%20help%3A%0A%0A%20%20%20%20-%20**Sampling%20error.**%20Each%20table%20prints%20the%20binomial%20standard%20error%20of%20one%20accuracy%20on%20600%20test%20pixels%3A%20about%200.002%20near%200.995%20and%200.019%20near%200.65.%20A%20difference%20of%200.003%20at%20day%20300%20is%20two%20test%20pixels.%0A%20%20%20%20-%20**What%20a%20seed%20changes.**%20Each%20seed%20redraws%20the%20data%20*and*%20the%20initialisation%20together%2C%20so%20a%20bad%20run%20can't%20be%20blamed%20on%20either%20alone.%20Fixing%20%60use_data(0)%60%20and%20varying%20only%20%60torch.manual_seed%60%20would%20separate%20them%20(exercise%207).%0A%0A%20%20%20%20Sizes%20are%20illustrative.%0A%0A%20%20%20%20First%2C%20the%20transformer%20itself%2C%20retrained%20on%20seeds%201%20and%202%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_0_run_anecdote(%0A%20%20%20%20EVAL_DOYS%2C%0A%20%20%20%20accuracy%2C%0A%20%20%20%20draw%2C%0A%20%20%20%20model%2C%0A%20%20%20%20test_idx%2C%0A%20%20%20%20train%2C%0A%20%20%20%20use_data%2C%0A)%3A%0A%20%20%20%20SEEDS%20%3D%20(0%2C%201%2C%202)%0A%0A%20%20%20%20def%20over_seeds(run)%3A%0A%20%20%20%20%20%20%20%20%23%20Call%20run(seed)%20with%20each%20seed's%20own%20data%20draw%20active%3B%20restore%20the%20active%20data%20even%20if%20interrupted.%0A%20%20%20%20%20%20%20%20active%20%3D%20(draw.pixels%2C%20draw.kinds%2C%20draw.stats_path)%0A%20%20%20%20%20%20%20%20results%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20seed%20in%20SEEDS%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20use_data(seed)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20results%5Bseed%5D%20%3D%20run(seed)%0A%20%20%20%20%20%20%20%20finally%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20draw.pixels%2C%20draw.kinds%2C%20draw.stats_path%20%3D%20active%0A%20%20%20%20%20%20%20%20return%20results%0A%0A%20%20%20%20def%20difference(a%2C%20b)%3A%0A%20%20%20%20%20%20%20%20%23%20Per-seed%20a%20minus%20b%3A%20both%20are%20scored%20on%20the%20same%20seed's%20test%20pixels%2C%20so%20the%20pairing%20is%20fair.%0A%20%20%20%20%20%20%20%20return%20%7Bs%3A%20%7Bd%3A%20a%5Bs%5D%5Bd%5D%20-%20b%5Bs%5D%5Bd%5D%20for%20d%20in%20a%5Bs%5D%7D%20for%20s%20in%20a%7D%0A%0A%20%20%20%20def%20table(columns%2C%20days%3DEVAL_DOYS)%3A%0A%20%20%20%20%20%20%20%20%23%20Mean%20%C2%B1%20standard%20deviation%20over%20the%20seeds%20per%20column%3B%20the%20last%20column%20also%20lists%20its%20value%20per%20seed.%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22prediction%20day%20%7C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20%7C%20%22.join(f%22%7Bname%3A%3E19%7D%22%20for%20name%20in%20columns)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20%7C%20per%20seed%20(last%20column)%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20d%20in%20days%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20cells%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20runs%20in%20columns.values()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20values%20%3D%20np.array(%5Bruns%5Bs%5D%5Bd%5D%20for%20s%20in%20SEEDS%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cells.append(f%22%7Bvalues.mean()%3A%2B.3f%7D%20%C2%B1%20%7Bvalues.std(ddof%3D1)%3A.3f%7D%22.rjust(19))%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bd%3A14d%7D%20%7C%20%22%20%2B%20%22%20%7C%20%22.join(cells)%20%2B%20%22%20%7C%20%22%20%2B%20%22%2C%20%22.join(f%22%7Bv%3A%2B.3f%7D%22%20for%20v%20in%20values)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20first%20%3D%20np.array(%5B%5Bnext(iter(columns.values()))%5Bs%5D%5Bd%5D%20for%20s%20in%20SEEDS%5D%20for%20d%20in%20days%5D).mean(%0A%20%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20se%20%3D%20np.sqrt(first%20*%20(1%20-%20first)%20%2F%20len(test_idx))%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22one%20accuracy%20on%20%7Blen(test_idx)%7D%20test%20pixels%20has%20a%20binomial%20standard%20error%20of%20%7Bse.min()%3A.3f%7D%20to%20%7Bse.max()%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20TRANSFORMERS%20%3D%20%7B%7D%0A%0A%20%20%20%20def%20run_transformer(seed)%3A%0A%20%20%20%20%20%20%20%20TRANSFORMERS%5Bseed%5D%20%3D%20model%20if%20seed%20%3D%3D%200%20else%20train(seed%3Dseed%2C%20verbose%3DFalse)%0A%20%20%20%20%20%20%20%20return%20%7Bd%3A%20accuracy(TRANSFORMERS%5Bseed%5D%2C%20d)%20for%20d%20in%20EVAL_DOYS%7D%0A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20transformer_runs%20%3D%20over_seeds(run_transformer)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22seeds%201%20and%202%20trained%20in%20%7Btime.perf_counter()%20-%20_t0%3A.0f%7D%20s%20(seed%200%20is%20the%20Part%203%20model)%22%0A%20%20%20%20)%0A%20%20%20%20print(%22prediction%20day%20%7C%20%22%20%2B%20%22%20%7C%20%22.join(f%22seed%20%7Bs%7D%22%20for%20s%20in%20SEEDS)%20%2B%20%22%20%7C%20%20%20mean%20%C2%B1%20sd%22)%0A%20%20%20%20for%20_d%20in%20EVAL_DOYS%3A%0A%20%20%20%20%20%20%20%20values%20%3D%20np.array(%5Btransformer_runs%5Bs%5D%5B_d%5D%20for%20s%20in%20SEEDS%5D)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_d%3A14d%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20%7C%20%22.join(f%22%7Bv%3A6.3f%7D%22%20for%20v%20in%20values)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20f%22%20%7C%20%7Bvalues.mean()%3A.3f%7D%20%C2%B1%20%7Bvalues.std(ddof%3D1)%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20SEEDS%2C%20TRANSFORMERS%2C%20difference%2C%20over_seeds%2C%20table%2C%20transformer_runs%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%20With%20production's%20one-cycle%20schedule%2C%20the%20three%20runs%20agree%20closely%3A%20the%20spread%20across%20seeds%20is%20at%20most%200.016%20on%20any%20day%2C%20and%20no%20run%20collapses.%20Section%204.5%20shows%20that%20this%20stability%20comes%20from%20the%20schedule.%0A%0A%20%20%20%20%23%23%204.1%20Fixed%20composites%20plus%20gradient%20boosting%0A%0A%20%20%20%20The%20classic%20alternative%3A%20summarise%20each%20pixel%20into%2023%20fixed%2016-day%20maximum-NDVI%20composites%20(empty%20composites%20stay%20missing)%2C%20then%20fit%20a%20gradient-boosted%20tree%20classifier.%20We%20use%20scikit-learn's%20%60HistGradientBoostingClassifier%60%2C%20the%20same%20histogram-based%20algorithm%20family%20as%20LightGBM%2C%20which%20handles%20the%20missing%20composites%20natively.%0A%0A%20%20%20%20**Why%20not%20LightGBM%20itself%3F**%20In%20this%20environment%2C%20LightGBM%20and%20torch%20each%20ship%20their%20own%20copy%20of%20the%20OpenMP%20threading%20library%20(%60libomp.dylib%60).%20Loading%20both%20into%20one%20Python%20process%20crashed%20the%20kernel%20with%20a%20segmentation%20fault%20in%20testing%2C%20whichever%20was%20imported%20first%2C%20and%20neither%20%60OMP_NUM_THREADS%3D1%60%20nor%20%60KMP_DUPLICATE_LIB_OK%3DTRUE%60%20helped.%20LightGBM%20ran%20fine%20in%20a%20separate%20process%2C%20and%20scikit-learn's%20boosting%20coexists%20with%20torch.%0A%0A%20%20%20%20**Two%20head%20starts%20the%20booster%20gets.**%20Composite%20*j*%20covers%20calendar%20days%2016*j*%20to%2016*j*%20%2B%2015%2C%20and%20the%20composites%20from%20the%20prediction%20day%20on%20are%20missing%2C%20so%20the%20bin%20positions%20give%20the%20booster%20calendar%20context%20and%20a%20rough%20cut-off%3A%20the%20date%20to%20within%20a%2016-day%20bin%2C%20blurred%20when%20cloud%20also%20empties%20earlier%20bins.%20That%20is%20a%20date%20cue%20the%20transformer%20doesn't%20get%20(section%204.4%20withholds%20the%20date%20from%20it).%20And%20it%20gets%20NDVI%2C%20which%20this%20generator%20makes%20nearly%20a%20sufficient%20statistic%20(red%20and%20NIR%20are%20both%20linear%20in%20one%20greenness)%2C%20while%20the%20transformer%20gets%20z-scored%20red%20and%20NIR%20and%20must%20learn%20the%20ratio.%20A%20third%20arm%20takes%20NDVI%20away%3A%20each%20composite%20keeps%20the%20red%20and%20NIR%20of%20its%20greenest%20observation%20instead.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_fixed_composites_plus(%0A%20%20%20%20EVAL_DOYS%2C%0A%20%20%20%20PRED_DOYS%2C%0A%20%20%20%20difference%2C%0A%20%20%20%20draw%2C%0A%20%20%20%20over_seeds%2C%0A%20%20%20%20table%2C%0A%20%20%20%20test_idx%2C%0A%20%20%20%20train_idx%2C%0A%20%20%20%20transformer_runs%2C%0A)%3A%0A%20%20%20%20def%20composites(idx%2C%20pred_doys)%3A%0A%20%20%20%20%20%20%20%20%23%2023%20fixed%2016-day%20max-NDVI%20composites%2C%20using%20only%20observations%20up%20to%20each%20pixel's%20prediction%20day.%0A%20%20%20%20%20%20%20%20X%20%3D%20np.full((len(idx)%2C%2023)%2C%20np.nan)%0A%20%20%20%20%20%20%20%20for%20row%2C%20(i%2C%20pred)%20in%20enumerate(zip(idx%2C%20pred_doys%2C%20strict%3DFalse))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20doys%2C%20x%20%3D%20draw.pixels%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20ndvi%20%3D%20(x%5B%3A%2C%201%5D%20-%20x%5B%3A%2C%200%5D)%20%2F%20(x%5B%3A%2C%201%5D%20%2B%20x%5B%3A%2C%200%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20j%20in%20range(23)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20in_bin%20%3D%20(doys%20%3C%3D%20pred)%20%26%20(doys%20%3E%3D%2016%20*%20j)%20%26%20(doys%20%3C%2016%20*%20j%20%2B%2016)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20in_bin.any()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20X%5Brow%2C%20j%5D%20%3D%20ndvi%5Bin_bin%5D.max()%0A%20%20%20%20%20%20%20%20return%20X%0A%0A%20%20%20%20def%20composites_raw(idx%2C%20pred_doys)%3A%0A%20%20%20%20%20%20%20%20%23%20The%20same%2023%20bins%2C%20but%20each%20bin's%20red%20and%20NIR%20(of%20its%20max-NDVI%20observation)%20instead%20of%20NDVI%20itself.%0A%20%20%20%20%20%20%20%20X%20%3D%20np.full((len(idx)%2C%2046)%2C%20np.nan)%0A%20%20%20%20%20%20%20%20for%20row%2C%20(i%2C%20pred)%20in%20enumerate(zip(idx%2C%20pred_doys%2C%20strict%3DFalse))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20doys%2C%20x%20%3D%20draw.pixels%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20ndvi%20%3D%20(x%5B%3A%2C%201%5D%20-%20x%5B%3A%2C%200%5D)%20%2F%20(x%5B%3A%2C%201%5D%20%2B%20x%5B%3A%2C%200%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20j%20in%20range(23)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20in_bin%20%3D%20np.flatnonzero((doys%20%3C%3D%20pred)%20%26%20(doys%20%3E%3D%2016%20*%20j)%20%26%20(doys%20%3C%2016%20*%20j%20%2B%2016))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20in_bin.size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20X%5Brow%2C%202%20*%20j%20%3A%202%20*%20j%20%2B%202%5D%20%3D%20x%5Bin_bin%5Bndvi%5Bin_bin%5D.argmax()%5D%5D%0A%20%20%20%20%20%20%20%20return%20X%0A%0A%20%20%20%20def%20run_boosting(seed%2C%20features%3DNone)%3A%0A%20%20%20%20%20%20%20%20%23%20Seed%200%20reproduces%20the%20single-run%20fit%3A%20prediction%20days%20drawn%20with%20seed%201%2C%20trees%20with%20seed%200.%0A%20%20%20%20%20%20%20%20features%20%3D%20features%20or%20composites%0A%20%20%20%20%20%20%20%20fit_rng%20%3D%20np.random.default_rng(1%20%2B%20seed)%0A%20%20%20%20%20%20%20%20boosted%20%3D%20HistGradientBoostingClassifier(%0A%20%20%20%20%20%20%20%20%20%20%20%20max_iter%3D200%2C%20learning_rate%3D0.05%2C%20random_state%3Dseed%0A%20%20%20%20%20%20%20%20).fit(features(train_idx%2C%20fit_rng.choice(PRED_DOYS%2C%20len(train_idx)))%2C%20draw.kinds%5Btrain_idx%5D)%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20d%3A%20float(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20boosted.predict(features(test_idx%2C%20np.full(len(test_idx)%2C%20d)))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%3D%20draw.kinds%5Btest_idx%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20).mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20d%20in%20EVAL_DOYS%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20boosted_runs%20%3D%20over_seeds(run_boosting)%0A%20%20%20%20raw_boosted_runs%20%3D%20over_seeds(lambda%20seed%3A%20run_boosting(seed%2C%20composites_raw))%0A%20%20%20%20print(f%22fitted%20on%20three%20seeds%20in%20%7Btime.perf_counter()%20-%20_t0%3A.1f%7D%20s%22)%0A%20%20%20%20table(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22transformer%22%3A%20transformer_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22composites%20%2B%20boost%22%3A%20boosted_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22boost%2C%20raw%20red%2BNIR%22%3A%20raw_boosted_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22boost%20%E2%88%92%20transformer%22%3A%20difference(boosted_runs%2C%20transformer_runs)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20return%20(boosted_runs%2C)%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**What%20the%20seeds%20support.**%20**Early%20in%20the%20season%20the%20transformer%20leads**%2C%20in%20all%20three%20seeds%3A%20on%20day%2090%20by%200.018%20to%200.065%20and%20on%20day%20120%20by%200.040%20to%200.088%2C%20more%20than%20the%20sampling%20error.%20**Late%2C%20the%20NDVI%20composites%20lead**%2C%20also%20in%20all%20three%20seeds%20but%20by%20less%3A%200.022%20on%20day%20180%2C%200.003%20to%200.015%20on%20day%20210%2C%20and%20nothing%20on%20day%20300.%20Day%20150%20is%20mixed.%20Taking%20NDVI%20away%20from%20the%20booster%20*helps*%20it%20early%20(day%20120%3A%200.874%20against%200.832)%2C%20because%20the%20max-NDVI%20rule%20already%20threw%20information%20away%3B%20raw%20bands%20close%20most%20of%20its%20gap%20to%20the%20transformer%20while%20keeping%20its%20late-season%20lead.%0A%0A%20%20%20%20So%20the%20transformer's%20advantage%20here%20is%20the%20irregular%20early%20season%2C%20when%20few%20clear%20observations%20exist%20and%20fixed%20bins%20are%20mostly%20empty%20or%20hold%20one%20noisy%20value%3B%20once%20each%20bin%20is%20full%2C%20a%20calendar-aware%20booster%20on%20clean%20single-signal%20data%20is%20very%20hard%20to%20beat.%20That%20matches%20the%20literature%3A%20self-attention's%20edge%20over%20random%20forests%20and%20temporal%20CNNs%20is%20largest%20on%20raw%2C%20cloudy%20series%20and%20shrinks%20on%20cleaned%20ones%20(%5BRu%C3%9Fwurm%20and%20K%C3%B6rner%2C%202020%5D(https%3A%2F%2Farxiv.org%2Fabs%2F1910.10536)).%20Whether%20it%20holds%20with%20many%20classes%20and%20several%20sensors%20in%20one%20sequence%20is%20a%20hypothesis%20this%20synthetic%20test%20doesn't%20cover.%0A%0A%20%20%20%20%23%23%204.2%20Take%20the%20clock%20away%0A%0A%20%20%20%20Train%20the%20same%20architecture%20again%2C%20but%20with%20every%20%60days_back%60%20set%20to%200%2C%20so%20the%20model%20sees%20*what*%20was%20observed%20but%20never%20*when*.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_take_clock_away(%0A%20%20%20%20EVAL_DOYS%2C%0A%20%20%20%20accuracy%2C%0A%20%20%20%20difference%2C%0A%20%20%20%20over_seeds%2C%0A%20%20%20%20table%2C%0A%20%20%20%20train%2C%0A%20%20%20%20transformer_runs%2C%0A)%3A%0A%20%20%20%20def%20run_no_time(seed)%3A%0A%20%20%20%20%20%20%20%20clockless%20%3D%20train(zero_time%3DTrue%2C%20seed%3Dseed%2C%20verbose%3DFalse)%0A%20%20%20%20%20%20%20%20return%20%7Bd%3A%20accuracy(clockless%2C%20d%2C%20zero_time%3DTrue)%20for%20d%20in%20EVAL_DOYS%7D%0A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20no_time_runs%20%3D%20over_seeds(run_no_time)%0A%20%20%20%20print(f%22trained%20on%20three%20seeds%20in%20%7Btime.perf_counter()%20-%20_t0%3A.0f%7D%20s%22)%0A%20%20%20%20table(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22with%20time%22%3A%20transformer_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22without%20time%22%3A%20no_time_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22without%20%E2%88%92%20with%22%3A%20difference(no_time_runs%2C%20transformer_runs)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20return%20(no_time_runs%2C)%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%20Without%20the%20time%20encoding%2C%20self-attention%20treats%20the%20observations%20as%20an%20unordered%20**bag**%20(section%202.3%20checked%20that%20order%20doesn't%20matter)%2C%20so%20%60Time2Vec%60%20is%20the%20only%20thing%20that%20tells%20the%20model%20the%20season's%20shape.%0A%0A%20%20%20%20**What%20the%20seeds%20support.**%20Taking%20the%20clock%20away%20costs%20accuracy%20**on%20every%20day%2C%20in%20every%20seed**.%20The%20cost%20is%20largest%20where%20the%20two%20crops'%20calendars%20matter%20most%3A%20day%20180%2C%20between%20their%20peaks%20(%E2%88%920.25%20to%20%E2%88%920.34)%2C%20and%20day%20120%2C%20as%20the%20early%20crop%20greens%20up%20(%E2%88%920.11%20to%20%E2%88%920.24).%20By%20day%20300%20the%20bag%20of%20values%20nearly%20suffices%20(%E2%88%920.003%20to%20%E2%88%920.017).%20The%20two%20synthetic%20crops%20differ%20in%20peak%20date%2C%20height%20and%20width%2C%20so%20this%20measures%20what%20removing%20this%20generator's%20time%20input%20costs%2C%20not%20that%20timing%20alone%20separates%20them.%0A%0A%20%20%20%20%23%23%204.3%20Ignore%20the%20padding%20mask%0A%0A%20%20%20%20Score%20the%20same%20trained%20models%20with%20every%20padded%20slot%20treated%20as%20a%20real%20observation%2C%20then%20look%20at%20what%20they%20predict.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_3_ignore_padding_mask(%0A%20%20%20%20CLASSES%2C%0A%20%20%20%20EVAL_DOYS%2C%0A%20%20%20%20SEEDS%2C%0A%20%20%20%20TRANSFORMERS%2C%0A%20%20%20%20T_MAX%2C%0A%20%20%20%20accuracy%2C%0A%20%20%20%20difference%2C%0A%20%20%20%20make_batch%2C%0A%20%20%20%20over_seeds%2C%0A%20%20%20%20table%2C%0A%20%20%20%20test_idx%2C%0A%20%20%20%20transformer_runs%2C%0A)%3A%0A%20%20%20%20mask_runs%20%3D%20over_seeds(%0A%20%20%20%20%20%20%20%20lambda%20seed%3A%20%7Bd%3A%20accuracy(TRANSFORMERS%5Bseed%5D%2C%20d%2C%20ignore_mask%3DTrue)%20for%20d%20in%20EVAL_DOYS%7D%0A%20%20%20%20)%0A%20%20%20%20table(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22with%20mask%22%3A%20transformer_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mask%20ignored%22%3A%20mask_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22ignored%20%E2%88%92%20with%22%3A%20difference(mask_runs%2C%20transformer_runs)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20def%20class_shares(model%2C%20pred_doy)%3A%0A%20%20%20%20%20%20%20%20%23%20Share%20of%20test%20pixels%20given%20each%20class%20when%20every%20padded%20slot%20is%20read%20as%20an%20observation.%0A%20%20%20%20%20%20%20%20bands%2C%20days_back%2C%20mask%20%3D%20make_batch(test_idx%2C%20np.full(len(test_idx)%2C%20pred_doy))%0A%20%20%20%20%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20embedding%20%3D%20model%5B%22enc%22%5D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bands%3Dbands%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20days_back%3Ddays_back%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20static%3Dtorch.zeros(len(test_idx)%2C%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20attn_mask%3Dtorch.ones_like(mask)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20predicted%20%3D%20model%5B%22head%22%5D(embedding).argmax(1).numpy()%0A%20%20%20%20%20%20%20%20return%20np.bincount(predicted%2C%20minlength%3D3)%20%2F%20len(predicted)%0A%0A%20%20%20%20shares%20%3D%20over_seeds(lambda%20seed%3A%20%7Bd%3A%20class_shares(TRANSFORMERS%5Bseed%5D%2C%20d)%20for%20d%20in%20(210%2C%20300)%7D)%0A%20%20%20%20for%20_seed%20in%20SEEDS%3A%0A%20%20%20%20%20%20%20%20for%20_d%20in%20(210%2C%20300)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22seed%20%7B_seed%7D%2C%20day%20%7B_d%7D%2C%20mask%20ignored%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%7Bc%7D%20%7Bp%3A.0%25%7D%22%20for%20c%2C%20p%20in%20zip(CLASSES%2C%20shares%5B_seed%5D%5B_d%5D%2C%20strict%3DTrue))%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22real%20tokens%20per%20test%20pixel%20on%20day%20300%3A%22%2C%0A%20%20%20%20%20%20%20%20int(make_batch(test_idx%2C%20np.full(len(test_idx)%2C%20300))%5B2%5D.sum(1).float().median())%2C%0A%20%20%20%20%20%20%20%20%22of%22%2C%0A%20%20%20%20%20%20%20%20T_MAX%2C%0A%20%20%20%20%20%20%20%20%22slots%22%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%20Every%20padded%20slot%20becomes%20a%20fake%20observation%20of%20zero%20reflectance%20taken%20%22today%22%20(%60days_back%60%200).%20**Ignoring%20the%20mask%20hurts%20on%20every%20prediction%20day%2C%20in%20every%20seed**%2C%20by%200.29%20to%200.67%2C%20with%20no%20error%20message.%20The%20predictions%20show%20what%20the%20models%20make%20of%20it%3A%20on%20day%20300%2C%20seeds%201%20and%202%20call%2099%20%25%20of%20pixels%20%22other%20crop%22%2C%20and%20seed%200%20calls%2058%20%25%20%22other%20crop%22%20and%2036%20%25%20maize.%20A%20pile%20of%20%22bare%2C%20today%22%20observations%20looks%20like%20a%20crop%20that%20has%20already%20been%20harvested%2C%20which%20in%20this%20generator%20is%20the%20early%20crop.%20How%20hard%20a%20model%20is%20fooled%20depends%20on%20what%20it%20learned%20and%20on%20how%20much%20of%20the%20sequence%20is%20padding%3A%20here%20a%20median%20pixel%20has%2033%20real%20tokens%20in%2080%20slots%2C%20and%20production%20trains%20with%20%60t_max%20%3D%20200%60%20slots%20(%5B%60train.py%60%20L40%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2Fsrc%2Fmaize_subannual%2Ftrain.py%23L40))%2C%20so%20its%20padding%20share%20can%20be%20larger%20still.%0A%0A%20%20%20%20A%20mask%20bug%20produces%20no%20error%20message%2C%20just%20a%20model%20that%20quietly%20reads%20padding%20as%20data%2C%20which%20is%20why%20%60fr_crop_type%60%20carries%20a%20GUARD%20comment%20on%20exactly%20this%20line.%0A%20%20%20%20%22%22%22)%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%204.4%20Tell%20it%20the%20date%0A%0A%20%20%20%20Production%20never%20tells%20the%20model%20the%20calendar%20date%20of%20the%20prediction%20(see%20the%20study%20question%20in%20%60science.md%60).%20Here%20we%20retrain%20the%20same%20small%20model%20with%20the%20prediction%20day%20passed%20in%20as%20its%20one%20static%20feature%20(%60pred_doy%20%2F%20365%60)%20and%20compare%20it%20with%20the%20run%20that%20gets%20a%20zero.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4_tell_date(%0A%20%20%20%20EVAL_DOYS%2C%0A%20%20%20%20accuracy%2C%0A%20%20%20%20difference%2C%0A%20%20%20%20over_seeds%2C%0A%20%20%20%20table%2C%0A%20%20%20%20train%2C%0A%20%20%20%20transformer_runs%2C%0A)%3A%0A%20%20%20%20def%20run_dated(seed)%3A%0A%20%20%20%20%20%20%20%20dated%20%3D%20train(use_date%3DTrue%2C%20seed%3Dseed%2C%20verbose%3DFalse)%0A%20%20%20%20%20%20%20%20return%20%7Bd%3A%20accuracy(dated%2C%20d%2C%20use_date%3DTrue)%20for%20d%20in%20EVAL_DOYS%7D%0A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20dated_runs%20%3D%20over_seeds(run_dated)%0A%20%20%20%20print(f%22trained%20on%20three%20seeds%20in%20%7Btime.perf_counter()%20-%20_t0%3A.0f%7D%20s%22)%0A%20%20%20%20table(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22static%20%3D%200%22%3A%20transformer_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22static%20%3D%20date%22%3A%20dated_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22date%20%E2%88%92%20zero%22%3A%20difference(dated_runs%2C%20transformer_runs)%2C%0A%20%20%20%20%20%20%20%20%7D%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**What%20the%20seeds%20support.**%20Telling%20the%20model%20the%20date%20has%20**no%20effect%20beyond%20sampling%20error%20on%20any%20day**%3A%20every%20paired%20difference%20is%20within%20about%20%C2%B10.03%2C%20the%20signs%20change%20between%20seeds%20on%20days%2090%20to%20180%2C%20and%20the%20small%20positive%20values%20on%20days%20210%20and%20300%20are%20a%20few%20test%20pixels.%20That's%20what%20the%20study%20question%20in%20%60science.md%60%20predicts.%20Every%20pixel's%20observations%20start%20in%20the%20first%20days%20of%20the%20year%2C%20so%20the%20oldest%20observation's%20%60days_back%60%20already%20tells%20the%20model%20roughly%20what%20day%20it%20is%2C%20and%20production's%20windows%20do%20the%20same%3A%20they%20start%20on%201%20January%20(%5B%60stream.py%60%20L65-68%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2Fsrc%2Fmaize_subannual%2Fstream.py%23L65-L68)).%0A%20%20%20%20%22%22%22)%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%204.5%20What%20the%20one-cycle%20schedule%20buys%0A%0A%20%20%20%20Everything%20above%20trains%20with%20production's%20schedule.%20Train%20the%20same%20model%20once%20more%20on%20each%20seed%20with%20a%20constant%20learning%20rate%20instead%2C%20the%20recipe%20this%20notebook%20first%20used.%20This%20removes%20the%20whole%20one-cycle%20schedule%20at%20once%3A%20the%20warm-up%2C%20the%20annealing%20and%20the%20momentum%20cycling.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_5_cycle_schedule_buys(%0A%20%20%20%20EVAL_DOYS%2C%0A%20%20%20%20SEEDS%2C%0A%20%20%20%20accuracy%2C%0A%20%20%20%20boosted_runs%2C%0A%20%20%20%20difference%2C%0A%20%20%20%20no_time_runs%2C%0A%20%20%20%20over_seeds%2C%0A%20%20%20%20table%2C%0A%20%20%20%20train%2C%0A%20%20%20%20transformer_runs%2C%0A)%3A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%0A%20%20%20%20def%20run_constant(seed)%3A%0A%20%20%20%20%20%20%20%20constant%20%3D%20train(%0A%20%20%20%20%20%20%20%20%20%20%20%20seed%3Dseed%2C%20one_cycle%3DFalse%2C%20verbose%3DFalse%0A%20%20%20%20%20%20%20%20)%20%20%23%20one%20fit%20per%20seed%2C%20scored%20on%20every%20day%0A%20%20%20%20%20%20%20%20return%20%7Bd%3A%20accuracy(constant%2C%20d)%20for%20d%20in%20EVAL_DOYS%7D%0A%0A%20%20%20%20constant_runs%20%3D%20over_seeds(run_constant)%0A%20%20%20%20print(f%22trained%20on%20three%20seeds%20in%20%7Btime.perf_counter()%20-%20_t0%3A.0f%7D%20s%22)%0A%20%20%20%20table(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22one-cycle%20schedule%22%3A%20transformer_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22constant%20rate%22%3A%20constant_runs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22constant%20%E2%88%92%20one-cycle%22%3A%20difference(constant_runs%2C%20transformer_runs)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.6))%0A%20%20%20%20_arms%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22transformer%20(one-cycle)%22%3A%20(transformer_runs%2C%20%22tab%3Ablue%22)%2C%0A%20%20%20%20%20%20%20%20%22composites%20%2B%20boosting%22%3A%20(boosted_runs%2C%20%22tab%3Aorange%22)%2C%0A%20%20%20%20%20%20%20%20%22transformer%2C%20no%20clock%22%3A%20(no_time_runs%2C%20%22tab%3Agrey%22)%2C%0A%20%20%20%20%20%20%20%20%22transformer%2C%20constant%20rate%22%3A%20(constant_runs%2C%20%22tab%3Ared%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20for%20offset%2C%20(_name%2C%20(runs%2C%20_colour))%20in%20zip((-4%2C%20-1.5%2C%201.5%2C%204)%2C%20_arms.items()%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20for%20_seed%20in%20SEEDS%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.array(EVAL_DOYS)%20%2B%20offset%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bruns%5B_seed%5D%5Bd%5D%20for%20d%20in%20EVAL_DOYS%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22o%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3D_colour%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ms%3D4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3D_name%20if%20_seed%20%3D%3D%200%20else%20None%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.array(EVAL_DOYS)%20%2B%20offset%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bnp.mean(%5Bruns%5Bs%5D%5Bd%5D%20for%20s%20in%20SEEDS%5D)%20for%20d%20in%20EVAL_DOYS%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22-%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3D_colour%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20lw%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_ax.set_xlabel(%22prediction%20day%22)%0A%20%20%20%20_ax.set_ylabel(%22held-out%20accuracy%22)%0A%20%20%20%20_ax.set_title(%22Accuracy%20through%20the%20season%3A%20one%20point%20per%20seed%2C%20lines%20through%20the%20means%22)%0A%20%20%20%20_ax.legend(fontsize%3D8%2C%20loc%3D%22lower%20right%22)%0A%20%20%20%20plt.show()%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**What%20the%20seeds%20support.**%20Without%20the%20one-cycle%20schedule%20the%20model%20is%20**worse%20early%20in%20every%20seed**%20(day%2090%3A%20%E2%88%920.028%20to%20%E2%88%920.128%3B%20day%20120%3A%20%E2%88%920.015%20to%20%E2%88%920.100%3B%20day%20150%3A%20%E2%88%920.007%20to%20%E2%88%920.032)%2C%20and%20one%20run%20in%20three%20**collapses**%20on%20day%20180%20(seed%202%3A%20%E2%88%920.290)%2C%20the%20same%20failure%20this%20notebook's%20first%20version%20reported%20as%20a%20property%20of%20small%20transformers.%20It's%20a%20property%20of%20the%20schedule.%20This%20comparison%20doesn't%20say%20which%20part%20of%20the%20schedule%20matters%20(exercise%2011%20isolates%20the%20warm-up)%2C%20but%20the%20warm-up%20is%20the%20usual%20suspect.%20At%20the%20start%20of%20training%20Adam's%20estimates%20of%20the%20gradients'%20spread%20rest%20on%20very%20few%20steps%2C%20so%20its%20first%20updates%20can%20be%20large%20and%20badly%20aimed%3B%20a%20warm-up%20keeps%20them%20small%20until%20those%20estimates%20settle%20(%5BLiu%20et%20al.%2C%202020%5D(https%3A%2F%2Farxiv.org%2Fabs%2F1908.03265)).%20Pre-LN%20transformers%20like%20this%20one%20depend%20on%20warm-up%20less%20than%20post-LN%20ones%20(%5BXiong%20et%20al.%2C%202020%5D(https%3A%2F%2Farxiv.org%2Fabs%2F2002.04745))%2C%20though%20at%20this%20small%20size%20and%20this%20high%20a%20rate%20the%20schedule%20as%20a%20whole%20decided%20whether%20a%20run%20collapsed.%20The%20plot%20puts%20the%20four%20main%20arms%20side%20by%20side%3A%20the%20transformer%20leads%20early%2C%20the%20composites%20edge%20it%20late%2C%20and%20the%20clockless%20and%20constant-rate%20runs%20fall%20behind%20both.%0A%20%20%20%20%22%22%22)%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%204.6%20The%20roads%20model-foundry%20itself%20took%0A%0A%20%20%20%20The%20classes%20taught%20here%20exist%20identically%20in%20%60maize_subannual%60%2C%20%60crop_classifier_s2%60%20and%20tf-deep-landcover.%20%60fr_crop_type%60%20took%20different%20turns%20on%20purpose%3A%20it%20adds%20embeddings%20for%20sensor%2C%20orbit%20direction%20and%20relative%20orbit%20track%2C%20uses%20geometrically%20spaced%20Time2Vec%20frequencies%20with%20random%20phases%2C%20fits%20%60BandNorm%60%20from%20data%2C%20and%20offers%20**mean%20or%20attention%20pooling**.%20Its%20own%20comment%20records%20that%20mean%20pooling%20%22cost%204.0%20pp%20zone-holdout%22%2C%20yet%20%60pool%60%20defaults%20to%20%60%22mean%22%60.%20Its%20training%20code%20also%20records%20a%20shortcut%20like%20this%20notebook's%20date%20proxy%3A%20%22track%20coverage%20is%20a%20longitude%20proxy%20that%20inflates%20pooled%20scores%22.%20%60code.md%60%20quotes%20and%20compares%20them.%0A%20%20%20%20%22%22%22)%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%20Part%205%3A%20embeddings%20and%20foundation%20models%0A%0A%20%20%20%20An%20**embedding**%20is%20a%20vector%20a%20model%20computes%20for%20an%20input%2C%20in%20which%20similar%20inputs%20land%20close%20together.%20The%20temporal%20transformer's%20pooled%20128-number%20vector%20is%20one.%20A%20**foundation%20model**%20is%20a%20large%20model%20trained%20once%2C%20usually%20without%20crop%20labels%2C%20whose%20embeddings%20are%20then%20reused%20for%20many%20tasks.%20Google's%20AlphaEarth%20Foundations%20publishes%20one%2064-number%20embedding%20per%2010%20m%20pixel%20per%20year%2C%20computed%20from%20a%20year%20of%20optical%2C%20radar%20and%20climate%20data%3B%20model-foundry's%20%60maize_presence_aef%60%20classifies%20maize%20from%20those%20embeddings%2C%20with%20two%20stacked%20boosters%20on%20top.%20%60maize_subannual%60%20deliberately%20uses%20none%3A%20%22an%20embedding%20that%20already%20encodes%20climate%20would%20confound%20the%20very%20hypothesis%20under%20test%22%2C%20whether%20maize%20responds%20to%20rainfall%20(%5BMODEL_CARD.md%20L5-9%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2FMODEL_CARD.md%23L5-L9)).%0A%0A%20%20%20%20%23%23%205.1%20Reading%20an%20embedding%3A%20int8%20and%20the%20signed%20square%0A%0A%20%20%20%20AlphaEarth's%20embeddings%20are%20unit%20vectors%2C%20stored%20as%20int8%20to%20save%20space.%20%60maize_presence_aef%60%20turns%20them%20back%20into%20floats%20with%20a%20**signed%20square**%20(%5B%60aef.py%60%20L261-269%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_presence_aef%2Fsrc%2Fmaize_presence_aef%2Faef.py%23L261-L269)%2C%20copied%20below%20because%20the%20package%20isn't%20installed%20here)%3A%20divide%20by%20127.5%2C%20then%20multiply%20by%20its%20own%20absolute%20value.%20Why%20not%20store%20the%20value%20linearly%3F%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s5_1_reading_embedding_int8()%3A%0A%20%20%20%20NODATA_INT8%20%3D%20-128%0A%20%20%20%20DIVISOR%20%3D%20127.5%0A%0A%20%20%20%20def%20dequantize(raw%3A%20np.ndarray)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22int8%20embeddings%20-%3E%20float32%20in%20%5B-1%2C%201%5D%2C%20nodata%20-%3E%20%60%60NaN%60%60.%22%22%22%0A%20%20%20%20%20%20%20%20if%20raw.dtype%20not%20in%20(np.int8%2C%20np.uint8)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raw%20%3D%20raw.view(np.int8)%0A%20%20%20%20%20%20%20%20nodata%20%3D%20raw%20%3D%3D%20NODATA_INT8%0A%20%20%20%20%20%20%20%20out%20%3D%20raw.astype(np.float32)%20%2F%20DIVISOR%0A%20%20%20%20%20%20%20%20np.multiply(out%2C%20np.abs(out)%2C%20out%3Dout)%0A%20%20%20%20%20%20%20%20out%5Bnodata%5D%20%3D%20np.nan%0A%20%20%20%20%20%20%20%20return%20out%0A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20_v%20%3D%20_rng.normal(size%3D(20_000%2C%2064))%0A%20%20%20%20_v%20%2F%3D%20np.linalg.norm(%0A%20%20%20%20%20%20%20%20_v%2C%20axis%3D1%2C%20keepdims%3DTrue%0A%20%20%20%20)%20%20%23%20AlphaEarth%20embeddings%20are%20unit%20vectors%20in%2064%20dimensions%0A%20%20%20%20signed_sqrt%20%3D%20np.clip(np.round(np.sign(_v)%20*%20np.sqrt(np.abs(_v))%20*%20DIVISOR)%2C%20-127%2C%20127).astype(%0A%20%20%20%20%20%20%20%20np.int8%0A%20%20%20%20)%0A%20%20%20%20linear%20%3D%20np.clip(np.round(_v%20*%20127)%2C%20-127%2C%20127).astype(np.int8)%0A%20%20%20%20err_sq%20%3D%20dequantize(signed_sqrt)%20-%20_v%0A%20%20%20%20err_lin%20%3D%20linear.astype(np.float32)%20%2F%20127%20-%20_v%0A%20%20%20%20print(f%22typical%20%7Ccomponent%7C%20of%20a%2064-d%20unit%20vector%3A%20%7Bnp.median(np.abs(_v))%3A.3f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22RMS%20error%3A%20signed%20square%20%7Bnp.sqrt((err_sq**2).mean())%3A.5f%7D%20%7C%20linear%20%7Bnp.sqrt((err_lin**2).mean())%3A.5f%7D%22%0A%20%20%20%20)%0A%20%20%20%20small%20%3D%20np.abs(_v)%20%3C%200.05%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22for%20components%20below%200.05%3A%20signed%20square%20%7Bnp.sqrt((err_sq%5Bsmall%5D%20**%202).mean())%3A.5f%7D%20%7C%20linear%20%7Bnp.sqrt((err_lin%5Bsmall%5D%20**%202).mean())%3A.5f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%22int8%20-128%20(nodata)%20-%3E%22%2C%20dequantize(np.array(%5B-128%2C%20-127%2C%20-1%2C%200%2C%201%2C%20127%5D%2C%20np.int8)))%0A%0A%20%20%20%20%23%20Production's%20guard%20skips%20the%20int8%20view%20for%20uint8%20input%2C%20so%20signed%20bytes%20read%20as%20unsigned%20are%20mis-dequantised%3A%0A%20%20%20%20signed%20%3D%20np.array(%5B-1%2C%20-128%5D%2C%20np.int8)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22bytes%20-1%20and%20-128%20as%20int8%3A%22%2C%0A%20%20%20%20%20%20%20%20dequantize(signed)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20the%20same%20bytes%20as%20uint8%3A%22%2C%0A%20%20%20%20%20%20%20%20dequantize(signed.view(np.uint8))%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%20A%2064-dimensional%20unit%20vector's%20components%20are%20small%2C%20typically%200.085%2C%20and%20the%20signed%20square%20spends%20its%20255%20levels%20where%20they%20are%3A%20its%20error%20on%20components%20below%200.05%20is%20a%20third%20of%20a%20linear%20code's.%20%E2%88%92128%20is%20reserved%20for%20no%20data%20and%20becomes%20NaN%2C%20which%20section%206.1%20shows%20is%20dangerous%20to%20feed%20a%20network.%20The%20last%20line%20shows%20a%20trap%20in%20the%20guard%3A%20%60if%20raw.dtype%20not%20in%20(np.int8%2C%20np.uint8)%60%20reinterprets%20only%20*other*%20dtypes%20as%20int8%2C%20so%20the%20same%20bytes%20arriving%20as%20uint8%20skip%20the%20view%2C%20and%20%E2%88%921%20and%20the%20no-data%20code%20come%20back%20as%204.0%20and%201.008.%20A%20reader%20that%20returns%20int8%2C%20as%20rasterio's%20bundled%20GDAL%20(3.7%20and%20later)%20does%20for%20Int8%20rasters%2C%20never%20meets%20it%3B%20a%20reader%20that%20reports%20signed%20bytes%20as%20uint8%20would.%20tf-deep-landcover's%20AlphaEarth%20loader%20uses%20the%20same%20signed%20square%20(%5B%60aef_mosaic.py%60%20L148-157%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fshared_packages%2Fgee%2Fsrc%2Fgee%2Fsatellite%2Faef_mosaic.py%23L148-L157)).%0A%0A%20%20%20%20%23%23%205.2%20A%20frozen%20encoder%20and%20a%20linear%20probe%0A%0A%20%20%20%20The%20point%20of%20a%20foundation%20model%20is%20to%20reuse%20it.%20The%20cheapest%20reuse%20is%20a%20**linear%20probe**%3A%20keep%20the%20encoder%20frozen%20and%20fit%20only%20a%20linear%20classifier%20on%20its%20embeddings.%20Treat%20Part%203's%20trained%20transformer%20as%20the%20frozen%20encoder%20and%20ask%20how%20far%20a%20handful%20of%20labels%20go%2C%20against%20training%20the%20whole%20transformer%20from%20scratch%20on%20the%20same%20handful%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s5_2_frozen_encoder_linear(%0A%20%20%20%20TRANSFORMERS%2C%0A%20%20%20%20accuracy%2C%0A%20%20%20%20draw%2C%0A%20%20%20%20make_batch%2C%0A%20%20%20%20test_idx%2C%0A%20%20%20%20train%2C%0A%20%20%20%20train_idx%2C%0A%20%20%20%20use_data%2C%0A)%3A%0A%20%20%20%20frozen%20%3D%20TRANSFORMERS%5B0%5D%0A%0A%20%20%20%20def%20embed(model%2C%20idx%2C%20pred_doy)%3A%0A%20%20%20%20%20%20%20%20bands%2C%20days_back%2C%20mask%20%3D%20make_batch(idx%2C%20np.full(len(idx)%2C%20pred_doy))%0A%20%20%20%20%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20model%5B%22enc%22%5D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bands%3Dbands%2C%20days_back%3Ddays_back%2C%20static%3Dtorch.zeros(len(idx)%2C%201)%2C%20attn_mask%3Dmask%0A%20%20%20%20%20%20%20%20%20%20%20%20).numpy()%0A%0A%20%20%20%20active%20%3D%20(%0A%20%20%20%20%20%20%20%20draw.pixels%2C%0A%20%20%20%20%20%20%20%20draw.kinds%2C%0A%20%20%20%20%20%20%20%20draw.stats_path%2C%0A%20%20%20%20)%20%20%23%20restored%20below%3A%20the%20probe%20leaves%20the%20active%20draw%20as%20it%20found%20it%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20use_data(0)%0A%20%20%20%20%20%20%20%20few%20%3D%20np.concatenate(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Btrain_idx%5Bdraw.kinds%5Btrain_idx%5D%20%3D%3D%20k%5D%5B%3A10%5D%20for%20k%20in%20range(3)%5D%0A%20%20%20%20%20%20%20%20)%20%20%23%2010%20labelled%20pixels%20per%20class%0A%20%20%20%20%20%20%20%20probe%20%3D%20LogisticRegression(max_iter%3D2000).fit(embed(frozen%2C%20few%2C%20150)%2C%20draw.kinds%5Bfew%5D)%0A%20%20%20%20%20%20%20%20probe_acc%20%3D%20probe.score(embed(frozen%2C%20test_idx%2C%20150)%2C%20draw.kinds%5Btest_idx%5D)%0A%20%20%20%20%20%20%20%20scratch%20%3D%20train(%0A%20%20%20%20%20%20%20%20%20%20%20%20seed%3D0%2C%20verbose%3DFalse%2C%20epochs%3D60%2C%20train_on%3Dfew%0A%20%20%20%20%20%20%20%20)%20%20%23%20from%20scratch%20on%20the%20same%2030%20pixels%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%2230%20labelled%20pixels%2C%20day%20150%3A%20linear%20probe%20on%20the%20frozen%20encoder%20%7Bprobe_acc%3A.3f%7D%20%7C%20transformer%20trained%20from%20scratch%20%7Baccuracy(scratch%2C%20150)%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20finally%3A%0A%20%20%20%20%20%20%20%20draw.pixels%2C%20draw.kinds%2C%20draw.stats_path%20%3D%20active%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%20With%2010%20labels%20per%20class%2C%20the%20probe%20on%20the%20frozen%20encoder%20scores%200.978%20on%20day%20150%2C%20and%20the%20transformer%20trained%20from%20scratch%20on%20the%20same%2030%20pixels%200.577.%20A%20caveat%20makes%20this%20a%20demonstration%2C%20not%20a%20fair%20contest%3A%20the%20%22foundation%22%20encoder%20here%20was%20trained%20on%202%2C400%20labelled%20pixels%20of%20the%20same%20task%2C%20which%20a%20real%20foundation%20model%20never%20sees.%20What%20carries%20over%20is%20the%20mechanism%3A%20an%20encoder%20that%20already%20separates%20the%20inputs%20leaves%20very%20little%20for%20the%20labels%20to%20do.%20That%20is%20why%20%60maize_presence_aef%60%20can%20learn%20maize%20from%20embeddings%20with%20a%20boosted%20tree%2C%20and%20why%20%60maize_subannual%60%2C%20which%20refuses%20embeddings%2C%20must%20train%20its%20own%20encoder%20end%20to%20end.%0A%0A%20%20%20%20%23%23%205.3%20The%20same%20ground%2C%20a%20different%20year%3A%20drift%0A%0A%20%20%20%20An%20embedding%20model%20can%20encode%20the%20*year*%20as%20well%20as%20the%20land%3A%20each%20year's%20embeddings%20are%20computed%20from%20different%20imagery%20and%20weather.%20%60maize_presence_aef%60%20measured%20it%20on%20366%2C046%20pixels%20its%20teacher%20maps%20as%20non-crop%20in%20every%20season%3A%20the%20between-year%20drift%20of%20the%20mean%20embedding%20is%200.02387%20per%20band%2C%20larger%20than%20the%20whole%20maize%2Fnon-maize%20signal%20of%200.02152%20(%5BMODEL_CARD.md%20L74-81%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_presence_aef%2FMODEL_CARD.md%23L74-L81)).%20Its%20fix%2C%20%60estimate_drift%60%20(%5Bfeatures.py%20L47-60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_presence_aef%2Fsrc%2Fmaize_presence_aef%2Ffeatures.py%23L47-L60)%2C%20copied%20below)%2C%20takes%20each%20year's%20mean%20over%20unchanging%20land%2C%20centres%20the%20years%2C%20and%20subtracts%20each%20year's%20offset%20at%20training%20and%20prediction%20time.%20Simulate%20drift%20of%20that%20size%20and%20train%20on%20two%20years%2C%20then%20predict%20a%20third%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s5_3_same_ground_different()%3A%0A%20%20%20%20def%20estimate_drift(stable_means%2C%20counts)%3A%0A%20%20%20%20%20%20%20%20years%20%3D%20sorted(stable_means)%0A%20%20%20%20%20%20%20%20matrix%20%3D%20np.stack(%5Bstable_means%5By%5D%20for%20y%20in%20years%5D)%0A%20%20%20%20%20%20%20%20centre%20%3D%20matrix.mean(axis%3D0)%0A%20%20%20%20%20%20%20%20offsets%20%3D%20%7By%3A%20(stable_means%5By%5D%20-%20centre).astype(np.float32)%20for%20y%20in%20years%7D%0A%20%20%20%20%20%20%20%20return%20offsets%2C%20%7B%22years%22%3A%20years%7D%0A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(1)%0A%20%20%20%20D%20%3D%2064%0A%20%20%20%20maize_dir%20%3D%20_rng.normal(size%3DD)%0A%20%20%20%20maize_dir%20%2F%3D%20np.linalg.norm(maize_dir)%0A%20%20%20%20_years%20%3D%20(2020%2C%202021%2C%202022)%0A%20%20%20%20year_shift%20%3D%20%7B%0A%20%20%20%20%20%20%20%20y%3A%20_rng.normal(0%2C%200.0239%2C%20D)%20for%20y%20in%20_years%0A%20%20%20%20%7D%20%20%23%20between-year%20drift%20per%20band%2C%20as%20measured%0A%20%20%20%20base%20%3D%20_rng.normal(0%2C%200.06%2C%20D)%0A%0A%20%20%20%20def%20embedding_pixels(year%2C%20n%2C%20maize_share)%3A%0A%20%20%20%20%20%20%20%20is_maize%20%3D%20_rng.random(n)%20%3C%20maize_share%0A%20%20%20%20%20%20%20%20noise%20%3D%20_rng.normal(0%2C%200.03%2C%20(n%2C%20D))%0A%20%20%20%20%20%20%20%20x%20%3D%20base%20%2B%20year_shift%5Byear%5D%20%2B%20noise%20%2B%20np.outer(is_maize%2C%20maize_dir%20*%200.0215%20*%20np.sqrt(D))%0A%20%20%20%20%20%20%20%20return%20x.astype(np.float32)%2C%20is_maize.astype(int)%0A%0A%20%20%20%20train_years%20%3D%20%5Bembedding_pixels(y%2C%203000%2C%200.4)%20for%20y%20in%20(2020%2C%202021)%5D%0A%20%20%20%20stable%20%3D%20%7B%0A%20%20%20%20%20%20%20%20y%3A%20(base%20%2B%20year_shift%5By%5D%20%2B%20_rng.normal(0%2C%200.03%2C%20(5000%2C%20D))).mean(0)%20for%20y%20in%20_years%0A%20%20%20%20%7D%20%20%23%20invariant%20non-crop%20land%0A%20%20%20%20offsets%2C%20_%20%3D%20estimate_drift(stable%2C%20dict.fromkeys(_years%2C%205000))%0A%20%20%20%20Xtr%20%3D%20np.concatenate(%5Bx%20for%20x%2C%20_%20in%20train_years%5D)%0A%20%20%20%20ytr%20%3D%20np.concatenate(%5By%20for%20_%2C%20y%20in%20train_years%5D)%0A%20%20%20%20Xtr_c%20%3D%20np.concatenate(%0A%20%20%20%20%20%20%20%20%5Bx%20-%20offsets%5By%5D%20for%20(x%2C%20_)%2C%20y%20in%20zip(train_years%2C%20(2020%2C%202021)%2C%20strict%3DTrue)%5D%0A%20%20%20%20)%0A%20%20%20%20Xte%2C%20yte%20%3D%20embedding_pixels(2022%2C%203000%2C%200.4)%0A%20%20%20%20raw%20%3D%20LogisticRegression(max_iter%3D2000).fit(Xtr%2C%20ytr)%0A%20%20%20%20corr%20%3D%20LogisticRegression(max_iter%3D2000).fit(Xtr_c%2C%20ytr)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22held-out%20year%202022%3A%20raw%20accuracy%20%7Braw.score(Xte%2C%20yte)%3A.3f%7D%2C%20predicted%20maize%20share%20%7Braw.predict(Xte).mean()%3A.2f%7D%20(true%200.40)%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22drift-corrected%20%20%20%3A%20accuracy%20%7Bcorr.score(Xte%20-%20offsets%5B2022%5D%2C%20yte)%3A.3f%7D%2C%20predicted%20maize%20share%20%7Bcorr.predict(Xte%20-%20offsets%5B2022%5D).mean()%3A.2f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22drift%20per%20band%20%7Bnp.abs(year_shift%5B2022%5D%20-%20year_shift%5B2020%5D).mean()%3A.4f%7D%20vs%20maize%20signal%20per%20band%20%7B0.0215%3A.4f%7D%22%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%20With%20the%20drift%20left%20in%2C%20the%20classifier%20is%20shifted%20on%20the%20new%20year%20and%20over-predicts%20maize%3B%20subtracting%20each%20year's%20offset%2C%20measured%20on%20land%20that%20never%20changes%2C%20recovers%20almost%20every%20pixel.%20In%20production%20that%20offset%20correlated%20with%20predicted%20maize%20on%20a%20rangeland%20square%20at%20r%20%3D%200.86%3A%20unchanged%20land%20drifting%20towards%20the%20maize%20direction%20looks%20like%20new%20maize.%0A%0A%20%20%20%20%23%23%205.4%20Stacking%3A%20why%20a%20stacked%20feature%20must%20be%20out%20of%20fold%0A%0A%20%20%20%20%60maize_presence_aef%60%20stacks%20two%20boosters.%20Stage%20A%20predicts%20cropland%3B%20its%20probability%20becomes%20a%20feature%20for%20stage%20B%2C%20which%20predicts%20maize.%20Stage%20A's%20probability%20on%20its%20own%20training%20rows%20is%20in-sample%3A%20a%20flexible%20model%20has%20seen%20those%20rows'%20labels%20and%20is%20too%20sure%20of%20them.%20Production%20feeds%20stage%20B%20the%20**out-of-fold**%20probability%20instead%20(%5B%60train.py%60%20L190-199%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_presence_aef%2Fsrc%2Fmaize_presence_aef%2Ftrain.py%23L190-L199))%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s5_4_stacking_stacked_feature()%3A%0A%20%20%20%20from%20sklearn.base%20import%20clone%0A%20%20%20%20from%20sklearn.model_selection%20import%20KFold%2C%20cross_val_predict%0A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2)%0A%20%20%20%20_n%20%3D%202000%0A%20%20%20%20Xa%20%3D%20_rng.normal(size%3D(_n%2C%2010))%0A%20%20%20%20cropland%20%3D%20(Xa%5B%3A%2C%200%5D%20%2B%200.8%20*%20_rng.normal(size%3D_n))%20%3E%200%0A%20%20%20%20maize%20%3D%20cropland%20%26%20((Xa%5B%3A%2C%201%5D%20%2B%200.8%20*%20_rng.normal(size%3D_n))%20%3E%200)%0A%20%20%20%20Xnew%20%3D%20_rng.normal(size%3D(_n%2C%2010))%0A%20%20%20%20crop_new%20%3D%20(Xnew%5B%3A%2C%200%5D%20%2B%200.8%20*%20_rng.normal(size%3D_n))%20%3E%200%0A%20%20%20%20maize_new%20%3D%20crop_new%20%26%20((Xnew%5B%3A%2C%201%5D%20%2B%200.8%20*%20_rng.normal(size%3D_n))%20%3E%200)%0A%20%20%20%20stage_a%20%3D%20HistGradientBoostingClassifier(%0A%20%20%20%20%20%20%20%20max_leaf_nodes%3D255%2C%20min_samples_leaf%3D1%2C%20max_iter%3D100%2C%20random_state%3D0%0A%20%20%20%20)%0A%20%20%20%20stage_b%20%3D%20HistGradientBoostingClassifier(random_state%3D0)%0A%0A%20%20%20%20def%20stage_a_feature(X%2C%20y%2C%20how)%3A%0A%20%20%20%20%20%20%20%20%23%20Stage%20A's%20probability%20on%20its%20own%20training%20rows%3A%20refitted%20on%20them%20(in-sample)%2C%20or%20cross-fitted%20(out%20of%20fold).%0A%20%20%20%20%20%20%20%20if%20how%20%3D%3D%20%22in-sample%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20clone(stage_a).fit(X%2C%20y).predict_proba(X)%5B%3A%2C%201%5D%0A%20%20%20%20%20%20%20%20return%20cross_val_predict(stage_a%2C%20X%2C%20y%2C%20cv%3D5%2C%20method%3D%22predict_proba%22)%5B%3A%2C%201%5D%0A%0A%20%20%20%20for%20how%20in%20(%22in-sample%22%2C%20%22out-of-fold%22)%3A%0A%20%20%20%20%20%20%20%20%23%20Naive%3A%20build%20the%20feature%20once%20on%20every%20row%2C%20then%20cross-validate%20stage%20B%20on%20the%20same%20rows.%0A%20%20%20%20%20%20%20%20feat%20%3D%20stage_a_feature(Xa%2C%20cropland%2C%20how)%0A%20%20%20%20%20%20%20%20_naive%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20cross_val_predict(stage_b%2C%20np.column_stack(%5BXa%2C%20feat%5D)%2C%20maize%2C%20cv%3D5)%20%3D%3D%20maize%0A%20%20%20%20%20%20%20%20).mean()%0A%20%20%20%20%20%20%20%20%23%20Nested%3A%20every%20outer%20fold%20rebuilds%20stage%20A%20from%20its%20own%20training%20rows%20only.%0A%20%20%20%20%20%20%20%20nested%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20tr%2C%20va%20in%20KFold(5%2C%20shuffle%3DTrue%2C%20random_state%3D0).split(Xa)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20f_tr%20%3D%20stage_a_feature(Xa%5Btr%5D%2C%20cropland%5Btr%5D%2C%20how)%0A%20%20%20%20%20%20%20%20%20%20%20%20f_va%20%3D%20clone(stage_a).fit(Xa%5Btr%5D%2C%20cropland%5Btr%5D).predict_proba(Xa%5Bva%5D)%5B%3A%2C%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20b%20%3D%20clone(stage_b).fit(np.column_stack(%5BXa%5Btr%5D%2C%20f_tr%5D)%2C%20maize%5Btr%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20nested.append((b.predict(np.column_stack(%5BXa%5Bva%5D%2C%20f_va%5D))%20%3D%3D%20maize%5Bva%5D).mean())%0A%20%20%20%20%20%20%20%20f_new%20%3D%20clone(stage_a).fit(Xa%2C%20cropland).predict_proba(Xnew)%5B%3A%2C%201%5D%0A%20%20%20%20%20%20%20%20fresh%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20clone(stage_b)%0A%20%20%20%20%20%20%20%20%20%20%20%20.fit(np.column_stack(%5BXa%2C%20feat%5D)%2C%20maize)%0A%20%20%20%20%20%20%20%20%20%20%20%20.predict(np.column_stack(%5BXnew%2C%20f_new%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20%3D%3D%20maize_new%0A%20%20%20%20%20%20%20%20).mean()%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bhow%3A12%7D%20stage%20A%3A%20naive%20CV%20%7B_naive%3A.3f%7D%20%7C%20nested%20CV%20%7Bnp.mean(nested)%3A.3f%7D%20%7C%20new%20data%20%7Bfresh%3A.3f%7D%22%0A%20%20%20%20%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%20Each%20arm%20is%20scored%20three%20ways%3A%20the%20**naive**%20cross-validation%20a%20pipeline%20would%20report%20(stage-A%20features%20built%20once%20on%20every%20row%2C%20then%20stage%20B%20cross-validated)%2C%20a%20**nested**%20cross-validation%20that%20rebuilds%20stage%20A%20from%20each%20outer%20fold's%20training%20rows%20only%2C%20and%20fresh%20data.%20With%20in-sample%20stage-A%20features%2C%20the%20naive%20score%20(0.877)%20promises%20far%20more%20than%20the%20stack%20delivers%20on%20new%20data%20(0.770)%2C%20and%20the%20stack%20itself%20is%20worse%2C%20because%20stage%20B%20learned%20to%20trust%20a%20feature%20that%20is%20too%20good%20on%20its%20training%20rows.%20Out-of-fold%20features%20give%20a%20better%20stack%20(0.803%20on%20new%20data).%20And%20only%20the%20nested%20score%20tracks%20new%20data%20for%20both%20arms%20(0.769%20and%200.814)%3A%20computing%20out-of-fold%20features%20once%20and%20then%20cross-validating%20stage%20B%20on%20the%20same%20rows%20still%20lets%20stage%20A's%20training%20see%20the%20rows%20stage%20B%20is%20validated%20on%2C%20an%20indirect%20leak%20that%20happens%20to%20be%20small%20here%20(0.804).%20Stacking's%20feature%20cross-validation%20and%20the%20model's%20evaluation%20cross-validation%20are%20separate%20loops%2C%20as%20scikit-learn's%20%5B%60StackingClassifier%60%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fgenerated%2Fsklearn.ensemble.StackingClassifier.html)%20builds%20them.%0A%0A%20%20%20%20Finding%20F11%20(%5Bissue%2038%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F38))%20is%20the%20first%20mistake%20in%20production%3A%20the%20stage-B%20%60p_cropland%60%20is%20cross-fitted%20only%20for%20stage%20A's%20own%20rows%2C%20and%20on%20the%20maize%20rows%20it%20is%20the%20full%20model's%20prediction.%0A%20%20%20%20%22%22%22)%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%20Part%206%3A%20segmentation%2C%20a%20class%20for%20every%20pixel%0A%0A%20%20%20%20A%20temporal%20transformer%20classifies%20one%20pixel%20at%20a%20time%20from%20its%20history.%20A%20**segmentation**%20network%20classifies%20every%20pixel%20of%20an%20image%20at%20once%20from%20its%20spatial%20neighbourhood.%20tf-deep-landcover's%20sugar%20India%20model%20is%20a%20U-Net%20over%20AlphaEarth%20embeddings%3A%20an%20encoder%20of%20convolution%2C%20BatchNorm%20and%20GELU%20blocks%20that%20halves%20the%20resolution%20four%20times%20(%5B%60unet_encoder.py%60%20L20-91%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fsrc%2Fstages%2Ftrain%2Fbackbones%2Funet_encoder.py%23L20-L91))%2C%20a%20decoder%20that%20upsamples%20back%2C%20and%20a%20masked%20focal%20loss.%20%60code.md%60%20section%2010%20walks%20through%20it.%0A%0A%20%20%20%20%23%23%206.1%20Masks%2C%20NaN%20and%20a%20loss%20of%20exactly%20zero%0A%0A%20%20%20%20Two%20traps%2C%20both%20recorded%20in%20tf-deep-landcover's%20code%20comments.%20First%2C%20one%20NaN%20input%20pixel%3A%20convolutions%20spread%20it%20to%20its%20neighbours%2C%20and%20BatchNorm%2C%20which%20in%20training%20averages%20over%20the%20whole%20batch%2C%20spreads%20it%20to%20every%20image.%20Second%2C%20a%20NaN%20at%20a%20pixel%20the%20mask%20excludes%3A%20NaN%20%C3%97%200%20is%20NaN%2C%20so%20masking%20after%20the%20fact%20doesn't%20remove%20it.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s6_1_masks_nan_loss()%3A%0A%20%20%20%20torch.manual_seed(0)%0A%20%20%20%20img%20%3D%20torch.rand(4%2C%203%2C%2016%2C%2016)%0A%20%20%20%20img%5B0%2C%20%3A%2C%205%2C%205%5D%20%3D%20float(%22nan%22)%20%20%23%20one%20bad%20pixel%20in%20one%20image%20of%20the%20batch%0A%20%20%20%20_net%20%3D%20nn.Sequential(%0A%20%20%20%20%20%20%20%20nn.Conv2d(3%2C%208%2C%203%2C%20padding%3D1)%2C%20nn.BatchNorm2d(8)%2C%20nn.GELU()%2C%20nn.Conv2d(8%2C%202%2C%203%2C%20padding%3D1)%0A%20%20%20%20)%0A%20%20%20%20_net.train()%0A%20%20%20%20_out%20%3D%20_net(img)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22NaN%20logits%20per%20image%20after%202%20convs%20%2B%20BatchNorm%20(training%20mode)%3A%22%2C%0A%20%20%20%20%20%20%20%20torch.isnan(_out).flatten(1).any(1).tolist()%2C%0A%20%20%20%20)%0A%20%20%20%20labels%20%3D%20torch.zeros(4%2C%2016%2C%2016%2C%20dtype%3Dtorch.long)%0A%20%20%20%20per_px%20%3D%20F.cross_entropy(_out%2C%20labels%2C%20reduction%3D%22none%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22loss%20without%20scrubbing%3A%22%2C%0A%20%20%20%20%20%20%20%20float(per_px.mean().detach())%2C%0A%20%20%20%20%20%20%20%20%22%7C%20after%20nan_to_num%3A%22%2C%0A%20%20%20%20%20%20%20%20float(torch.nan_to_num(per_px%2C%20nan%3D0.0).mean().detach())%2C%0A%20%20%20%20)%0A%20%20%20%20_logits%20%3D%20torch.randn(1%2C%202%2C%204%2C%204)%0A%20%20%20%20_logits%5B0%2C%20%3A%2C%200%2C%200%5D%20%3D%20float(%22nan%22)%0A%20%20%20%20_mask%20%3D%20torch.ones(1%2C%204%2C%204)%0A%20%20%20%20_mask%5B0%2C%200%2C%200%5D%20%3D%200%0A%20%20%20%20ce%20%3D%20F.cross_entropy(_logits%2C%20torch.zeros(1%2C%204%2C%204%2C%20dtype%3Dtorch.long)%2C%20reduction%3D%22none%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22NaN%20at%20a%20masked%20pixel%2C%20(loss%20*%20mask).sum()%3A%22%2C%0A%20%20%20%20%20%20%20%20float((ce%20*%20_mask).sum())%2C%0A%20%20%20%20%20%20%20%20%22%7C%20scrubbed%20first%3A%22%2C%0A%20%20%20%20%20%20%20%20round(float((torch.nan_to_num(ce)%20*%20_mask).sum())%2C%204)%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%20One%20bad%20pixel%20in%20one%20image%20makes%20**every**%20image's%20logits%20NaN.%20%60torch.nan_to_num%60%20then%20turns%20each%20pixel's%20NaN%20loss%20into%200%2C%20so%20the%20batch's%20loss%20is%20exactly%200%3A%20training%20%22succeeds%22%20while%20the%20BatchNorm%20running%20statistics%20are%20already%20poisoned.%20That%20is%20what%20happened%20to%20sugar%20India%20runs%20H2%E2%80%93H6%3A%2032%20windows%20of%209%2C542%20had%20NaN%20padding%20from%20one%20AlphaEarth%20tile%2C%20and%20every%20run%20collapsed%20in%20its%20first%20epoch%20whatever%20the%20loss%20settings%20(%5Bconfig%20comment%20L1-9%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fsrc%2Fconfigs%2Fmodels%2Fsugar%2Fsugar_india_8class_unet_v12.yaml%23L1-L9)).%20The%20fix%20was%20at%20the%20input%3A%20quarantine%20the%20windows%2C%20and%20zero%20non-finite%20inputs%20before%20the%20first%20convolution.%20The%20second%20line%20shows%20why%20the%20head%20scrubs%20NaN%20*before*%20multiplying%20by%20the%20mask.%0A%0A%20%20%20%20%23%23%206.2%20Which%20loss%20for%20a%20rare%20class%3F%0A%0A%20%20%20%20Sugarcane%20is%20under%2010%20%25%20of%20sugar%20India's%20pixels%20and%20water%20under%201%20%25.%20The%20usual%20remedies%20are%20**class%20weights**%20(count%20a%20rare%20pixel%20more)%20and%20**focal%20loss**%20(count%20a%20confidently%20right%20pixel%20less).%20Train%20a%20small%20segmentation%20network%20on%20synthetic%20images%20with%20about%203.5%20%25%20foreground%2C%20with%20tf-deep-landcover's%20masked%20loss%2C%20four%20ways%2C%20on%20three%20seeds%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s6_2_loss_rare_class()%3A%0A%20%20%20%20def%20blobs(seed%2C%20n%3D48%2C%20size%3D64%2C%20share%3D0.03)%3A%0A%20%20%20%20%20%20%20%20g%20%3D%20torch.Generator().manual_seed(seed)%0A%20%20%20%20%20%20%20%20yy%2C%20xx%20%3D%20torch.meshgrid(torch.arange(size)%2C%20torch.arange(size)%2C%20indexing%3D%22ij%22)%0A%20%20%20%20%20%20%20%20images%2C%20labels%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20target%20%3D%20torch.zeros(size%2C%20size%2C%20dtype%3Dtorch.long)%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20target.float().mean()%20%3C%20share%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cy%2C%20cx%20%3D%20torch.randint(4%2C%20size%20-%204%2C%20(2%2C)%2C%20generator%3Dg)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r%20%3D%20int(torch.randint(2%2C%205%2C%20(1%2C)%2C%20generator%3Dg))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20target%5B((yy%20-%20cy)%20**%202%20%2B%20(xx%20-%20cx)%20**%202)%20%3C%3D%20r%20*%20r%5D%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20signal%20%3D%20target.float()%20*%200.6%0A%20%20%20%20%20%20%20%20%20%20%20%20image%20%3D%20torch.stack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bsignal%20%2B%200.5%20*%20torch.randn(size%2C%20size%2C%20generator%3Dg)%20for%20_%20in%20range(3)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20images.append(image)%0A%20%20%20%20%20%20%20%20%20%20%20%20labels.append(target)%0A%20%20%20%20%20%20%20%20return%20torch.stack(images)%2C%20torch.stack(labels)%0A%0A%20%20%20%20def%20masked_focal(logits%2C%20labels%2C%20valid%2C%20gamma%3D0.0%2C%20class_weights%3DNone)%3A%0A%20%20%20%20%20%20%20%20%23%20The%20masked%20loss%20of%20tf-deep-landcover's%20FocalSegmentationHead%20(focal_head.py%20L66-93)%2C%20as%20a%20function.%0A%20%20%20%20%20%20%20%20safe%20%3D%20labels.clamp(0%2C%20logits.shape%5B1%5D%20-%201)%0A%20%20%20%20%20%20%20%20ce%20%3D%20F.cross_entropy(logits%2C%20safe%2C%20reduction%3D%22none%22)%0A%20%20%20%20%20%20%20%20if%20gamma%20%3E%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20p_t%20%3D%20logits.softmax(1).gather(1%2C%20safe.unsqueeze(1)).squeeze(1).clamp(1e-7%2C%201%20-%201e-7)%0A%20%20%20%20%20%20%20%20%20%20%20%20ce%20%3D%20(1%20-%20p_t)%20**%20gamma%20*%20ce%0A%20%20%20%20%20%20%20%20if%20class_weights%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ce%20%3D%20ce%20*%20class_weights%5Bsafe%5D%0A%20%20%20%20%20%20%20%20ce%20%3D%20torch.nan_to_num(ce%2C%20nan%3D0.0%2C%20posinf%3D0.0%2C%20neginf%3D0.0)%0A%20%20%20%20%20%20%20%20return%20(ce%20*%20valid).sum()%20%2F%20valid.sum().clamp_min(1)%0A%0A%20%20%20%20def%20small_net()%3A%0A%20%20%20%20%20%20%20%20return%20nn.Sequential(%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.Conv2d(3%2C%2016%2C%203%2C%20padding%3D1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.BatchNorm2d(16)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.GELU()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.Conv2d(16%2C%2016%2C%203%2C%20padding%3D1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.BatchNorm2d(16)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.GELU()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nn.Conv2d(16%2C%202%2C%201)%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20fit_seg(seed%2C%20gamma%3D0.0%2C%20weights%3DNone%2C%20steps%3D150)%3A%0A%20%20%20%20%20%20%20%20torch.manual_seed(seed)%0A%20%20%20%20%20%20%20%20X%2C%20Y%20%3D%20blobs(seed)%0A%20%20%20%20%20%20%20%20net%20%3D%20small_net()%0A%20%20%20%20%20%20%20%20opt%20%3D%20torch.optim.AdamW(net.parameters()%2C%20lr%3D3e-3)%0A%20%20%20%20%20%20%20%20valid%20%3D%20torch.ones_like(Y%2C%20dtype%3Dtorch.float32)%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(steps)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20idx%20%3D%20torch.randint(0%2C%20len(X)%2C%20(16%2C))%0A%20%20%20%20%20%20%20%20%20%20%20%20loss%20%3D%20masked_focal(net(X%5Bidx%5D)%2C%20Y%5Bidx%5D%2C%20valid%5Bidx%5D%2C%20gamma%2C%20weights)%0A%20%20%20%20%20%20%20%20%20%20%20%20opt.zero_grad()%0A%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%20opt.step()%0A%20%20%20%20%20%20%20%20Xt%2C%20Yt%20%3D%20blobs(seed%20%2B%20100%2C%20n%3D16)%0A%20%20%20%20%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20pred%20%3D%20net.eval()(Xt).argmax(1)%0A%20%20%20%20%20%20%20%20tp%20%3D%20((pred%20%3D%3D%201)%20%26%20(Yt%20%3D%3D%201)).sum().item()%0A%20%20%20%20%20%20%20%20fp%20%3D%20((pred%20%3D%3D%201)%20%26%20(Yt%20%3D%3D%200)).sum().item()%0A%20%20%20%20%20%20%20%20fn%20%3D%20((pred%20%3D%3D%200)%20%26%20(Yt%20%3D%3D%201)).sum().item()%0A%20%20%20%20%20%20%20%20return%20tp%20%2F%20max(tp%20%2B%20fp%20%2B%20fn%2C%201)%2C%20net%0A%0A%20%20%20%20share%20%3D%20blobs(0)%5B1%5D.float().mean().item()%0A%20%20%20%20print(f%22foreground%20share%20%7Bshare%3A.3f%7D%22)%0A%20%20%20%20inv%20%3D%20torch.tensor(%5B1%20%2F%20(1%20-%20share)%2C%201%20%2F%20share%5D)%0A%20%20%20%20inv%20%3D%20inv%20%2F%20inv.mean()%0A%20%20%20%20_arms%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22cross-entropy%22%3A%20(0.0%2C%20None)%2C%0A%20%20%20%20%20%20%20%20%22class-weighted%22%3A%20(0.0%2C%20inv)%2C%0A%20%20%20%20%20%20%20%20%22focal%20%CE%B3%3D1%22%3A%20(1.0%2C%20None)%2C%0A%20%20%20%20%20%20%20%20%22focal%20%CE%B3%3D2%22%3A%20(2.0%2C%20None)%2C%0A%20%20%20%20%7D%0A%20%20%20%20t%20%3D%20time.perf_counter()%0A%20%20%20%20iou%20%3D%20%7Bname%3A%20%5Bfit_seg(s%2C%20g%2C%20w)%5B0%5D%20for%20s%20in%20(0%2C%201%2C%202)%5D%20for%20name%2C%20(g%2C%20w)%20in%20_arms.items()%7D%0A%20%20%20%20for%20_name%2C%20_v%20in%20iou.items()%3A%0A%20%20%20%20%20%20%20%20print(f%22%7B_name%3A15%7D%20foreground%20IoU%20per%20seed%20%7Bnp.round(_v%2C%203)%7D%20mean%20%7Bnp.mean(_v)%3A.3f%7D%22)%0A%20%20%20%20print(f%2212%20fits%20in%20%7Btime.perf_counter()%20-%20t%3A.0f%7D%20s%22)%0A%20%20%20%20return%20blobs%2C%20fit_seg%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%20On%20this%20data%20neither%20remedy%20helps.%20Focal%20loss%20with%20%CE%B3%20%3D%201%20or%202%20matches%20plain%20cross-entropy%20(foreground%20IoU%200.866%20to%200.870%2C%20the%20seeds%20agreeing)%2C%20and%20inverse-frequency%20class%20weights%20*lower*%20it%2C%20to%200.760%3A%20weighting%20the%20rare%20class%20heavily%20pushes%20the%20network%20to%20call%20borderline%20pixels%20foreground%2C%20which%20adds%20false%20positives%20that%20IoU%20punishes.%20That%20matches%20tf-deep-landcover's%20own%20modelling%20guide%2C%20which%20cites%20focal%20loss%20as%20the%20worst%20performer%20at%204.8%20%25%20foreground%20(Yeung%20et%20al.%2C%202022)%20and%20records%20that%20a%20coffee%20run%20with%201.3%20%25%20positives%20was%20fixed%20by%20data%20selection%2C%20not%20by%20the%20loss.%20In%20production%2C%20cocoa%20trains%20with%20%CE%B3%20%3D%200%20and%20sugar%20India%20with%20%CE%B3%20%3D%201%20plus%20gentle%20weights%20(0.26%20to%202.15).%0A%0A%20%20%20%20%23%23%206.3%20Wall%20to%20wall%3A%20tiles%2C%20seams%20and%20cores%0A%0A%20%20%20%20A%20map%20is%20far%20bigger%20than%20a%20training%20patch%2C%20so%20inference%20runs%20tile%20by%20tile.%20A%20convolution%20near%20a%20tile's%20edge%20sees%20zero%20padding%20instead%20of%20the%20real%20neighbours%2C%20so%20a%20pixel%20near%20an%20edge%20can%20be%20classified%20differently%20depending%20on%20where%20the%20tile%20boundary%20fell.%20model-foundry's%20%60segmentation%60%20package%20cuts%20**overlapping**%20tiles%20and%20keeps%20only%20each%20tile's%20core%20(%5B%60spans%60%2C%20model.py%20L80-84%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fsegmentation%2Fsrc%2Fsegmentation%2Fmodel.py%23L80-L84)%2C%20copied%20below)%2C%20so%20every%20kept%20pixel%20is%20far%20from%20an%20edge%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s6_3_wall_wall_tiles(blobs%2C%20fit_seg)%3A%0A%20%20%20%20TILE%2C%20OVERLAP%20%3D%2064%2C%2016%0A%0A%20%20%20%20def%20spans(n%3A%20int%2C%20size%3A%20int%20%3D%20TILE%2C%20overlap%3A%20int%20%3D%20OVERLAP)%3A%0A%20%20%20%20%20%20%20%20starts%20%3D%20%5B0%5D%20if%20n%20%3C%3D%20size%20else%20%5B*range(0%2C%20n%20-%20size%2C%20size%20-%20overlap)%2C%20n%20-%20size%5D%0A%20%20%20%20%20%20%20%20cuts%20%3D%20%5B0%2C%20*((a%20%2B%20size%20%2B%20b)%20%2F%2F%202%20for%20a%2C%20b%20in%20zip(starts%2C%20starts%5B1%3A%5D%2C%20strict%3DFalse))%2C%20n%5D%0A%20%20%20%20%20%20%20%20return%20%5B(slice(s%2C%20s%20%2B%20size)%2C%20slice(cuts%5Bi%5D%2C%20cuts%5Bi%20%2B%201%5D))%20for%20i%2C%20s%20in%20enumerate(starts)%5D%0A%0A%20%20%20%20_%2C%20_net%20%3D%20fit_seg(0)%0A%20%20%20%20big_X%2C%20_%20%3D%20blobs(7%2C%20n%3D1%2C%20size%3D160)%0A%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20whole%20%3D%20_net(big_X)%5B0%5D.argmax(0)%0A%20%20%20%20%20%20%20%20_naive%20%3D%20torch.zeros_like(whole)%0A%20%20%20%20%20%20%20%20for%20y0%20in%20range(0%2C%20160%2C%20TILE)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20x0%20in%20range(0%2C%20160%2C%20TILE)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_naive%5By0%20%3A%20y0%20%2B%20TILE%2C%20x0%20%3A%20x0%20%2B%20TILE%5D%20%3D%20_net(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20big_X%5B%3A%2C%20%3A%2C%20y0%20%3A%20y0%20%2B%20TILE%2C%20x0%20%3A%20x0%20%2B%20TILE%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%5B0%5D.argmax(0)%0A%20%20%20%20%20%20%20%20cored%20%3D%20torch.zeros_like(whole)%0A%20%20%20%20%20%20%20%20for%20ys%2C%20ycore%20in%20spans(160)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20xs%2C%20xcore%20in%20spans(160)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20p%20%3D%20_net(big_X%5B%3A%2C%20%3A%2C%20ys%2C%20xs%5D)%5B0%5D.argmax(0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cored%5Bycore%2C%20xcore%5D%20%3D%20p%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ycore.start%20-%20ys.start%20%3A%20ycore.stop%20-%20ys.start%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20xcore.start%20-%20xs.start%20%3A%20xcore.stop%20-%20xs.start%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22pixels%20that%20differ%20from%20one%20whole-image%20pass%3A%20tiles%20without%20overlap%22%2C%0A%20%20%20%20%20%20%20%20int((_naive%20!%3D%20whole).sum())%2C%0A%20%20%20%20%20%20%20%20%22%7C%20overlapping%20tiles%2C%20cores%20kept%22%2C%0A%20%20%20%20%20%20%20%20int((cored%20!%3D%20whole).sum())%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22spans(160)%3A%22%2C%20%5B(s.start%2C%20s.stop%2C%20c.start%2C%20c.stop)%20for%20s%2C%20c%20in%20spans(160)%5D)%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%20Tiles%20that%20simply%20abut%20give%2021%20pixels%20that%20a%20single%20whole-image%20pass%20would%20classify%20differently%2C%20all%20along%20the%20seams%3B%20overlapping%20tiles%20whose%20cores%20tile%20the%20image%20exactly%20give%20none.%20tf-deep-landcover's%20chunked%20inference%20instead%20crops%20each%20chunk's%20padding%20and%20writes%20chunks%20side%20by%20side%20(%5B%60infer%2Fprocess.py%60%20L451-462%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fsrc%2Fstages%2Finfer%2Fprocess.py%23L451-L462))%2C%20and%20its%20UTM-zone%20mosaics%20disagree%20on%2092%E2%80%9398%20%25%20of%20overlap%20pixels%20between%20adjacent%20zones%20(unit%201.1's%20planned%20section).%0A%20%20%20%20%22%22%22)%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%20Part%207%3A%20learning%20from%20imperfect%20labels%0A%0A%20%20%20%20Crop%20maps%20are%20trained%20on%20labels%20that%20are%20themselves%20maps%3A%20a%20teacher%20model%2C%20an%20older%20product%2C%20a%20survey.%20They%20are%20wrong%20in%20places%2C%20and%20wrong%20most%20often%20where%20the%20classes%20are%20hard%20to%20tell%20apart.%20Three%20production%20answers%3A%20refuse%20to%20label%20what%20the%20sources%20dispute%2C%20label%20only%20what%20several%20years%20agree%20on%2C%20and%20weight%20each%20label%20by%20how%20far%20it%20can%20be%20trusted.%0A%0A%20%20%20%20%23%23%207.1%20Abstain%20rather%20than%20force%0A%0A%20%20%20%20tf-deep-landcover's%20label-fusion%20guide%3A%20%22If%20the%20sources%20disagree%20or%20the%20pixel%20is%20obviously%20ambiguous%2C%20set%20it%20to%20%60255%60%20and%20exclude%20it%20from%20training.%20Do%20not%20force%20the%20pixel%20into%20the%20target%20class%22%20(%5Blabel_fusion.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fdocs%2Fguides%2Flabel_fusion.md)).%20Simulate%20two%20label%20sources%20that%20are%20wrong%2030%20%25%20of%20the%20time%20near%20the%20class%20boundary%20and%205%20%25%20elsewhere%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s7_1_abstain_rather_than()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(3)%0A%20%20%20%20_n%20%3D%206000%0A%20%20%20%20Xl%20%3D%20_rng.normal(size%3D(_n%2C%205))%0A%20%20%20%20truth%20%3D%20(Xl%5B%3A%2C%200%5D%20%2B%200.5%20*%20Xl%5B%3A%2C%201%5D%20%3E%200).astype(int)%0A%20%20%20%20margin%20%3D%20np.abs(Xl%5B%3A%2C%200%5D%20%2B%200.5%20*%20Xl%5B%3A%2C%201%5D)%0A%20%20%20%20p_flip%20%3D%20np.where(margin%20%3C%200.5%2C%200.30%2C%200.05)%20%20%23%20disputed%20pixels%20sit%20near%20the%20boundary%0A%20%20%20%20src_a%20%3D%20np.where(_rng.random(_n)%20%3C%20p_flip%2C%201%20-%20truth%2C%20truth)%0A%20%20%20%20src_b%20%3D%20np.where(_rng.random(_n)%20%3C%20p_flip%2C%201%20-%20truth%2C%20truth)%0A%20%20%20%20print(f%22source%20A%20label%20error%20%7Bnp.mean(src_a%20!%3D%20truth)%3A.3f%7D%22)%0A%20%20%20%20Xt%20%3D%20_rng.normal(size%3D(4000%2C%205))%0A%20%20%20%20yt%20%3D%20(Xt%5B%3A%2C%200%5D%20%2B%200.5%20*%20Xt%5B%3A%2C%201%5D%20%3E%200).astype(int)%0A%20%20%20%20agree%20%3D%20src_a%20%3D%3D%20src_b%0A%20%20%20%20for%20_name%2C%20keep%2C%20_y%20in%20(%0A%20%20%20%20%20%20%20%20(%22forced%3A%20source%20A%20everywhere%22%2C%20np.ones(_n%2C%20bool)%2C%20src_a)%2C%0A%20%20%20%20%20%20%20%20(%22abstain%20where%20sources%20disagree%20(255)%22%2C%20agree%2C%20src_a)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20m%20%3D%20HistGradientBoostingClassifier(random_state%3D0).fit(Xl%5Bkeep%5D%2C%20_y%5Bkeep%5D)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_name%3A40%7D%20trains%20on%20%7Bkeep.mean()%3A.0%25%7D%20of%20pixels%2C%20label%20error%20%7Bnp.mean(_y%5Bkeep%5D%20!%3D%20truth%5Bkeep%5D)%3A.3f%7D%2C%20clean%20test%20accuracy%20%7Bm.score(Xt%2C%20yt)%3A.3f%7D%22%0A%20%20%20%20%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%20Dropping%20the%20fifth%20of%20pixels%20where%20the%20two%20sources%20disagree%20cuts%20the%20training%20labels'%20error%20rate%20from%200.139%20to%200.044%2C%20and%20the%20model%20trained%20on%20fewer%2C%20cleaner%20labels%20is%20more%20accurate%20on%20clean%20test%20data.%20The%20opposite%20move%20is%20costly%3A%20tf-deep-landcover's%20Vietnam%20V2%20burned%20a%20post-processing%20mask%20into%20its%20training%20labels%2C%20and%20core-belt%20recall%20fell%20from%200.92%20to%200.72%2C%20because%20the%20mask's%20%22uncertain%22%20classes%20overlapped%20real%20coffee.%0A%0A%20%20%20%20%23%23%207.2%20Consensus%20pseudo-labels%0A%0A%20%20%20%20Where%20there%20are%20no%20labels%20at%20all%2C%20tf-deep-landcover%20builds%20**pseudo-labels**%20from%20its%20own%20yearly%20probability%20maps%20(%5B%60build_v17b_pseudo_labels.py%60%20L158-201%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fsrc%2Fstages%2Fsample%2Fbuild_v17b_pseudo_labels.py%23L158-L201))%3A%20a%20pixel%20is%20the%20target%20crop%20if%20its%20probability%20exceeds%20%CF%84%20in%20**at%20least%203**%20of%20the%20years%2C%20background%20if%20it%20exceeds%20%CF%84%20in%20**none**%2C%20and%20255%20(abstain)%20otherwise.%20Simulate%20five%20noisy%20yearly%20maps%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s7_2_consensus_pseudo_labels()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(4)%0A%20%20%20%20size%20%3D%20200%0A%20%20%20%20crop%20%3D%20np.zeros((size%2C%20size)%2C%20bool)%0A%20%20%20%20for%20_%20in%20range(40)%3A%0A%20%20%20%20%20%20%20%20cy%2C%20cx%20%3D%20_rng.integers(0%2C%20size%2C%202)%0A%20%20%20%20%20%20%20%20crop%5Bmax(cy%20-%206%2C%200)%20%3A%20cy%20%2B%206%2C%20max(cx%20-%206%2C%200)%20%3A%20cx%20%2B%206%5D%20%3D%20True%0A%20%20%20%20_years%20%3D%20%5B%0A%20%20%20%20%20%20%20%20np.clip(np.where(crop%2C%200.7%2C%200.2)%20%2B%20_rng.normal(0%2C%200.2%2C%20crop.shape)%2C%200%2C%201)%20for%20_%20in%20range(5)%0A%20%20%20%20%5D%0A%20%20%20%20tau%20%3D%200.5%0A%20%20%20%20count%20%3D%20sum((p%20%3E%20tau).astype(np.uint8)%20for%20p%20in%20_years)%0A%20%20%20%20pseudo%20%3D%20np.full(crop.shape%2C%20255%2C%20np.uint8)%0A%20%20%20%20pseudo%5Bcount%20%3E%3D%203%5D%20%3D%201%0A%20%20%20%20pseudo%5Bcount%20%3D%3D%200%5D%20%3D%200%0A%20%20%20%20single%20%3D%20(_years%5B0%5D%20%3E%20tau).astype(np.uint8)%0A%0A%20%20%20%20def%20label_quality(labels)%3A%0A%20%20%20%20%20%20%20%20lab%20%3D%20labels%20!%3D%20255%0A%20%20%20%20%20%20%20%20return%20lab.mean()%2C%20np.mean(labels%5Blab%5D%20%3D%3D%20crop%5Blab%5D)%0A%0A%20%20%20%20cov%2C%20acc%20%3D%20label_quality(single)%0A%20%20%20%20print(f%22one%20year%2C%20p%20%3E%20%7Btau%7D%3A%20labels%20%7Bcov%3A.0%25%7D%20of%20pixels%2C%20%7Bacc%3A.3f%7D%20correct%22)%0A%20%20%20%20cov%2C%20acc%20%3D%20label_quality(pseudo)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22consensus%20%E2%89%A53%20of%205%20(1)%2C%200%20of%205%20(0)%2C%20else%20255%3A%20labels%20%7Bcov%3A.0%25%7D%20of%20pixels%2C%20%7Bacc%3A.3f%7D%20correct%3B%20crop%20recall%20%7Bnp.mean(pseudo%5Bcrop%5D%20%3D%3D%201)%3A.3f%7D%22%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%20One%20year's%20map%20labels%20every%20pixel%20and%20gets%208%20%25%20of%20them%20wrong.%20The%20consensus%20labels%20only%20three-quarters%20of%20the%20pixels%20but%20is%20right%20on%2099.7%20%25%20of%20those%2C%20and%20still%20keeps%2097%20%25%20of%20the%20crop.%20The%20abstained%20quarter%20is%20where%20the%20years%20disagreed%3A%20exactly%20the%20pixels%20a%20model%20would%20learn%20noise%20from.%0A%0A%20%20%20%20%23%23%207.3%20Trust%20weights%2C%20distillation%20and%20calibration%0A%0A%20%20%20%20%60maize_presence_aef%60%20learns%20from%20a%20**teacher**%3A%20an%20existing%20crop-type%20map%2C%20whose%20labels%20it%20distils%20into%20a%20model%20on%20embeddings.%20Not%20every%20label%20is%20equally%20trusted%20(%5B%60config.py%60%20L37-44%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_presence_aef%2Fsrc%2Fmaize_presence_aef%2Fconfig.py%23L37-L44))%3A%20a%20surveyed%20field%20polygon%20weighs%203.0%2C%20a%20pixel%20where%20the%20teacher's%20years%20agree%201.0%2C%20a%20single-year%20teacher%20label%200.6%2C%20and%20a%20teacher%20negative%20in%20a%20season%20it%20didn't%20map%200.15.%20%60maize_subannual%60%20uses%20the%20same%20idea%20in%20time%3A%20pretrain%20on%20teacher%20labels%2C%20then%20fine-tune%20on%20field%20polygons%2C%20which%20raised%20field%20maize-F1%20from%200.667%20to%200.711%20but%20lowered%20the%20teacher-test%20score%20from%200.593%20to%200.498%2C%20so%20it%20may%20cost%20accuracy%20away%20from%20the%20surveyed%20fields%20(%5BMODEL_CARD.md%20L93-95%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_subannual%2FMODEL_CARD.md%23L93-L95)).%0A%0A%20%20%20%20A%20model%20trained%20on%20weighted%2C%20imperfect%20labels%20gives%20scores%2C%20not%20probabilities.%20%60maize_presence_aef%60%20therefore%20**calibrates**%20its%20out-of-fold%20scores%20with%20isotonic%20regression%20on%20teacher%20rows%20before%20choosing%20a%20threshold%20(%5B%60train.py%60%20L218-227%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fmaize_presence_aef%2Fsrc%2Fmaize_presence_aef%2Ftrain.py%23L218-L227))%3A%20a%20monotone%20map%20from%20score%20to%20observed%20frequency.%20Simulate%20an%20over-confident%20score%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s7_3_trust_weights_distillation()%3A%0A%20%20%20%20from%20sklearn.isotonic%20import%20IsotonicRegression%0A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(5)%0A%20%20%20%20score_oof%20%3D%20_rng.random(5000)%0A%20%20%20%20truth_p%20%3D%20(%0A%20%20%20%20%20%20%20%20score_oof**2.5%0A%20%20%20%20)%20%20%23%20an%20over-confident%20score%3A%20true%20frequency%20is%20much%20lower%20than%20the%20score%20says%0A%20%20%20%20_y%20%3D%20(_rng.random(5000)%20%3C%20truth_p).astype(int)%0A%0A%20%20%20%20def%20ece(p%2C%20y%2C%20bins%3D10)%3A%0A%20%20%20%20%20%20%20%20edges%20%3D%20np.linspace(0%2C%201%2C%20bins%20%2B%201)%0A%20%20%20%20%20%20%20%20idx%20%3D%20np.clip(np.digitize(p%2C%20edges)%20-%201%2C%200%2C%20bins%20-%201)%0A%20%20%20%20%20%20%20%20return%20sum(%0A%20%20%20%20%20%20%20%20%20%20%20%20abs(p%5Bidx%20%3D%3D%20b%5D.mean()%20-%20y%5Bidx%20%3D%3D%20b%5D.mean())%20*%20np.mean(idx%20%3D%3D%20b)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20b%20in%20range(bins)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20np.any(idx%20%3D%3D%20b)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20iso%20%3D%20IsotonicRegression(out_of_bounds%3D%22clip%22%2C%20y_min%3D0.0%2C%20y_max%3D1.0).fit(%0A%20%20%20%20%20%20%20%20score_oof%5B%3A2500%5D%2C%20_y%5B%3A2500%5D%0A%20%20%20%20)%0A%20%20%20%20cal%20%3D%20iso.predict(score_oof%5B2500%3A%5D)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22expected%20calibration%20error%20on%20held-out%20rows%3A%20raw%20%7Bece(score_oof%5B2500%3A%5D%2C%20_y%5B2500%3A%5D)%3A.3f%7D%20%7C%20isotonic%20%7Bece(cal%2C%20_y%5B2500%3A%5D)%3A.3f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22rows%20called%20maize%20at%200.5%3A%20raw%20%7Bnp.mean(score_oof%5B2500%3A%5D%20%3E%3D%200.5)%3A.2f%7D%20%7C%20isotonic%20%7Bnp.mean(cal%20%3E%3D%200.5)%3A.2f%7D%20%7C%20true%20share%20%7B_y%5B2500%3A%5D.mean()%3A.2f%7D%22%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%20Uncalibrated%2C%20a%200.5%20threshold%20calls%2049%20%25%20of%20rows%20maize%20when%2028%20%25%20are%3B%20after%20isotonic%20calibration%20on%20held-out%20rows%2C%2027%20%25.%20The%20expected%20calibration%20error%20(the%20average%20gap%20between%20stated%20probability%20and%20observed%20frequency%2C%20by%20bin)%20falls%20from%200.214%20to%200.033.%20The%20calibration%20rows%20must%20be%20out%20of%20fold%2C%20for%20the%20reason%20section%205.4%20gives.%20Production%20calibrates%20on%20teacher%20rows%20only%2C%20because%20the%20field%20survey's%2043%20%25%20maize%20share%20would%20otherwise%20set%20the%20threshold%20for%20a%20landscape%20that%20is%20mostly%20not%20maize.%0A%20%20%20%20%22%22%22)%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%20Part%208%3A%20exercises%0A%0A%20%20%20%20Change%20one%20thing%2C%20predict%20the%20result%2C%20then%20run%20it.%0A%0A%20%20%20%201.%20In%202.3%2C%20set%20the%20last%20two%20entries%20of%20pixel%200's%20%60attn_mask%60%20to%20%601%60.%20What%20happens%20to%20its%20pooling%20weights%2C%20and%20to%20its%20embedding%3F%20*Check%3A*%20the%20two%20padded%20weights%20become%20non-zero.%20The%20cell's%20own%20check%20still%20prints%20True%2C%20because%20the%20hand-run%20encoder%20and%20the%20module%20both%20see%20the%20new%20mask%3B%20to%20see%20the%20embedding%20change%2C%20keep%20a%20copy%20of%20%60reference%60%20from%20before%20the%20edit%20and%20compare%20the%20new%20%60_pooled%60%20with%20it.%0A%20%20%20%202.%20In%20Part%203%2C%20raise%20the%20cloud%20probability%20in%20the%20rainy%20season%20from%20%600.3%60%20to%20%600.6%60.%20Which%20prediction%20days%20lose%20the%20most%20accuracy%2C%20and%20why%20those%3F%20*Check%3A*%20the%20median%20pixel%20keeps%20fewer%20than%2041%20observations%3B%20compare%20the%20days%20that%20lose%20most%20with%20where%20the%20extra%20clouds%20fall.%0A%20%20%20%203.%20Part%203%20trained%20the%20LayerNorm%20fallback.%20Predict%20what%20happens%20if%20you%20also%20take%20away%20the%20one-cycle%20schedule%20(%60train(band_stats%3DFalse%2C%20one_cycle%3DFalse)%60)%2C%20then%20run%20it.%20*Check%3A*%20compare%20the%20final%20loss%20with%20ln%203%20%E2%89%88%201.099.%20Can%20any%20schedule%20rescue%20inputs%20that%20all%20look%20alike%3F%0A%20%20%20%204.%20In%203.1%2C%20plot%20the%20pooling%20weights%20for%20the%20**other%20crop**%20class%20instead.%20Where%20do%20they%20peak%3F%20*Check%3A*%20compare%20with%20its%20greenness%20peak%20near%20day%20150.%0A%20%20%20%205.%20In%204.2%2C%20keep%20the%20time%20encoding%20but%20zero%20the%20*values*%20(%60bands%60)%20instead.%20Which%20matters%20more%20for%20this%20synthetic%20data%2C%20*when*%20or%20*what*%3F%20*Check%3A*%20with%20no%20values%2C%20only%20the%20observation%20dates%20remain%2C%20and%20the%20generator%20draws%20them%20the%20same%20way%20for%20every%20class%2C%20so%20expect%20accuracy%20near%201%2F3.%0A%20%20%20%206.%20In%204.4%2C%20the%20oldest%20observation%20gives%20the%20date%20away.%20Start%20each%20pixel's%20observations%20at%20a%20random%20day%20between%200%20and%2060%20instead%20of%200%20to%204%2C%20retrain%20both%20arms%2C%20and%20see%20whether%20telling%20the%20model%20the%20date%20now%20helps.%20*Check%3A*%20compare%20the%20paired%20differences%20with%20the%20standard-error%20line.%0A%20%20%20%207.%20In%204.0%2C%20separate%20data%20from%20initialisation%3A%20call%20%60use_data(0)%60%20once%20and%20train%20with%20seeds%200%2C%201%20and%202.%20*Check%3A*%20the%20spread%20across%20seeds%20now%20comes%20from%20training%20randomness%20alone%20(initial%20weights%2C%20batch%20order%20and%20prediction%20days%2C%20dropout)%2C%20not%20from%20the%20data%20draw%3B%20compare%20it%20with%20the%20table's.%0A%0A%20%20%20%208.%20In%205.1%2C%20quantise%20linearly%20with%20255%20levels%20instead%20of%20127%20per%20side%20(%60np.round(v%20*%20127.5)%60)%2C%20and%20compare%20the%20error%20on%20components%20below%200.05%20again.%20*Check%3A*%20the%20signed%20square%20still%20wins%20on%20small%20components%3B%20why%20can't%20any%20linear%20code%3F%0A%20%20%20%209.%20In%206.2%2C%20give%20the%20masked%20loss%20a%20%60valid%60%20mask%20that%20excludes%20a%204-pixel%20border%20around%20every%20patch%2C%20and%20retrain%20cross-entropy.%20*Check%3A*%20the%20loss%20now%20divides%20by%20the%20valid%20pixels%20only%3B%20compare%20with%20%60F.cross_entropy(weight%3D...)%60%2C%20which%20divides%20by%20the%20summed%20class%20weights.%20Which%20normalisation%20keeps%20the%20loss's%20scale%20independent%20of%20how%20many%20rare%20pixels%20a%20batch%20holds%3F%0A%20%20%20%2010.%20In%207.2%2C%20require%204%20of%205%20years%20instead%20of%203.%20Predict%20the%20coverage%20and%20the%20accuracy%20before%20you%20run%20it.%20*Check%3A*%20coverage%20falls%2C%20and%20the%20crop's%20recall%20falls%20more%20than%20the%20accuracy%20rises.%0A%20%20%20%2011.%20In%204.5%2C%20isolate%20the%20warm-up%3A%20replace%20%60OneCycleLR%60%20with%20a%20%60LinearLR%60%20warm-up%20over%20the%20same%2010%20%25%20of%20steps%20followed%20by%20a%20constant%20rate%20(chain%20them%20with%20%60SequentialLR%60)%2C%20and%20retrain%20the%20three%20seeds.%20*Check%3A*%20if%20no%20run%20collapses%20on%20day%20180%2C%20the%20warm-up%20alone%20is%20enough%3B%20if%20one%20does%2C%20the%20annealing%20or%20momentum%20matters%20too.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20-%20PyTorch%3A%20%5B%60nn.Module%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.Module.html)%2C%20%5B%60nn.TransformerEncoderLayer%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.TransformerEncoderLayer.html)%2C%20%5B%60nn.TransformerEncoder%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.TransformerEncoder.html)%2C%20%5B%60nn.MultiheadAttention%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.MultiheadAttention.html)%0A%20%20%20%20-%20PyTorch%202.15%3A%20%5B%60nn.LayerNorm%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.LayerNorm.html)%2C%20%5B%60F.cross_entropy%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.functional.cross_entropy.html)%20(%60weight%3D%60%20and%20how%20%60reduction%3D'mean'%60%20divides%20by%20the%20summed%20weights)%2C%20%5B%60OneCycleLR%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.optim.lr_scheduler.OneCycleLR.html)%2C%20%5B%60Module.register_buffer%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.Module.html%23torch.nn.Module.register_buffer)%2C%20%5B%60nn.Embedding%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.nn.Embedding.html)%20and%20%5B%60torch.load%60%5D(https%3A%2F%2Fdocs.pytorch.org%2Fdocs%2F2.15%2Fgenerated%2Ftorch.load.html)%20(%60weights_only%60)%0A%20%20%20%20-%20scikit-learn%201.9%20%5B%60HistGradientBoostingClassifier%60%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fgenerated%2Fsklearn.ensemble.HistGradientBoostingClassifier.html)%2C%20including%20its%20native%20missing-value%20support%0A%20%20%20%20-%20Vaswani%20et%20al.%20(2017)%2C%20%22Attention%20Is%20All%20You%20Need%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1706.03762%0A%20%20%20%20-%20Liu%20et%20al.%20(2020)%2C%20%22On%20the%20Variance%20of%20the%20Adaptive%20Learning%20Rate%20and%20Beyond%22%20(why%20warm-up)%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1908.03265%0A%20%20%20%20-%20Ba%20et%20al.%20(2016)%2C%20%22Layer%20Normalization%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1607.06450%0A%20%20%20%20-%20Kazemi%20et%20al.%20(2019)%2C%20%22Time2Vec%3A%20Learning%20a%20Vector%20Representation%20of%20Time%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1907.05321%0A%20%20%20%20-%20Xiong%20et%20al.%20(2020)%2C%20%22On%20Layer%20Normalization%20in%20the%20Transformer%20Architecture%22%20(why%20%60norm_first%3DTrue%60)%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F2002.04745%0A%20%20%20%20-%20Ru%C3%9Fwurm%20and%20K%C3%B6rner%20(2020)%2C%20%22Self-attention%20for%20raw%20optical%20satellite%20time%20series%20classification%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1910.10536%0A%20%20%20%20-%20Jain%20and%20Wallace%20(2019)%2C%20%22Attention%20is%20not%20Explanation%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1902.10186%2C%20and%20Wiegreffe%20and%20Pinter%20(2019)%2C%20%22Attention%20is%20not%20not%20Explanation%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1908.04626%0A%20%20%20%20-%20Garnot%20et%20al.%20(2020)%2C%20%22Satellite%20Image%20Time%20Series%20Classification%20with%20Pixel-Set%20Encoders%20and%20Temporal%20Self-Attention%22%20(PSE-TAE)%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1911.07757%0A%20%20%20%20-%20Brown%20et%20al.%20(2025)%2C%20%22AlphaEarth%20Foundations%22%2C%20Google%20DeepMind%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F2507.22291%0A%20%20%20%20-%20Ronneberger%20et%20al.%20(2015)%2C%20%22U-Net%3A%20Convolutional%20Networks%20for%20Biomedical%20Image%20Segmentation%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1505.04597%0A%20%20%20%20-%20Lin%20et%20al.%20(2017)%2C%20%22Focal%20Loss%20for%20Dense%20Object%20Detection%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1708.02002%0A%20%20%20%20-%20Yeung%20et%20al.%20(2022)%2C%20%22Unified%20Focal%20loss%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F2102.04525%0A%20%20%20%20-%20Hinton%20et%20al.%20(2015)%2C%20%22Distilling%20the%20Knowledge%20in%20a%20Neural%20Network%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1503.02531%0A%20%20%20%20-%20Zadrozny%20and%20Elkan%20(2002)%2C%20isotonic%20calibration%2C%20and%20scikit-learn%201.9's%20%5Bprobability%20calibration%20guide%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fcalibration.html)%0A%20%20%20%20-%20Wolpert%20(1992)%2C%20%22Stacked%20generalization%22%2C%20*Neural%20Networks*%205(2)%0A%20%20%20%20-%20The%20companion%20reading%20in%20this%20folder%3A%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.2-temporal-transformers%2Fcode.md)%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.2-temporal-transformers%2Fscience.md)%0A%20%20%20%20%22%22%22)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
e5fc840472d4e9b009c537ae86e0dd3a