Learning to Prune in Metric and Non-Metric Spaces