Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast differentiable sorting and ranking #88

Merged
merged 26 commits into from Sep 12, 2023
Merged

Fast differentiable sorting and ranking #88

merged 26 commits into from Sep 12, 2023

Conversation

BatyLeo
Copy link
Member

@BatyLeo BatyLeo commented Aug 23, 2023

Following this discourse post, here is a Julia implementation of Fast differentiable sorting and ranking (see here for the original Python implementation).

This includes:

  • soft_rank and soft_sort differentiable methods
  • options for choosing regularization: l2 or entropic
  • Two new custom optimization layers:SoftRank <: AbstractRegularized and SoftSort <: AbstractOptimizationLayer compatible with the InferOpt ecosystem

Reproducing figures from the paper:

using InferOpt, Plots, Zygote
plot(x -> sort([0.0, x, 1.0, 2.0]; rev=true)[2], label="Sort")
plot!(x -> soft_sort_l2([0.0, x, 1.0, 2.0]; rev=true)[2], label="Soft sort l2")
plot!(y -> gradient(x -> soft_sort_l2([0.0, x, 1.0, 2.0]; ε=1.0, rev=true)[2], y)[1], label="Soft sort l2 derivative")

plot

plot(x -> sort([0.0, x, 1.0, 2.0]; rev=true)[2], label="Sort")
plot!(x -> soft_sort_kl([0.0, x, 1.0, 2.0]; rev=true)[2], label="Soft sort kl")
plot!(y -> gradient(x -> soft_sort_kl([0.0, x, 1.0, 2.0]; ε=1.0, rev=true)[2], y)[1], label="Soft sort kl derivative")

plot

plot(x -> ranking([x, 3.0, 1.0, 2.0]; rev=true)[1], label="Ranking")
plot!(x -> soft_rank_l2([x, 3.0, 1.0, 2.0]; rev=true)[1], label="Soft rank l2")
plot!(y -> gradient(x -> soft_rank_l2([x, 3.0, 1.0, 2.0]; ε=1.0, rev=true)[1], y)[1], label="Soft rank l2 derivative")

plot

plot(x -> ranking([x, 3.0, 1.0, 2.0]; rev=true)[1], label="Ranking")
plot!(x -> soft_rank_kl([x, 3.0, 1.0, 2.0]; rev=true)[1], label="Soft rank kl")
plot!(y -> gradient(x -> soft_rank_kl([x, 3.0, 1.0, 2.0]; ε=1.0, rev=true)[1], y)[1], label="Soft rank kl derivative")

plot

@codecov-commenter
Copy link

codecov-commenter commented Aug 24, 2023

Codecov Report

Patch coverage: 91.66% and project coverage change: +1.95% 🎉

Comparison is base (f9d8dab) 85.34% compared to head (ff85e4a) 87.29%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #88      +/-   ##
==========================================
+ Coverage   85.34%   87.29%   +1.95%     
==========================================
  Files          18       23       +5     
  Lines         389      559     +170     
==========================================
+ Hits          332      488     +156     
- Misses         57       71      +14     
Files Changed Coverage Δ
src/InferOpt.jl 100.00% <ø> (ø)
src/regularized/soft_rank.jl 60.00% <60.00%> (ø)
src/utils/isotonic_regression/isotonic_kl.jl 100.00% <100.00%> (ø)
src/utils/isotonic_regression/isotonic_l2.jl 100.00% <100.00%> (ø)
src/utils/isotonic_regression/projection.jl 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@BatyLeo BatyLeo marked this pull request as ready for review September 10, 2023 11:25
@BatyLeo BatyLeo added the enhancement New feature or request label Sep 11, 2023
test/soft_rank.jl Outdated Show resolved Hide resolved
@BatyLeo BatyLeo merged commit 65001da into main Sep 12, 2023
3 checks passed
@BatyLeo BatyLeo deleted the soft-sort branch September 12, 2023 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants