-
Notifications
You must be signed in to change notification settings - Fork 45
Automatic Differentiation with coloring #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ authors = ["Pankaj Mishra <[email protected]>"] | |
version = "0.1.0" | ||
|
||
[deps] | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f" | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||
using ForwardDiff: Dual, jacobian, partials | ||||||
|
||||||
function compute_jacobian!(J::AbstractMatrix{<:Number}, | ||||||
f, | ||||||
x::AbstractArray{<:Number}; | ||||||
color=1:length(x)) | ||||||
|
||||||
partials_array = Array{Float64}(undef, length(x), maximum(color)) | ||||||
for color_i in 1:maximum(color) | ||||||
for i in 1:length(x) | ||||||
if color[i]==color_i | ||||||
partials_array[i,color_i] = 1 | ||||||
else | ||||||
partials_array[i,color_i] = 0 | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
p = Tuple.(eachrow(partials_array)) | ||||||
t = zeros(Dual{Nothing, Float64, maximum(color)},0) | ||||||
for i in 1:length(x) | ||||||
push!(t, Dual(x[i], p[i])) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
end | ||||||
|
||||||
fx = similar(t) | ||||||
f(fx, t) | ||||||
|
||||||
rows_index, cols_index, val = findnz(J) | ||||||
for color_i in 1:maximum(color) | ||||||
du = partials.(fx,color_i) | ||||||
for i in 1:length(cols_index) | ||||||
if color[cols_index[i]]==color_i | ||||||
J[rows_index[i],cols_index[i]] = du[rows_index[i]] | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
J = Array(J) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't needed. |
||||||
|
||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using SparseDiffTools | ||
using SparseArrays | ||
using ForwardDiff: Dual, jacobian | ||
|
||
fcalls = 0 | ||
function f(dx,x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer 4 spaces for indentation |
||
global fcalls += 1 | ||
for i in 2:length(x)-1 | ||
dx[i] = x[i-1] - 2x[i] + x[i+1] | ||
end | ||
dx[1] = -2x[1] + x[2] | ||
dx[end] = x[end-1] - 2x[end] | ||
nothing | ||
end | ||
|
||
function second_derivative_stencil(N) | ||
A = zeros(N,N) | ||
for i in 1:N, j in 1:N | ||
(j-i==-1 || j-i==1) && (A[i,j]=1) | ||
j-i==0 && (A[i,j]=-2) | ||
end | ||
A | ||
end | ||
|
||
x = rand(30) | ||
dx = rand(30) | ||
|
||
J = jacobian(f, dx, x) | ||
@test J ≈ second_derivative_stencil(30) | ||
_J = sparse(J) | ||
@test fcalls == 3 | ||
|
||
fcalls = 0 | ||
_J1 = similar(_J) | ||
compute_jacobian!(_J1, f, x, color = repeat(1:3,10)) | ||
@test _J1 ≈ J | ||
@test fcalls == 1 |
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.
It's usually better to pre-allocate. The first part here being
typeof(f)
will tag the Duals so they won't accidentally overlap with other differentiations.