-
Notifications
You must be signed in to change notification settings - Fork 15
/
finite_difference_calls.jl
89 lines (75 loc) · 2.82 KB
/
finite_difference_calls.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
_make_jvp_call(fdm, f, y, xs, ẋs, ignores)
Call `FiniteDifferences.jvp`, with the option to ignore certain `xs`.
# Arguments
- `fdm::FiniteDifferenceMethod`: How to numerically differentiate `f`.
- `f`: The function to differentiate.
- `y`: The primal output `y=f(xs...)` or at least something of the right type
- `xs`: Inputs to `f`, such that `y = f(xs...)`.
- `ẋs`: The directional derivatives of `xs` w.r.t. some real number `t`.
- `ignores`: Collection of `Bool`s, the same length as `xs` and `ẋs`.
If `ignores[i] === true`, then `ẋs[i]` is ignored for derivative estimation.
# Returns
- `Ω̇`: Derivative of output w.r.t. `t` estimated by finite differencing.
"""
function _make_jvp_call(fdm, f, y, xs, ẋs, ignores)
f2 = _wrap_function(f, xs, ignores)
ignores = collect(ignores)
all(ignores) && return ntuple(_ -> NoTangent(), length(xs))
sigargs = zip(xs[.!ignores], ẋs[.!ignores])
return ProjectTo(y)(jvp(fdm, f2, sigargs...))
end
"""
_make_j′vp_call(fdm, f, ȳ, xs, ignores) -> Tuple
Call `FiniteDifferences.j′vp`, with the option to ignore certain `xs`.
# Arguments
- `fdm::FiniteDifferenceMethod`: How to numerically differentiate `f`.
- `f`: The function to differentiate.
- `ȳ`: The adjoint w.r.t. output of `f`.
- `xs`: Inputs to `f`, such that `y = f(xs...)`.
- `ignores`: Collection of `Bool`s, the same length as `xs`.
If `ignores[i] === true`, then `xs[i]` is ignored; `∂xs[i] === NoTangent()`.
# Returns
- `∂xs::Tuple`: Derivatives estimated by finite differencing.
"""
function _make_j′vp_call(fdm, f, ȳ, xs, ignores)
f2 = _wrap_function(f, xs, ignores)
ignores = collect(ignores)
args = Any[NoTangent() for _ in 1:length(xs)]
all(ignores) && return (args...,)
sigargs = xs[.!ignores]
arginds = (1:length(xs))[.!ignores]
fd = j′vp(fdm, f2, ȳ, sigargs...)
@assert length(fd) == length(arginds)
for (dx, ind) in zip(fd, arginds)
args[ind] = ProjectTo(xs[ind])(dx)
end
return (args...,)
end
"""
_wrap_function(f, xs, ignores)
Return a new version of `f`, `fnew`, that ignores some of the arguments `xs`.
# Arguments
- `f`: The function to be wrapped.
- `xs`: Inputs to `f`, such that `y = f(xs...)`.
- `ignores`: Collection of `Bool`s, the same length as `xs`.
If `ignores[i] === true`, then `xs[i]` is ignored; `∂xs[i] === NoTangent()`.
"""
function _wrap_function(f, xs, ignores)
function fnew(sigargs...)
callargs = Any[]
j = 1
for (i, (x, ignore)) in enumerate(zip(xs, ignores))
if ignore
push!(callargs, x)
else
push!(callargs, sigargs[j])
j += 1
end
end
@assert j == length(sigargs) + 1
@assert length(callargs) == length(xs)
return f(callargs...)
end
return fnew
end