Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
Deep-ML | Poincare Embeddings: Distance, Riemannian Gradient and ProjectionPoincare Embeddings: Distance, Riemannian Gradient and Projection
Poincare Embeddings: Distance, Riemannian Gradient and Projection
HardMachine Learning
Example:
Input:
```python
import numpy as np
print([round(poincare_dist([0.0, 0.0], [r, 0.0]), 4) for r in (0.5, 0.9, 0.99)])
```
Output:
[1.0986, 2.9444, 5.2933]
Reasoning:
From the origin to a point at radius r the distance is arcosh(1 + 2r^2/(1 - r^2)) = 2 artanh(r): 1.0986 at r = 0.5, 2.9444 at r = 0.9 and 5.2933 at r = 0.99, growing without bound near the boundary.
Hyperbolic space embeds trees with low distortion. Nickel and Kiela embed nodes in the Poincare ball {z:∥z∥<1} with distance
d(u,v)=arcosh(1+2(1−∥u∥2)(1−∥v∥2)∥u−v∥2)
. Implement:
poincare_dist(u, v): the distance, as a Python float.
poincare_dist_grad(u, v): the Euclidean gradient ∂d(u,v)/∂u. With α=1−∥u∥2, β=1−∥v∥2 and γ=1+2∥u−v∥2/(αβ) it is
βγ2−14(α2∥v∥2−2u⊤v+1u−αv)
poincare_loss(Z, pos_pairs, neg_lists): the mean over positive pairs (u,v) (with negatives neg_lists[k] for the k-th pair) of −log∑v′∈{v}∪nege−d(zu,zv′)e−d(zu,zv), the softmax form of the loss. Return a Python float.
riemannian_sgd_step(Z, egrad, lr, eps=1e-5): return a new array in which each row z is updated to z−η4(1−∥z∥2)2∇E (the Riemannian gradient rescales the Euclidean one by the inverse metric) and then, if its norm is at least 1−ϵ, rescaled to norm 1−ϵ.