Skip to content

Commit

Permalink
Wct/fix repeats euclidean (#1307)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix repeated inputs

* Remove comment

* Undo change

* Uncomment tests

* Bump patch version

* Defensive choice of zero

Co-authored-by: Miha Zgubic <[email protected]>

Co-authored-by: Miha Zgubic <[email protected]>
  • Loading branch information
willtebbutt and mzgubic authored Sep 21, 2022
1 parent 4e10cea commit b13d595
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Zygote"
uuid = "e88e6eb3-aa80-5325-afca-941959d7151f"
version = "0.6.47"
version = "0.6.48"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
17 changes: 11 additions & 6 deletions src/lib/distances.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ end
end

@adjoint function pairwise(::Euclidean, X::AbstractMatrix; dims=2)
D, back = pullback(X -> pairwise(SqEuclidean(), X; dims = dims), X)
D .= sqrt.(D)
return D, function(Δ)
Δ = Δ ./ (2 .* max.(D, eps(eltype(D))))
Δ[diagind(Δ)] .= 0
return (nothing, first(back(Δ)))

_conditional(d, δ) = d > δ ? sqrt(d) : zero(d)

function _pairwise_euclidean(X)
δ = eps(eltype(X))^2
D2 = pairwise(SqEuclidean(), X; dims=dims)
return _conditional.(D2, δ)
end
D, back = pullback(_pairwise_euclidean, X)

_pairwise_pullback(Δ) = (nothing, back(Δ)...)
return D, _pairwise_pullback
end
13 changes: 13 additions & 0 deletions test/gradcheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,19 @@ end
# This is impressively inaccurate, but at least it doesn't produce a NaN.
@test first(Δ_fd) first(pb(Δ)) atol=1e-3 rtol=1e-3
end

@testset "repeated X" begin
Δ = randn(P, P)
X = repeat(randn(rng, D), 1, P)

Δ_fd = FiniteDifferences.j′vp(
FiniteDifferences.central_fdm(5, 1), X -> pairwise(metric, X; dims=2), Δ, X
)
_, pb = Zygote.pullback(X -> pairwise(metric, X; dims=2), X)

# This is impressively inaccurate, but at least it doesn't produce a NaN.
@test first(Δ_fd) first(pb(Δ)) atol=1e-3 rtol=1e-3
end
end

@testset "binary pairwise - X and Y close" begin
Expand Down

2 comments on commit b13d595

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/68695

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.48 -m "<description of version>" b13d5953952e3c134ec2cd60023366e67eb215fc
git push origin v0.6.48

Please sign in to comment.