Implement the frozen F3 protocol, nested LOWO, posterior label banks, ablations, diagnostics, checkpoint validation and full-volume inference. Record the negative G-C result: improvement at 1/4 wells, coverage80 52.38% and coverage90 60.91%, with unchanged thresholds and historical P2 results. Add native Windows logging, the RTX 3060 environment lock, detached runs, and an audited dashboard-lock recovery that preserves frozen scientific code. Update report.html, the plan and agent guidance with completed P3 status. Archive thesis sources under code_thesis with identical Git blobs and record the original-output audit without committing large data or model artifacts. Validation: 129 tests passed, 13 skipped; CUDA end-to-end smoke passed; completion audit verified 1,320 checkpoint/prediction pairs, 28 registration bundles and all 649 interior volume inlines. Scripts 04 and 09 were not rerun. Co-authored-by: Codex <codex@openai.com>
74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
"""Losses from the thesis (`code_thesis/train_open_patch_v2.py`), ported verbatim to build baseline B0.
|
||
|
||
Note (plan §1.1 #7): shape_loss ≈ 2·pcc_loss − 2/L, so there are only 5 independent constraints.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
|
||
import torch
|
||
import torch.nn as nn
|
||
|
||
|
||
def pcc_loss(pred: torch.Tensor, target: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
|
||
x = pred.squeeze(1); y = target.squeeze(1)
|
||
x = (x - x.mean(dim=1, keepdim=True)) / (x.std(dim=1, keepdim=True) + eps)
|
||
y = (y - y.mean(dim=1, keepdim=True)) / (y.std(dim=1, keepdim=True) + eps)
|
||
return 1.0 - (x * y).mean(dim=1).mean()
|
||
|
||
|
||
def grad_loss(pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||
dx = pred[..., 1:] - pred[..., :-1]
|
||
dy = target[..., 1:] - target[..., :-1]
|
||
return torch.mean((dx - dy) ** 2)
|
||
|
||
|
||
def shape_loss(pred: torch.Tensor, target: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
|
||
x = pred.squeeze(1); y = target.squeeze(1)
|
||
x = (x - x.mean(dim=1, keepdim=True)) / (x.std(dim=1, keepdim=True) + eps)
|
||
y = (y - y.mean(dim=1, keepdim=True)) / (y.std(dim=1, keepdim=True) + eps)
|
||
return ((x - y) ** 2).mean()
|
||
|
||
|
||
def var_match_loss(pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||
x = pred.squeeze(1); y = target.squeeze(1)
|
||
return ((x.std(dim=1) - y.std(dim=1)) ** 2).mean()
|
||
|
||
|
||
def crosscorr_loss(pred: torch.Tensor, target: torch.Tensor, max_shift: int = 8) -> torch.Tensor:
|
||
"""Minimum MSE over the shift range [-max_shift, max_shift]."""
|
||
x = pred.squeeze(1); y = target.squeeze(1)
|
||
L = x.shape[1]
|
||
losses = []
|
||
for lag in range(-max_shift, max_shift + 1):
|
||
if lag < 0: xs, ys = x[:, -lag:], y[:, :L + lag]
|
||
elif lag > 0: xs, ys = x[:, :L - lag], y[:, lag:]
|
||
else: xs, ys = x, y
|
||
losses.append(((xs - ys) ** 2).mean(dim=1))
|
||
return torch.stack(losses, dim=1).min(dim=1).values.mean()
|
||
|
||
|
||
@dataclass
|
||
class LegacyV2Loss:
|
||
lambda_pcc: float = 0.60
|
||
lambda_grad: float = 0.05
|
||
lambda_shape: float = 0.10
|
||
lambda_var: float = 0.10
|
||
lambda_xcorr: float = 0.20
|
||
xcorr_max_shift: int = 8
|
||
huber_beta: float = 0.02
|
||
|
||
def __post_init__(self):
|
||
self._reg = nn.SmoothL1Loss(beta=self.huber_beta)
|
||
|
||
def __call__(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||
return (
|
||
self._reg(pred, target)
|
||
+ self.lambda_pcc * pcc_loss(pred, target)
|
||
+ self.lambda_grad * grad_loss(pred, target)
|
||
+ self.lambda_shape * shape_loss(pred, target)
|
||
+ self.lambda_var * var_match_loss(pred, target)
|
||
+ self.lambda_xcorr * crosscorr_loss(pred, target, self.xcorr_max_shift)
|
||
)
|