MCPcopy Create free account
hub / github.com/apple/ml-pointersect / SphericalGaussian

Class SphericalGaussian

plib/spherical_gaussian.py:12–146  ·  view source on GitHub ↗

r""" This class implements the spherical Gaussian distribution (i.e., the von Mises-Fisher distribution in 3D). Let :math:`w` in :math:`\mathbb{S}^2` and :math:`k \ge 0`, the spherical Gaussian distribution is defined as .. math:: f(w) & = \frac{1}{4 \pi}, if k = 0 \\

Source from the content-addressed store, hash-verified

10
11
12class SphericalGaussian:
13 r"""
14 This class implements the spherical Gaussian distribution (i.e., the von Mises-Fisher distribution in 3D).
15
16 Let :math:`w` in :math:`\mathbb{S}^2` and :math:`k \ge 0`, the spherical Gaussian distribution is defined as
17
18 .. math::
19 f(w) & = \frac{1}{4 \pi}, if k = 0 \\
20 & = \frac{k}{2 \pi (1 - exp(-2 k))} exp(k (u^T w - 1)), k > 0
21
22 Ref: http://www.mitsuba-renderer.org/~wenzel/files/vmf.pdf
23 """
24
25 def __init__(
26 self,
27 u: torch.Tensor,
28 k: T.Union[torch.Tensor, None],
29 log_k: torch.Tensor = None,
30 normalize_u: bool = False,
31 ):
32 """
33 Args:
34 u: (*, 3)
35 the mean direction.
36 (*) is the dimension of independent components`.
37 We are going to use M to represent it for simplicity.
38 k: (*,) or (,)
39 the concentration on the sphere (large k -> more concentrated)
40 log_k: (*,) or (,)
41 log of k, can be None. If given, k will be ignored.
42 """
43 *m_shape, dim = u.shape
44 self.m_shape = m_shape
45 self.normalize_u = normalize_u
46
47 assert u.size(-1) == 3
48 if self.normalize_u:
49 u = torch.nn.functional.normalize(u, dim=-1)
50
51 self.u = u
52
53 if isinstance(k, (float, int)):
54 k = torch.ones(*m_shape, device=u.device) * k
55
56 # assert k.shape == m_shape, f'{k.shape} {m_shape}'
57 self.k = k
58 if log_k is None:
59 self.log_k = self.k.log()
60 else:
61 self.log_k = log_k
62 self.k = self.log_k.exp()
63
64 self.log_2pi = math.log(2 * math.pi)
65
66 def compute_neg_log_likelihoods(
67 self,
68 samples: torch.Tensor,
69 normalize: bool = True,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected