in hype/manifolds/lorentz.py [0:0]
def normalize(self, w):
"""Normalize vector such that it is located on the hyperboloid"""
d = w.size(-1) - 1
narrowed = w.narrow(-1, 1, d)
if self.max_norm:
narrowed.view(-1, d).renorm_(p=2, dim=0, maxnorm=self.max_norm)
if self.K is not None:
# Push embeddings outside of `inner_radius`
w0 = w.narrow(-1, 0, 1).squeeze()
wnrm = th.sqrt(th.sum(th.pow(narrowed, 2), dim=-1)) / (1 + w0)
scal = th.ones_like(wnrm)
ix = wnrm < (self.inner_radius + self._eps)
scal[ix] = (self.inner_radius + self._eps) / wnrm[ix]
narrowed.mul_(scal.unsqueeze(-1))
tmp = 1 + th.sum(th.pow(narrowed, 2), dim=-1, keepdim=True)
tmp.sqrt_()
w.narrow(-1, 0, 1).copy_(tmp)
return w