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

Simplify rule according to feedback in #531 #549

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@

function ChainRulesCore.rrule(s::Sinus, x::AbstractVector, y::AbstractVector)
d = x - y
sind = sinpi.(d)
abs2_sind_r = abs2.(sind) ./ s.r .^ 2
abs2_sind_r = (sinpi.(d) ./ s.r) .^ 2
val = sum(abs2_sind_r)
gradx = twoπ .* cospi.(d) .* sind ./ s.r .^ 2
gradx = π .* sinpi.(2 .* d) ./ s.r .^ 2
function evaluate_pullback(Δ::Any)
r̄ = -2Δ .* abs2_sind_r ./ s.r
s̄ = ChainRulesCore.Tangent{typeof(s)}(; r=r̄)
Expand All @@ -136,7 +135,7 @@
for j in 1:n, i in 1:n
xi = view(x, i, :)
xj = view(x, j, :)
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- xj) .* cospi.(xi .- xj) ./ d.r .^ 2
ds = π .* Δ[i, j] .* sinpi.(2 .* (xi .- xj)) ./ d.r .^ 2

Check warning on line 138 in src/chainrules.jl

View check run for this annotation

Codecov / codecov/patch

src/chainrules.jl#L138

Added line #L138 was not covered by tests
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- xj) .^ 2 ./ d.r .^ 3
x̄[i, :] += ds
x̄[j, :] -= ds
Expand All @@ -147,8 +146,8 @@
xj = view(x, :, j)
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- xj) .* cospi.(xi .- xj) ./ d.r .^ 2
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- xj) .^ 2 ./ d.r .^ 3
x̄[:, i] += ds
x̄[:, j] -= ds
x̄[:, i] .+= ds
x̄[:, j] .-= ds
end
end
d̄ = ChainRulesCore.Tangent{typeof(d)}(; r=r̄)
Expand All @@ -173,19 +172,19 @@
for j in 1:m, i in 1:n
xi = view(x, i, :)
yj = view(y, j, :)
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- yj) .* cospi.(xi .- yj) ./ d.r .^ 2
ds = π .* Δ[i, j] .* sinpi.(2 .* (xi .- yj)) ./ d.r .^ 2

Check warning on line 175 in src/chainrules.jl

View check run for this annotation

Codecov / codecov/patch

src/chainrules.jl#L175

Added line #L175 was not covered by tests
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- yj) .^ 2 ./ d.r .^ 3
x̄[i, :] += ds
ȳ[j, :] -= ds
x̄[i, :] .+= ds
ȳ[j, :] .-= ds

Check warning on line 178 in src/chainrules.jl

View check run for this annotation

Codecov / codecov/patch

src/chainrules.jl#L177-L178

Added lines #L177 - L178 were not covered by tests
end
elseif dims == 2
for j in 1:m, i in 1:n
xi = view(x, :, i)
yj = view(y, :, j)
ds = twoπ .* Δ[i, j] .* sinpi.(xi .- yj) .* cospi.(xi .- yj) ./ d.r .^ 2
ds = π .* Δ[i, j] .* sinpi.(2 .* (xi .- yj)) ./ d.r .^ 2
r̄ .-= 2 .* Δ[i, j] .* sinpi.(xi .- yj) .^ 2 ./ d.r .^ 3
x̄[:, i] += ds
ȳ[:, j] -= ds
x̄[:, i] .+= ds
ȳ[:, j] .-= ds
end
end
d̄ = ChainRulesCore.Tangent{typeof(d)}(; r=r̄)
Expand All @@ -208,10 +207,10 @@
for i in 1:n
xi = view(x, :, i)
yi = view(y, :, i)
ds = twoπ .* Δ[i] .* sinpi.(xi .- yi) .* cospi.(xi .- yi) ./ d.r .^ 2
ds = π .* Δ[i] .* sinpi.(2 .* (xi .- yi)) ./ d.r .^ 2
r̄ .-= 2 .* Δ[i] .* sinpi.(xi .- yi) .^ 2 ./ d.r .^ 3
x̄[:, i] += ds
ȳ[:, i] -= ds
x̄[:, i] .+= ds
ȳ[:, i] .-= ds
end
d̄ = ChainRulesCore.Tangent{typeof(d)}(; r=r̄)
return NoTangent(), d̄, @thunk(project_x(x̄)), @thunk(project_y(ȳ))
Expand Down
Loading