Download src/cbf/loss.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/src/cbf/loss.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/src/cbf/loss.py
-
curl -L -o loss.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/src/cbf/loss.py
2.92 kB
| """ | |
| Training Loss Formulations for Neural Control Barrier Functions | |
| Combines boundary classification with self-supervised Lie derivative condition penalties. | |
| """ | |
| from typing import Dict | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class CBFLoss(nn.Module): | |
| """ | |
| Multi-objective loss function for training Neural CBFs: | |
| L(theta) = lambda_safe * L_safe + lambda_unsafe * L_unsafe + lambda_cbf * L_cbf + lambda_reg * L_reg | |
| """ | |
| def __init__( | |
| self, | |
| alpha_cbf: float = 1.0, | |
| margin: float = 0.1, | |
| weight_safe: float = 1.0, | |
| weight_unsafe: float = 1.5, | |
| weight_cbf: float = 2.0, | |
| weight_reg: float = 1e-4, | |
| ): | |
| super().__init__() | |
| self.alpha_cbf = alpha_cbf | |
| self.margin = margin | |
| self.weight_safe = weight_safe | |
| self.weight_unsafe = weight_unsafe | |
| self.weight_cbf = weight_cbf | |
| self.weight_reg = weight_reg | |
| def forward( | |
| self, | |
| model: nn.Module, | |
| x_safe: torch.Tensor, | |
| x_unsafe: torch.Tensor, | |
| x_boundary: torch.Tensor, | |
| f_boundary: torch.Tensor, | |
| g_boundary: torch.Tensor, | |
| u_nominal: torch.Tensor, | |
| ) -> Dict[str, torch.Tensor]: | |
| """ | |
| Evaluates full multi-objective loss across safe, unsafe, and boundary regions. | |
| """ | |
| # 1. Safe Set Loss: h(x) >= margin -> penalize ReLU(margin - h(x)) | |
| h_safe = model(x_safe) | |
| loss_safe = torch.mean(F.relu(self.margin - h_safe)) | |
| # 2. Unsafe Set Loss: h(x) <= -margin -> penalize ReLU(h(x) + margin) | |
| h_unsafe = model(x_unsafe) | |
| loss_unsafe = torch.mean(F.relu(h_unsafe + self.margin)) | |
| # 3. CBF Condition Loss on boundary / domain states: | |
| # Require: sup_u [ L_f h + L_g h * u + alpha * h ] >= 0 | |
| h_b, grad_b = model.compute_h_and_jacobian(x_boundary) | |
| # L_f h = sum(grad_b * f, dim=-1) | |
| Lf_h = torch.sum(grad_b * f_boundary, dim=-1, keepdim=True) | |
| # L_g h * u = grad_b @ g @ u | |
| # g_boundary: (batch, n, m), grad_b: (batch, n) | |
| # Lg_h: (batch, m) | |
| Lg_h = torch.bmm(grad_b.unsqueeze(1), g_boundary).squeeze(1) | |
| Lg_u = torch.sum(Lg_h * u_nominal, dim=-1, keepdim=True) | |
| cbf_violation = -(Lf_h + Lg_u + self.alpha_cbf * h_b) | |
| loss_cbf = torch.mean(F.relu(cbf_violation)) | |
| # 4. L2 Weight Regularization | |
| loss_reg = sum(torch.sum(p ** 2) for p in model.parameters()) | |
| # Total Weighted Loss | |
| total_loss = ( | |
| self.weight_safe * loss_safe | |
| + self.weight_unsafe * loss_unsafe | |
| + self.weight_cbf * loss_cbf | |
| + self.weight_reg * loss_reg | |
| ) | |
| return { | |
| "total_loss": total_loss, | |
| "loss_safe": loss_safe, | |
| "loss_unsafe": loss_unsafe, | |
| "loss_cbf": loss_cbf, | |
| "loss_reg": loss_reg, | |
| } | |