-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rrule for map and expand the testing framework
This implements `rrule(map, f, xs...)` and expands `rrule_test` to allow non-differentiable arguments. For such cases, the user need only pass `nothing` as the argument's assumed sensitivity.
- Loading branch information
Showing
6 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
##### | ||
##### `map` | ||
##### | ||
|
||
function rrule(::typeof(map), f, xs...) | ||
y = map(f, xs...) | ||
∂xs = ntuple(length(xs)) do i | ||
Rule() do ȳ | ||
map(ȳ, xs...) do ȳi, xis... | ||
r = rrule(f, xis...) | ||
if r === nothing | ||
throw(ArgumentError("can't differentiate `map` with `$f`; no `rrule` " * | ||
"is defined for `$f$xis`")) | ||
end | ||
_, ∂xis = r | ||
extern(∂xis[i](ȳi)) | ||
end | ||
end | ||
end | ||
return y, (DNERule(), ∂xs...) | ||
end | ||
|
||
##### | ||
##### `sum` | ||
##### | ||
|
||
frule(::typeof(sum), x) = (sum(x), Rule(sum)) | ||
|
||
rrule(::typeof(sum), x) = (sum(x), Rule(cast)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@testset "Maps and Reductions" begin | ||
@testset "map" begin | ||
rng = MersenneTwister(42) | ||
n = 10 | ||
x = randn(rng, n) | ||
vx = randn(rng, n) | ||
ȳ = randn(rng, n) | ||
rrule_test(map, ȳ, (sin, nothing), (x, vx)) | ||
rrule_test(map, ȳ, (+, nothing), (x, vx), (randn(rng, n), randn(rng, n))) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters