Hi, I would like to optimize parameters of an analytic function to fit heterogeneous data (different experimental conditions, partly overlapping). I’m using index arrays to know which parameters to use for which data point. This seems problematic with Flux and Zygote, here’s a minimal example trying to get the parameter gradients. I didn’t know how to wrap the meta-information (last-index arrays, here id) so I put it in a nested function:
using Flux.Tracker # or: Zygote
function pred(f,id)
d = Vector{Float64}(undef,id[end])
i1 = 1
for (n,i2) in enumerate(id)
d[i1:i2] .= f[n] # dummy function depending on f[n]
i1 = i2+1
end
return d
end
function grad(f0,id,d)
loss(f) = sum(abs2.(d .- pred(f,id)))
gradient(loss,f0)
end
id = [2,3,5] # indices defining where parameters apply
f0 = [1.0,2.0,3.0] # parameters
d = pred(f0,id) # test data
grad(f0,id,d)
In Flux.Tracker, this gives me
ERROR: LoadError: MethodError: no method matching Float64(::Tracker.TrackedReal{Float64})
While Zygote complains about
ERROR: LoadError: Mutating arrays is not supported
Any (other) idea how to optimize “partly-global” parameters? Or what I’m doing wrong?