Files
ML-Porosity/ml_porosity/losses/legacy_v2.py
lamlvh a76e61398a P0 + P1: ml_porosity package, audited F3 data, LOWO baselines, protocol v2
P0
- ml_porosity/: survey geometry, LAS/TDR/SEG-Y readers (4 ms delay fix,
  invalid time scalar, header LUT for incomplete cubes), anti-aliased
  log-to-time resampling, CNN-GRU v2 with the thesis loss, 41+ unit tests.
- Full F3 Demo 2023 under data/ (text well/fault files tracked, bulk data
  ignored); the filtered copy and its cleanup notes are removed.
- SHA-256 manifests for F3, Volve, NLOG and the thesis outputs.

P1
- configs/protocol.yaml v2, locked by SHA-256 at 2026-10-26T04:28:09Z,
  before the first LOWO run (04:30:50Z); 05_lowo.py refuses to run on a
  hash mismatch. This commit postdates the runs.
- Wavelet/synthetic tie QC, residual misregistration, LOWO 5 seeds for
  B0a, B0b, B1, B2, B3a, B3b (scripts 04-06); gate G-A: no pivot.
- Volve D3: horizons, well picks, checkshots and the ST10010 cube read
  from data/Volve (scripts/07).
- D-1 label check against measured NLOG density (scripts/08): PHIE_DT
  stays the primary label.
- docs/plan.md, Volve and NLOG inventories, report.html.

Full-text reference PDFs in refs/ stay local (ignored).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 08:41:11 +00:00

74 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Các loss của luận văn (`test 2/train_open_patch_v2.py`), chuyển nguyên trạng để dựng baseline B0.
Lưu ý (plan mục 1.1 #7): shape_loss ≈ 2·pcc_loss − 2/L nên chỉ có 5 ràng buộc độc lập.
"""
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:
"""MSE nhỏ nhất trên dải shift [-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)
)