Created
October 1, 2021 11:56
-
-
Save cossio/106d65b591b11b646d1236184e9baed2 to your computer and use it in GitHub Desktop.
Testing rrules
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
using ChainRulesCore, ChainRulesTestUtils, Test | |
function mylog(x::Real) | |
r = log(abs(x)) | |
if x > 0 | |
return r | |
else | |
return oftype(r, -Inf) | |
end | |
end | |
function ChainRulesCore.rrule(::typeof(mylog), x) | |
function mylog_pullback(δ) | |
if x > 0 | |
dx = 1/x | |
else | |
dx = oftype(1/x, Inf) | |
end | |
return NoTangent(), dx | |
end | |
return mylog(x), mylog_pullback | |
end | |
test_rrule(mylog, 1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment