-
Notifications
You must be signed in to change notification settings - Fork 126
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
Differentiable ABMs with StochasticAD.jl #175
Comments
I know nothing about zygote, nor the data structures it needs. I can't add anything to the discussion here. :( p.s.: This issue is not well described. You should consider adding more information about the errors you get, what you are trying to do, a MWE, and more. |
Agent-based model are often very complex and one needs to run many simulations and replicates to explore parameter space and understand the behavior of a model. If we were able to differentiate AMBs with Automatic Differentiation (AD), we would be able to save a lot of time in finding parameter values that best fit data. I have read about AD in Julia, but I am not deeply familiar with its limitations. I tried to differentiate a simple ABM (Agent.jl's wealth distribution example). But I get the following error. I am not sure what part of the data structure we use are not supported. using Agents
using Zygote
using Statistics: std
mutable struct WealthAgent <: AbstractAgent
id::Int
wealth::Int
end
function wealth_model(;numagents = 100, initwealth = 1)
model = ABM(WealthAgent, scheduler=random_activation)
for i in 1:numagents
add_agent!(model, initwealth)
end
return model
end
function agent_step!(agent, model)
agent.wealth == 0 && return # do nothing
ragent = random_agent(model)
agent.wealth -= 1
ragent.wealth += 1
end
function costWealth(M)
agent_properties = [:wealth]
model = wealth_model(numagents=M)
step!(model, agent_step!, 5)
std([agent.wealth for agent in values(model.agents)])
end
gradient(N ->costWealth(N), 1000)
The error varies depending of the model though. |
I'm getting a core dump when I try this example
I haven't worked with AD in Julia that much myself, but I've never considered using it on discrete variables. What if you changed |
Thanks for the reply. Good point. I get the error below with Float64 for wealth. But this error seems more promising to be solvable.
|
I'm going to try and dig into this a little deeper. Completely agree it would be good to have this working. Luckily I've had a need to start working with Zygote myself recently, so hopefully we can get this done for v3 |
You could attempt to get the strong solution of some models, but in many ABMs the discreteness is inherently non-differentiable through AD, and you'd have to move to the continuous master equation PDE to get a differentiable form. |
Thank you, @ChrisRackauckas , for your reply. It is great to have the input of an expert on this issue. Is is possible to know when the discreteness is not a problem? |
this one might be relevant to this thread "Differentiable Agent-Based Simulation for |
That isn't a very great way to do it. That smoothing can introduce a lot of bias and has exponential cost scaling. We have a paper that will be out really soon that shows how to do this, along with a new AD in Julia that implements the method. I was wrong two years ago that it isn't possible: you just need an entirely different definition of automatic differentiation. |
@ChrisRackauckas this sounds very interesting. Is your paper out yet? |
Remind me tomorrow. The library should be out by the end of the week. |
Reminder :) |
Here it is: https://twitter.com/ChrisRackauckas/status/1582324437285625857 https://arxiv.org/abs/2210.08572 Reopen the issue 😄 |
Implementing a model that integrates with this as one of our "Integration Examples" would be lovely. :) Perhaps we can even provide some function(s) that make integration easy, as we did when we were writing the integration example with CellListMap.jl (#659 ) |
Interesting @ChrisRackauckas. We did similar work with the reparameterization trick to enable differentiable ABMs. Then, we also demonstrate how you can calibrate such large differentiable ABMs with deep neural networks (~10 million agents). https://arxiv.org/abs/2207.09714. Also, at MIT - I would love to chat further and share notes! |
What's the current status of this? Are there any blockers in linking Agents.jl with StochasticAD.jl? |
Noone has worked on this yet. |
EDIT: The issue title has been edited to showcase that there is now a formal solution on how to differentiate ABMs, see this comment. Implementing a model that integrates with this as one of our "Integration Examples" would be lovely. :)
Perhaps we can even provide some function(s) that make integration easy, as we did when we were writing the integration example with CellListMap.jl (#659 )
I have tried to use Zygote.jl to differentiate some random and small ABMs. But I get many different errors. Are there data structures that are not supported by Zygote yet? A differentiable ABM would be amazing and allowing to solve many problems.
The text was updated successfully, but these errors were encountered: