Skip to content

Instantly share code, notes, and snippets.

@cossio
Created October 1, 2021 11:56
Show Gist options
  • Save cossio/106d65b591b11b646d1236184e9baed2 to your computer and use it in GitHub Desktop.
Save cossio/106d65b591b11b646d1236184e9baed2 to your computer and use it in GitHub Desktop.
Testing rrules
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