Please I am working on a physical system, and after running simulations I have 2 arrays of variables like this:
D = [0.01, 0.02, …, 0.05]
m = [1, 2, …, 5]
Please how do I find the derivative (∂D/∂m) using Zygote, given only the above 2 arrays of values. I have checked the docs and I do not understand it. I do not have a function, so I am thinking Zygote could compute the derivative directly.
Hello @alegeaa and welcome!
If you don’t have a function, you cannot compute a derivative. Indeed, the arrays D and m might have been generated by an arbitrary process we know nothing about.
However, what I mean by “function” is quite fuzzy, and it could encompass physical simulators. Do you have access to one or not at all?
Hello @gdalle thank YOU FOR YOUR REPLY. I have access to all, i.e. both D and m arrays. I wrote a function to compute the derivative numerically by this formula, ∂D/∂m = [D(m1+delta/2) - D(m1-delta/2)]/ delta. However, I would like to use Zygote for this. Any directions please?
I have a function that uses the D and m arrays to compute the differentials for corresponding D and m pairs in the arrays. The function looks like this:
function calc_differentials(delta, diameters, masses)
diffs = []
for i in range(1, length(diameters))
diff = ((diameters[i] * (masses[i] + delta / 2)) - (diameters[i] * (masses[i] - delta / 2))) / delta
append!(diffs, diff)
end;
return diffs
end;
So I understand, you are implying that I do sth like:
for i in range(1, length(diameters))
JacD = Zygote.jacobian(D[i], m[I])[1]
end
OK, so I think I’m starting to understand your problem. The formula you’re using only makes sense when D is a function that you apply to m and not just a value.
To clarify, let me change notations, and suppose you want to compute the derivative of f: x \longmapsto y. The method of finite differences tells you
One reason is that without the function f, you really cannot say anything about the relation between x and y. For instance, consider x = 0 and y = 0:
This could be the result of f_1(x) = x, in which case f_1'(x) = 1
This could be the result of f_2(x) = -x, in which case f_2'(x) = -1
The same goes if you have several input-ouput pairs (x_i, y_i). You need to have access to the function to differentiate it, whether you use finite differences, symbolic differentiation or automatic differentiation (with Zygote or ForwardDiff)
So my message above tells you why your function calc_differentials is not correct. You need access to the function compute_D if you want to retrieve any useful derivative information.
Supposing you do, then you would compute the derivative at a point m using the following syntax:
i = 3
Zygote.gradient(compute_D, m[i])[1]
I suggested jacobian because I thought compute_D took a vector as input and returned another vector. If the output is a real number, gradient is the way to go.
Thank you so much. I understand the solution now. However, my compute_D function returns an array of all the D’s in the simulation. Also, the function has 4 arguments, can I do sth like this:
i = 3
Zygote.gradient(compute_D(arg1, arg2, arg3, arg4), m[i])[1]