(x: torch.Tensor, eps: float = EPS)
| 14 | |
| 15 | |
| 16 | def length_safe(x: torch.Tensor, eps: float = EPS) -> torch.Tensor: |
| 17 | return torch.sqrt(torch.clamp(dot(x, x), min=eps*eps)) # Clamp to avoid NaN gradients because grad(sqrt(0)) = NaN. |
| 18 | |
| 19 | |
| 20 | def normalize_safe(x: torch.Tensor, eps: float = EPS) -> torch.Tensor: |