sbromberger / LightGraphs.jl

An optimized graphs package for the Julia programming language
Other
670 stars 185 forks source link

autodiff with LightGraphs? #1599

Closed andreaskoher closed 2 years ago

andreaskoher commented 2 years ago

Hey all,

I would love to use LG for an inference problem but unfortunately I dont get autodiff to work. Take the following simple example:

(1) generate a random graph with edge probability p = 0.2 (2) observe edge density (3) compare with taget density (4) propagate gradient with respect to p


using LightGraphs
import ForwardDiff

observed_density(p) = p -> density( erdos_renyi(100, p) )
target_density = 0.1

ForwardDiff.gradient( p -> ( target_density - observed_density(p...) )^2, [0.2]) #[0.] 

The gradient with respect to p is zero using ForwardDiff and ReverseDiff. Any idea, where autodiff is blocked?

jpfairbanks commented 2 years ago

I'm not sure how AutoDiff works with randomization, but graph generators draw a bunch of random numbers in order to sample the graph. They are basically just rand and add_edge!

andreaskoher commented 2 years ago

Many thanks! Apparently, the issue here is my misunderstanding of autodiff :)