- P2: SMC tie posterior (numpy + torch), RML/soft-DTW losses, bank training, D2 synthetic generator, scripts 09-13, protocol_p2 (locked) and its outcome (G-B failed; H1 not rejected, H3 rejected, H4 not rejected). - Diagnosis on dev realizations 900/901 only (scripts 14-17, run_p3_dev.sh): posterior screen, SMC stability (P = 1024, K = 1), sigma_a misspecification stress and training, H3 with the SMC posterior, F3 placement on sigma_post. - protocol_p3: H2 split into H2a (posterior-mean labels vs point ties) and H2b (RML vs posterior mean as a function of sigma_post); H3 descriptive only; sigma_a arm; new D2 config d2_synth_p3.yaml; main grid on realizations 8-15. Locked by configs/protocol_p3.sha256 and tag protocol-p3. - runlog: per-run logs and runtime index (logs/index.csv). - Docs: plan §12 items 10-11, theory, technical overview, report; wording aligned with Gunning & Glinsky (2006): the tie posterior is not claimed as new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
"""Losses from the thesis (`test 2/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)
|
||
)
|