Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/torchjd/aggregation/_utils/dual_cone.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@
"""Transforms a tensor into a numpy array with float64 dtype."""

return tensor.cpu().detach().numpy().astype(np.float64)


def estimate_dual_cone_spherical_volume(G: Tensor, n_samples=1_000_000) -> Tensor:
"""
Estimates the spherical volume of the dual cone defined by the Gramian G.
"""
n = G.size(0)
device = G.device

Check warning on line 70 in src/torchjd/aggregation/_utils/dual_cone.py

View check run for this annotation

Codecov / codecov/patch

src/torchjd/aggregation/_utils/dual_cone.py#L69-L70

Added lines #L69 - L70 were not covered by tests

L = torch.linalg.cholesky(G + torch.eye(n, device=device) * 1e-10)
ws = torch.randn(n_samples, n, device=device)
zs = ws @ L.T

Check warning on line 74 in src/torchjd/aggregation/_utils/dual_cone.py

View check run for this annotation

Codecov / codecov/patch

src/torchjd/aggregation/_utils/dual_cone.py#L72-L74

Added lines #L72 - L74 were not covered by tests

is_inside = torch.all(zs >= 0, dim=1)
proportion = is_inside.float().mean()

Check warning on line 77 in src/torchjd/aggregation/_utils/dual_cone.py

View check run for this annotation

Codecov / codecov/patch

src/torchjd/aggregation/_utils/dual_cone.py#L76-L77

Added lines #L76 - L77 were not covered by tests

return proportion

Check warning on line 79 in src/torchjd/aggregation/_utils/dual_cone.py

View check run for this annotation

Codecov / codecov/patch

src/torchjd/aggregation/_utils/dual_cone.py#L79

Added line #L79 was not covered by tests