-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e01a2b1
commit 73eabbd
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "NonconvexCore" | ||
uuid = "035190e5-69f1-488f-aaab-becca2889735" | ||
authors = ["Mohamed Tarek <[email protected]> and contributors"] | ||
version = "1.0.6" | ||
version = "1.0.7" | ||
|
||
[deps] | ||
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# workaround Zygote.jacobian and ForwardDiff.jacobian not supporting sparse jacobians | ||
|
||
function sparse_jacobian(f, x) | ||
val, pb = Zygote.pullback(f, x) | ||
M, N = length(val), length(x) | ||
T = eltype(val) | ||
return copy(mapreduce(hcat, 1:M, init = spzeros(T, N, 0)) do i | ||
pb(I(M)[:, i])[1] | ||
end') | ||
end | ||
function sparse_hessian(f, x) | ||
return sparse_fd_jacobian(x -> Zygote.gradient(f, x)[1], x) | ||
end | ||
|
||
function sparse_fd_jacobian(f, x) | ||
pf = pushforward_function(f, x) | ||
M, N = length(f(x)), length(x) | ||
init = pf(I(M)[:, 1])[1] | ||
M = length(init) | ||
return mapreduce(hcat, 2:N; init) do i | ||
pf(I(M)[:, i])[1] | ||
end | ||
end | ||
|
||
# from AbstractDifferentiation | ||
function pushforward_function(f, xs...) | ||
return function pushforward(vs) | ||
if length(xs) == 1 | ||
v = vs isa Tuple ? only(vs) : vs | ||
return (ForwardDiff.derivative(h -> f(step_toward(xs[1], v, h)), 0),) | ||
else | ||
return ForwardDiff.derivative(h -> f(step_toward.(xs, vs, h)...), 0) | ||
end | ||
end | ||
end | ||
|
||
@inline step_toward(x::Number, v::Number, h) = x + h * v | ||
# support arrays and tuples | ||
@noinline step_toward(x, v, h) = x .+ h .* v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73eabbd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
73eabbd
There was a problem hiding this comment.
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/62361
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: