Add Piecewise Exponential CIF Loss and update model evaluation for PWE
This commit is contained in:
146
losses.py
146
losses.py
@@ -258,3 +258,149 @@ class DiscreteTimeCIFNLLLoss(nn.Module):
|
||||
F.nll_loss(logp_at_event_bin, target_events, reduction="mean")
|
||||
|
||||
return nll, reg
|
||||
|
||||
|
||||
class PiecewiseExponentialCIFNLLLoss(nn.Module):
|
||||
"""
|
||||
Piecewise-Exponential (PWE) cause-specific hazards with discrete-time CIF likelihood.
|
||||
- No censoring
|
||||
- No regularization (reg always 0)
|
||||
- Forward signature matches DiscreteTimeCIFNLLLoss:
|
||||
forward(logits, target_events, dt, reduction) -> (nll, reg)
|
||||
|
||||
Expected shapes:
|
||||
logits: (M, K, n_bins) # hazard logits per cause per bin
|
||||
target_events: (M,) long in [0, K-1]
|
||||
dt: (M,) event times (strictly > 0)
|
||||
|
||||
bin_edges:
|
||||
length n_bins+1, strictly increasing, bin_edges[0]==0,
|
||||
and MUST be finite at the last edge (no +inf) for PWE.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
bin_edges: Sequence[float],
|
||||
eps: float = 1e-6,
|
||||
lambda_reg: float = 0.0, # kept for signature compatibility; UNUSED
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
if len(bin_edges) < 2:
|
||||
raise ValueError("bin_edges must have length >= 2 (n_bins >= 1)")
|
||||
if float(bin_edges[0]) != 0.0:
|
||||
raise ValueError("bin_edges[0] must equal 0.0")
|
||||
for i in range(1, len(bin_edges)):
|
||||
if not (float(bin_edges[i]) > float(bin_edges[i - 1])):
|
||||
raise ValueError("bin_edges must be strictly increasing")
|
||||
if math.isinf(float(bin_edges[-1])):
|
||||
raise ValueError(
|
||||
"PiecewiseExponentialCIFNLLLoss requires a finite last bin edge (no +inf). "
|
||||
"Use a finite truncation horizon for PWE."
|
||||
)
|
||||
|
||||
self.eps = float(eps)
|
||||
# unused, kept only for interface compatibility
|
||||
self.lambda_reg = float(lambda_reg)
|
||||
|
||||
self.register_buffer(
|
||||
"bin_edges",
|
||||
torch.tensor([float(x) for x in bin_edges], dtype=torch.float32),
|
||||
persistent=False,
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
logits: torch.Tensor,
|
||||
target_events: torch.Tensor,
|
||||
dt: torch.Tensor,
|
||||
reduction: str = "mean",
|
||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||
if reduction not in {"mean", "sum", "none"}:
|
||||
raise ValueError("reduction must be one of {'mean','sum','none'}")
|
||||
|
||||
if logits.ndim != 3:
|
||||
raise ValueError(
|
||||
f"logits must be 3D (M, K, n_bins); got shape={tuple(logits.shape)}")
|
||||
if target_events.ndim != 1 or dt.ndim != 1:
|
||||
raise ValueError("target_events and dt must be 1D tensors")
|
||||
if logits.shape[0] != target_events.shape[0] or logits.shape[0] != dt.shape[0]:
|
||||
raise ValueError(
|
||||
"Batch size mismatch among logits, target_events, dt")
|
||||
|
||||
if not torch.all(dt > 0):
|
||||
raise ValueError(
|
||||
"dt must be strictly positive (no censoring supported here)")
|
||||
|
||||
M, K, n_bins = logits.shape
|
||||
|
||||
if target_events.dtype != torch.long:
|
||||
target_events = target_events.to(torch.long)
|
||||
if (target_events < 0).any() or (target_events >= K).any():
|
||||
raise ValueError(f"target_events must be in [0, {K-1}]")
|
||||
|
||||
# Prepare bin_edges / bin widths
|
||||
bin_edges = self.bin_edges.to(device=dt.device, dtype=dt.dtype)
|
||||
if bin_edges.numel() != n_bins + 1:
|
||||
raise ValueError(
|
||||
f"bin_edges length must be n_bins+1={n_bins+1}; got {bin_edges.numel()}"
|
||||
)
|
||||
|
||||
dt_bins = (bin_edges[1:] - bin_edges[:-1]
|
||||
).to(device=logits.device, dtype=logits.dtype) # (n_bins,)
|
||||
if not torch.isfinite(dt_bins).all():
|
||||
raise ValueError("All bin widths must be finite for PWE.")
|
||||
if not (dt_bins > 0).all():
|
||||
raise ValueError(
|
||||
"All bin widths must be strictly positive for PWE.")
|
||||
|
||||
# Map event time -> bin index k* in {1..n_bins}
|
||||
# (same convention as your discrete_time_cif: clamp to [1, n_bins])
|
||||
time_bin = torch.bucketize(dt, bin_edges)
|
||||
time_bin = torch.clamp(
|
||||
time_bin, min=1, max=n_bins).to(torch.long) # (M,)
|
||||
k0 = time_bin - 1 # 0..n_bins-1
|
||||
|
||||
# Nonnegative hazards per cause per bin
|
||||
hazards = F.softplus(logits) + self.eps # (M, K, n_bins)
|
||||
|
||||
# Integrated hazards H_{j,k} = lambda_{j,k} * Δt_k
|
||||
H_jk = hazards * dt_bins.view(1, 1, n_bins) # (M, K, n_bins)
|
||||
H_k = H_jk.sum(dim=1) # (M, n_bins)
|
||||
|
||||
# Previous survival term: Σ_{u<k*} H_u
|
||||
bins = torch.arange(
|
||||
1, n_bins + 1, device=logits.device).unsqueeze(0) # (1, n_bins)
|
||||
mask_prev = bins < time_bin.unsqueeze(1) # (M, n_bins)
|
||||
loss_prev = (H_k * mask_prev.to(H_k.dtype)).sum(dim=1) # (M,)
|
||||
|
||||
# Event term at k*: -log p_{k*}(cause)
|
||||
m_idx = torch.arange(M, device=logits.device)
|
||||
|
||||
H_event_total = torch.clamp(H_k[m_idx, k0], min=self.eps) # (M,)
|
||||
H_event_cause = torch.clamp(
|
||||
H_jk[m_idx, target_events, k0], min=self.eps) # (M,)
|
||||
|
||||
# log(1 - exp(-H)) stable
|
||||
log1mexp = torch.log(-torch.expm1(-H_event_total)) # (M,)
|
||||
loss_event = -log1mexp - \
|
||||
torch.log(H_event_cause) + torch.log(H_event_total)
|
||||
|
||||
loss_vec = loss_prev + loss_event # (M,)
|
||||
|
||||
if reduction == "mean":
|
||||
nll = loss_vec.mean()
|
||||
elif reduction == "sum":
|
||||
nll = loss_vec.sum()
|
||||
else:
|
||||
nll = loss_vec
|
||||
|
||||
if self.lambda_reg > 0.0 and n_bins >= 3:
|
||||
log_h = torch.log(hazards) # (M, K, n_bins)
|
||||
d2 = log_h[:, :, 2:] - 2.0 * log_h[:, :, 1:-1] + \
|
||||
log_h[:, :, :-2] # (M, K, n_bins-2)
|
||||
reg = self.lambda_reg * (d2.pow(2).mean())
|
||||
else:
|
||||
reg = torch.zeros((), device=logits.device, dtype=loss_vec.dtype)
|
||||
|
||||
return nll, reg
|
||||
|
||||
Reference in New Issue
Block a user