An example of using geotorch for output embeddings
Thank you for coming up with the approach and building this fantastic library. Still working through the paper but was interested if you can hint if and how the library can be build for optimizing the output embeddings of an ANN if we have a constraint that those should be on a unit sphere. In other words can I use this library somehow on the output vector of the last Linear layer to normalize it or is it only usable for weights/biases?
A little bit of background: this will be particularly interesting in a number of applications that work with deep metric learning - those often use Siamese networks and are optimized in Euclidean space during training. Later, during inference cosine similarity is used. This sort of gap is often waved off as "not problematic" in literature but would be nice to experiment with make the proper training procedure (esp. for ADAM-based optimization).
If I have understood the problem correctly, you want to design a map (say an ANN) with outputs on the sphere.
You can certainly do that, although at the moment GeoTorch does not offer a clean way of doing it (it will do so in the future). Of course, the most obvious way of doing this would be to normalise the output by dividing by the norm of the output. Other way, would be to use the exponential map on the sphere. Both of these maps are implemented in https://github.com/Lezcano/geotorch/blob/master/geotorch/sphere.py although the second one is implemented in a bit of a cryptic way. A less cryptic way to implement that is presented in section E.3.2 of the paper that you mention. Note that in this formula, although we also have the a v/\norm{v}, it is multiplied by sin(\norm{v}), so we have that lim_{t=0}sin(t)/t v = 0, so it is well defined for small vectors. The x \in S^n in that formula, you can fix it at the start of the training by sampling a random vector in the Sphere and saving it in your layer as a buffer. A short implementation of this layer would go as follows:
class expSn(nn.Module):
def __init__(self, n):
self.register_buffer("x", torch.rand(n))
self.x = self.x / torch.norm(self.x)
def forward(self, v):
# Assume v a vector of size (batch, n)
x = self.x.expand_as(v)
# Project to T_xS
v = v - (v @ x).expand_as(v) * x
norm_v = torch.norm(v)
# Maybe it is worth to see how to compute sin(t)/t in a stable way
return torch.cos(norm_v) * x + (torch.sin(norm_v)/norm_v) * v
As a disclaimer, this is just a sketch, I have not tested it, so expect some dimension errors, but hopefully the idea is clear.
Thank you!
Of course, the most obvious way of doing this would be to normalise the output by dividing by the norm of the output.
Interesting, tried it on a couple occasions before to do just that in forward() pass and for some reason training with that modification never seem to work, so without going into deep I speculated that this is due to gradient pushing the ANN in all sort of random directions as opposed to keep outputs on the unit sphere, or to quote a more eloquent another paper:
Since the optimization problem is constrained on the unit sphere, the Euclidean space optimization methods such as SGD cannot be used to optimize our objective, because the Euclidean gradient provides the update direction in a non-curvature space, while the parameters in our model must be updated on a surface with constant positive curvature. Therefore, we need to base our embedding training problem on Riemannian optimization.
Does it sound like a reason for straightforward F.normalize(output, dim=-1, p=2) to mess with training? If not - perhaps I should go back and check for bugs / hyper-parameters.
What they say is not really correct. As you see in my paper, you can use Euclidean optimisers to do optimisation on manifolds, as long as you can pullback the problem to a Euclidean space. What can happen is that, if you get some outputs with small norm and you use x / \norm{x} to pullback the problem to a Euclidean space, then the gradients will be very large, hence making the problem very unstable.
Now, this does not say that using the parametrisation of the exponential that I implemented up there should work in any given problem. What I think it should happen though is that, if you replicate the experiments in the paper that you cite on spherical text embeddings and use the class Sphere in GeoTorch to optimise those parameters, I think you should be able to improve, or at the very least match, their results, as pulling back the problem to a tangent space tends to work much better than the usual gradient descent they do (which is basically what I argue on my paper). But then again, the problem they have in that paper looks different to the one you mentioned, as they do optimise some parameters, while you said that you wanted to parametrise the output of an ANN, which is different.
Thank you, that helps. I think the problem they mention is exactly the same I try to solve but I just failed to verbalize it properly. Once again, cool paper and library, looking forward to see where it all goes, good luck!