|
| 1 | +""" |
| 2 | + within_gradient(x) --> Bool |
| 3 | +
|
| 4 | +Returns `false` except when used inside a `gradient` call, when it returns `true`. |
| 5 | +Useful for Flux regularisation layers which behave differently during training and inference. |
| 6 | +
|
| 7 | +This should work with any ChainRules-based differentiation package, in which case `x` is ignored. |
| 8 | +But Tracker.jl overloads `with_gradient(x::TrackedArray)`, thus for widest use you should |
| 9 | +pass it an array whose gradient is of interest. |
| 10 | +There is also an overload for ForwardDiff.jl's `Dual` types (and arrays of them). |
| 11 | +
|
| 12 | +# Examples |
| 13 | +``` |
| 14 | +julia> using ForwardDiff, Zygote, NNlib |
| 15 | +
|
| 16 | +julia> f_good(x) = if NNlib.within_gradient(x) |
| 17 | + @show 10x |
| 18 | + else |
| 19 | + x |
| 20 | + end; |
| 21 | +
|
| 22 | +julia> Zygote.withgradient(f_good, 1.0) |
| 23 | +10x = 10.0 |
| 24 | +(val = 10.0, grad = (10.0,)) |
| 25 | +
|
| 26 | +julia> ForwardDiff.derivative(f_good, 1.0) |
| 27 | +10x = Dual{ForwardDiff.Tag{typeof(f_good), Float64}}(10.0,10.0) |
| 28 | +10.0 |
| 29 | +
|
| 30 | +julia> f_bad(x, y) = if any(NNlib.within_gradient, (x, y)) |
| 31 | + @show x * y |
| 32 | + else |
| 33 | + x / y |
| 34 | + end; |
| 35 | +
|
| 36 | +julia> Zygote.withgradient(f_bad, 2.0, 3.0) |
| 37 | +(val = 0.6666666666666666, grad = (0.3333333333333333, -0.2222222222222222)) |
| 38 | +
|
| 39 | +julia> ForwardDiff.derivative(x -> f_bad(x, 3.0), 2.0) |
| 40 | +x * y = Dual{ForwardDiff.Tag{var"#9#10", Float64}}(6.0,3.0) |
| 41 | +3.0 |
| 42 | +``` |
| 43 | +
|
| 44 | +What goes wrong in `f_bad` is that Zygote knows `any` to be non-differentiable, |
| 45 | +and thus completely ignores its contents. This is not a perfect mechanism, |
| 46 | +and the only style recommended is precisely that of `f_good` above. |
| 47 | +""" |
| 48 | +within_gradient(x) = false |
| 49 | + |
| 50 | +ChainRulesCore.rrule(::typeof(within_gradient), x) = true, _ -> (NoTangent(), NoTangent()) |
| 51 | + |
| 52 | + |
1 | 53 | """
|
2 | 54 | safe_div(x, y)
|
3 | 55 |
|
|
0 commit comments