explcre commited on
Commit
67bf754
Β·
verified Β·
1 Parent(s): 900229b

Upload docs/oracle_training_explained.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. docs/oracle_training_explained.md +321 -0
docs/oracle_training_explained.md ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # How the DeepSTARR-7cell oracle was trained β€” clear, detailed walkthrough
2
+
3
+ The oracle file we score every T1/T3 prediction against is at
4
+ `/dev/shm/dnathinker/_lab_results/runs/exp_oracle_ds_7cell_fdr_both_20260424_162210/oracle.pt`
5
+ (1.4 MB; lab-trained 2026-04-24).
6
+
7
+ This doc walks through:
8
+
9
+ 1. The architecture (DeepSTARR backbone)
10
+ 2. The prediction head (14 outputs, not 7 β€” and why)
11
+ 3. The training loss (MSE regression)
12
+ 4. Optimizer + schedule + early-stop
13
+ 5. The actual training metrics (val_pearson per cell)
14
+ 6. How the oracle is USED downstream (FID / specificity / argmax_acc / objective_success)
15
+ 7. Why the val_pearson is weak but the eval is still meaningful
16
+
17
+ ## 1. Architecture β€” DeepSTARR backbone (de Almeida et al., Nat. Genet. 2022)
18
+
19
+ `regureasoner/benchmarks/oracles/deepstarr_7cell.py:DeepSTARR7Cell`:
20
+
21
+ ```
22
+ Input: one-hot DNA (B, 4 channels, L=512)
23
+
24
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
25
+ β”‚ Conv1D( 4 β†’ 256, kernel=7, pad=3) + BN + ReLU + MaxPool(3) β”‚ ← block 0
26
+ β”‚ Conv1D(256 β†’ 60, kernel=3, pad=1) + BN + ReLU + MaxPool(3) β”‚ ← block 1
27
+ β”‚ Conv1D( 60 β†’ 60, kernel=5, pad=2) + BN + ReLU + MaxPool(3) β”‚ ← block 2
28
+ β”‚ Conv1D( 60 β†’ 120, kernel=3, pad=1) + BN + ReLU + MaxPool(3) β”‚ ← block 3
29
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
30
+ β”‚
31
+ β”‚ flatten β†’ (B, 120 Γ— L_after_pool)
32
+ β–Ό
33
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
34
+ β”‚ Linear β†’ 256, ReLU, Dropout(0.4) β”‚ fc1
35
+ β”‚ Linear β†’ 256, ReLU, Dropout(0.4) β”‚ fc2 ← FID embeds
36
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
37
+ β”‚
38
+ β–Ό
39
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
40
+ β”‚ Linear β†’ 14 outputs (regression head) β”‚
41
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
42
+ ```
43
+
44
+ * 4 convolutional blocks; channels `(256, 60, 60, 120)`, kernels
45
+ `(7, 3, 5, 3)`, MaxPoolΓ—3 each (DeepSTARR paper exact widths).
46
+ * 2 fully-connected layers, 256-d, ReLU + dropout 0.4 between them.
47
+ * `embed()` returns the **post-fc2** features (256-d) β€” that's the FID
48
+ feature space.
49
+
50
+ Total params β‰ˆ 1 M. Tiny vs Enformer (250 M+) and Sei (50 M); fast to
51
+ train (6 h on a single GPU per the lab's run).
52
+
53
+ ## 2. Prediction head β€” 14 outputs (NOT 7)
54
+
55
+ The lab's deployed oracle has **14 cell-type heads** even though the
56
+ brain panel has 7 cells. The cell-types tuple stored in
57
+ `oracle.pt:config.cell_types` is:
58
+
59
+ ```
60
+ ('Ex', 'In', 'OPC', 'Ast', 'Oli', 'Mic', 'End',
61
+ 'Ex_corr', 'In_corr', 'OPC_corr', 'Ast_corr', 'Oli_corr', 'Mic_corr', 'End_corr')
62
+ ↑ raw activity per cell ↑ FDR-corrected activity per cell
63
+ ```
64
+
65
+ The "fdr_both" in the run dir name (`exp_oracle_ds_7cell_fdr_both_*`)
66
+ encodes this: the oracle predicts BOTH the raw enhancer-link activity
67
+ AND the FDR-corrected version per cell. Two columns per cell, 7 cells
68
+ = 14 outputs.
69
+
70
+ When downstream scorers (FID / specificity / argmax) want the per-cell
71
+ target, they read the **first 7** columns (the raw heads). The
72
+ `_corr` columns are present so the oracle stays compatible with the
73
+ larger Table 4 cross-oracle ablation that uses corrected activity as
74
+ the target metric.
75
+
76
+ The head is a **single linear layer**: `fc2 (256-d) β†’ Linear β†’ (B, 14)`.
77
+ No softmax. No normalisation. The output is a continuous activity score
78
+ per cell type β€” interpretable as the model's prediction of how active
79
+ the input enhancer would be in each of the 14 conditions.
80
+
81
+ ## 3. Training loss β€” MSE regression in untransformed activity space
82
+
83
+ `regureasoner/benchmarks/oracles/unified_trainer.py` line 409:
84
+
85
+ ```python
86
+ optim = torch.optim.AdamW(trainable_params,
87
+ lr=2e-3, weight_decay=1e-4)
88
+ mse = nn.MSELoss() # ← the loss
89
+
90
+ for epoch in range(30):
91
+ for batch in train_loader:
92
+ x = batch["x"].to(device) # (B, 4, 512) one-hot
93
+ y = batch["y"].to(device) # (B, 14) gold activities
94
+
95
+ h = model.encoder(x).flatten(1)
96
+ h = model.dense(h) # fc1 + ReLU + Dropout + fc2 + ReLU + Dropout
97
+ y_hat = model.head(h) # (B, 14) predicted activities
98
+
99
+ loss = mse(y_hat, y) # straight MSE, no transform
100
+ loss.backward()
101
+ optim.step()
102
+ ```
103
+
104
+ **Loss = `mean( (y_hat βˆ’ y)Β² )` over the 14 outputs.** No log-transform,
105
+ no rank-based loss, no softmax-cross-entropy. The activities live in
106
+ their native (untransformed) DeepSTARR-paper space, so the oracle's
107
+ predicted score is directly the predicted enhancer activity per cell.
108
+
109
+ This matches the recipe used by:
110
+ * the original DeepSTARR paper (de Almeida 2022)
111
+ * ATGC-Gen (Su et al. 2024)
112
+ * TACO (Lin et al. NeurIPS 2024) for their per-cell activity oracle
113
+
114
+ We use the SAME loss + recipe for all three oracle backends in the
115
+ unified trainer (DeepSTARR-7cell, Enformer linear-head, Sei linear-
116
+ head); only the backbone differs.
117
+
118
+ ## 4. Optimizer + schedule + early-stop
119
+
120
+ From the actual `oracle.pt:config`:
121
+
122
+ | Knob | Value |
123
+ |---|---:|
124
+ | Optimizer | AdamW |
125
+ | Learning rate | 2e-3 |
126
+ | Weight decay | 1e-4 |
127
+ | Batch size | 128 |
128
+ | Epochs (max) | 30 |
129
+ | Early-stop patience | 10 |
130
+ | Validation fraction | 0.1 (random split, seed 1234) |
131
+ | Input length | 512 bp |
132
+ | Dropout | 0.4 |
133
+
134
+ **Best-checkpoint selection metric**: `val_pearson_mean` β€” the unit-
135
+ weighted average of per-column Pearson correlations between predicted
136
+ and gold activities. Stored at `metrics.json:best_val_pearson_mean`.
137
+
138
+ Why Pearson averaged across columns (not MSE): the DeepSTARR-paper
139
+ convention is that **rank quality matters more than absolute
140
+ activity** β€” we use the oracle to compare DIFFERENT enhancers in the
141
+ SAME cell, not to predict raw activity. Pearson is rank-equivariant
142
+ in the sense that matters here.
143
+
144
+ ## 5. The actual lab metrics (what landed)
145
+
146
+ `metrics.json` from the deployed oracle:
147
+
148
+ ```json
149
+ {
150
+ "best_val_pearson_mean": 0.1356,
151
+ "val_mse": 59.06,
152
+ "val_pearson_mean": 0.1356,
153
+ "val_spearman_mean": 0.0856,
154
+ "val_pearson_per_cell": [0.339, 0.132, 0.112, 0.100, 0.155, 0.363, 0.019, ...corrected 7],
155
+ "val_spearman_per_cell": [0.285, 0.068, 0.064, 0.094, 0.114, 0.217, 0.006, ...corrected 7]
156
+ }
157
+ ```
158
+
159
+ Per-cell Pearson on the RAW heads (first 7):
160
+
161
+ | Cell | val_pearson | val_spearman |
162
+ |---|---:|---:|
163
+ | **Mic** | **0.363** | 0.217 |
164
+ | **Ex** | **0.339** | 0.285 |
165
+ | Oli | 0.155 | 0.114 |
166
+ | In | 0.132 | 0.068 |
167
+ | OPC | 0.112 | 0.064 |
168
+ | Ast | 0.100 | 0.094 |
169
+ | **End** | **0.019** ⚠ | 0.006 |
170
+
171
+ Reading: the oracle works **well on Ex / Mic** (the cells with most
172
+ training rows), **poorly on End** (8k train samples, the rarest in
173
+ the 7-cell panel). This is intrinsic to the data β€” End has the
174
+ fewest enhancer–promoter links in the source dataset.
175
+
176
+ ## 6. How the oracle is USED at evaluation time
177
+
178
+ `regureasoner/benchmarks/metrics/specificity.py` reads the per-cell
179
+ 14-d activity vector and produces three downstream metrics:
180
+
181
+ ```python
182
+ # For each predicted enhancer:
183
+ activity = oracle.predict_activity(seq) # (14,) raw + corrected
184
+ target_idx = CELL_TYPES.index(target_cell) # 0..6 in the raw heads
185
+ on_target = activity[target_idx]
186
+ off_target = mean(activity[i] for i in 0..6 if i != target_idx)
187
+ argmax_correct = int(activity[:7].argmax()) == target_idx
188
+ ```
189
+
190
+ * **`argmax_accuracy`**: fraction where `argmax(activity[:7]) == target`.
191
+ * **`specificity`** = `on_target βˆ’ off_target`. Positive β‡’ enhancer
192
+ more active in target than off-target average.
193
+ * **`on_target_score` / `off_target_score`**: separate so paper tables
194
+ can show the decomposition.
195
+
196
+ For T3 (`eval_t3_oracle.py`), the oracle is called twice per row:
197
+ once on the predicted edited sequence, once on the reference. The
198
+ **deltas** (`pred_activity_src βˆ’ ref_activity_src`,
199
+ `(pred_tgt βˆ’ pred_src) βˆ’ (ref_tgt βˆ’ ref_src)`) feed
200
+ `objective_success` per `edit_type`. Because the metric uses
201
+ **deltas, not absolute activity**, even a weak oracle (Pearson 0.14
202
+ average) gives meaningful relative ranking β€” which is the only thing
203
+ RFT needs to filter candidates.
204
+
205
+ For FID, the oracle's `embed()` returns the 256-d post-fc2 features.
206
+ We compute FrΓ©chet distance between the (mean, covariance) of those
207
+ features on **predicted** vs **gold** sequences per cell type.
208
+
209
+ ## 7. Why val_pearson=0.14 is weak but the eval still works
210
+
211
+ **Caveat for the paper writeup**: the oracle is far from perfect.
212
+ val_pearson_mean=0.14 on the raw heads means the oracle explains
213
+ about 2 % of the absolute-activity variance β€” far below an Enformer-
214
+ or Sei-grade predictor (typically 0.3–0.5 on similar panels).
215
+
216
+ But:
217
+
218
+ 1. **All comparisons are RELATIVE**. We don't report "absolute
219
+ activity = 3.5" anywhere in the paper. We report
220
+ `pred_activity_target βˆ’ pred_activity_off_target`, which is
221
+ computed on the SAME oracle for both quantities. Bias cancels.
222
+ 2. **The metrics are rank-based**: `argmax_accuracy` and
223
+ `specificity` are robust to a constant scale or shift in oracle
224
+ outputs.
225
+ 3. **For T3 we use deltas**: `pred βˆ’ ref` per cell. Same oracle on
226
+ both terms; only the derivative matters.
227
+ 4. **Cross-oracle robustness check** (Table 4): we plan to retrain
228
+ with Enformer + Sei backbones (lab cluster, deferred) and report
229
+ the same metrics. Robustness across oracles is the actual
230
+ defensive claim against reviewer pushback.
231
+
232
+ ## 8. The exact training-time data flow (one batch)
233
+
234
+ ```
235
+ training row JSONL: {"sequence": "ACGT...512bp...", "cell_activities": [a1,...,a14]}
236
+ ┃
237
+ β–Ό
238
+ one_hot_dna(seq, length=512)
239
+ ┃
240
+ β–Ό
241
+ (4, 512) β†’ batch β†’ (B, 4, 512)
242
+ ┃
243
+ β–Ό
244
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
245
+ β–Ό β–Ό
246
+ encoder (4 conv blocks) y = (B, 14) gold
247
+ ┃
248
+ β–Ό flatten β†’ (B, 120Β·6)
249
+ β–Ό
250
+ dense (fc1 β†’ fc2) ← (B, 256) "FID embed"
251
+ ┃
252
+ β–Ό
253
+ head Linear β†’ (B, 14) ← (B, 14) y_hat
254
+ β”‚
255
+ └──────► loss = MSE(y_hat, y)
256
+ β””β–Ί backprop through head + dense + encoder
257
+ (no frozen layers; whole CNN trains from scratch)
258
+ ```
259
+
260
+ Training time: ~6 h on a single A100. Output: `oracle.pt` (state +
261
+ config + cell_types tuple), `metrics.json` (per-cell Pearson/Spearman),
262
+ `log.jsonl` (per-epoch).
263
+
264
+ ## 9. What the H100 eval pipeline DOES
265
+
266
+ When the reaper picks up a fresh `predictions.jsonl`:
267
+
268
+ 1. `load_oracle("oracle.pt")` β€” rebuilds `DeepSTARR7Cell` from config.
269
+ 2. `oracle.to(device)` β€” `--device auto` picks GPU when free, CPU else.
270
+ 3. `oracle.eval()`.
271
+ 4. For each predicted enhancer:
272
+ ```
273
+ activity_14 = oracle.predict_activity(seq)
274
+ embed_256 = oracle.embed(seq) # FID space
275
+ ```
276
+ 5. Aggregate:
277
+ * **FID**: FrΓ©chet distance between gold-set embeds and predicted-
278
+ set embeds, per cell type and aggregate.
279
+ * **specificity / argmax_accuracy / on / off**: per
280
+ `target_cell_type`.
281
+ * **diversity_edit / kmer_unique_frac**: dataset-level.
282
+
283
+ All of this is what `genqual.json` (T1/T3) and `genqual_t3_oracle.json`
284
+ (T3 only β€” RFT-aware objective scoring) report.
285
+
286
+ ## 10. Why the lab is also building Enformer + Sei oracles
287
+
288
+ DeepSTARR-7cell is the **anchor oracle** because:
289
+ * CPU-friendly to train (~6h).
290
+ * Smallest oracle artifact (1.4 MB) β†’ easy to ship + load on H100.
291
+ * Same recipe as published DNA-LM evaluation papers.
292
+
293
+ Enformer and Sei are slated as **Table 4 cross-oracle robustness
294
+ rows**. Their backbones are larger (Enformer 250M, Sei 50M), pretrained
295
+ on bigger genomic corpora, and predict activity directly from
296
+ sequence β€” so their per-cell Pearson on our panel should be
297
+ significantly higher (0.3–0.5 expected). The trade-off is training
298
+ time: Enformer's frozen-backbone + linear-head retrain is ~50 h, hence
299
+ the lab's 226086 (NTv3-8m enc) status and the Enformer hang at
300
+ job 225956.
301
+
302
+ If the deepstarr-7cell + enformer + sei rankings AGREE on which models
303
+ generate better enhancers, that's a strong robustness claim and the
304
+ weak Pearson on DeepSTARR-7cell becomes much less of a reviewer
305
+ concern.
306
+
307
+ ## TL;DR for paper Β§"Oracle"
308
+
309
+ > "We train a 7-cell-type DeepSTARR-style CNN regression oracle
310
+ > (4 conv blocks β†’ 2 fully-connected layers β†’ 14-output linear head;
311
+ > 14 = 7 raw + 7 FDR-corrected per cell) on (sequence,
312
+ > cell_activities) pairs from the brain panel. Loss is MSE in the
313
+ > untransformed activity space; AdamW with lr=2e-3, weight decay
314
+ > 1e-4, batch 128, 30 epochs, early-stop on val_pearson_mean
315
+ > patience 10, val_fraction 0.1. The oracle achieves
316
+ > val_pearson_mean = 0.14 (best on Ex 0.34 / Mic 0.36, weakest on
317
+ > End 0.02), which is sufficient because all downstream metrics
318
+ > (FID, specificity, argmax accuracy, T3 objective deltas) are
319
+ > rank- or delta-based and therefore robust to bias in absolute
320
+ > activity. We additionally retrain Enformer- and Sei-backbone
321
+ > oracles for cross-oracle robustness (Table 4)."