You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/nondiff_points.md
+1-141Lines changed: 1 addition & 141 deletions
Original file line number
Diff line number
Diff line change
@@ -138,144 +138,4 @@ These rough rules are:
138
138
- If the derivative from one side is finite and the other isn't, say it is the derivative taken from finite side.
139
139
- When derivative from each side is not equal, strongly consider reporting the average
140
140
141
-
Our goal as always, is to get a pragmatically useful result for everyone, which must by necessity also avoid a pathological result for anyone.
142
-
143
-
144
-
145
-
### sub/super-differential convention
146
-
**TODO: Incorperate this with rest of the document. Or move to design notes**
147
-
148
-
Instead we introduce what we call the **sub/super-differential convention**:
149
-
150
-
> It is always permissible to return any element of the sub/super-differential.
151
-
> You should return the most useful.
152
-
153
-
Or equivalently but considering the trivial singleton sub/super-differential seperately:
154
-
155
-
> At any point where the derivative is not defined, it is permissible to return any element of the sub/super-differential.
156
-
> You should return the most useful.
157
-
158
-
We will justify this further below, but first let us discuss what it means.
159
-
160
-
### What is the sub/super-differential?
161
-
162
-
The subderivative is defined only for locally convex functions, whereas the super-derivative is defined only for locally concave functions.
163
-
For our purpose we basically never care which we are working with and so write sub/super-derivative.
164
-
165
-
For a function $f$ at some point $x_0$ a sub/super-derivative is a real number $c$ such that there exists a open ball $\mathcal{B} \subset \mathrm{dom}(f)$ containing $x_0$,
166
-
and for all points $z \in \mathcal{B}$ the following holds:
We call the the set of all values $c$ the sub/super-differential at $x_0$.
173
-
174
-
More informally: consider a plot of the function.
175
-
The sub/super-differential at a point is the set of slopes of all lines you could draw touching that point, but with the lines either entirely above, or entirely below the curve.
In this figure a plot of a function is shown in blue.
182
-
Two subtangent lines are plotted in red.
183
-
Their slopes are sub/super-derivatives at $x_0$, and they are two elements of the subdifferential.
184
-
If you flip it upside down, it would apply for the super-differential, with the lines being above the curve.
185
-
186
-
### Some key things to note.
187
-
188
-
For a function where the derivative is defined on both sides of the point:
189
-
- the derivative on each side is a sub/super-derivative at the point
190
-
- as is the mean of the derivative of each side
191
-
- in-fact the mean of any subset (including the whole sub/super-differential) of sub/super-derivatives is also a sub/super-derivative.
192
-
- if the derivative one one side is negative and the other positive then zero is a sub/super-derivative.
193
-
194
-
For a function that is only defined on one side of the point, the sub/super-differential is the full set of real numbers.
195
-
This by the subgradient convention leaves you free to chose *any* useful value.
196
-
197
-
!!! info "Does AD always return a sub/super-derivative? No"
198
-
On consideration of this, one might be tempted to think that AD always returns a sub/super-derivative.
199
-
And that in normal cases it return the only sub/super-derivative i.e. the actual derivative; and in other case it picks one of the branches.
200
-
Thus all weirdness of AD disagreeing with calculus could be explained away in this way.
201
-
**This is not the case.**
202
-
As it is not necessarily true that all branches derivatives are correct sub/super-differentials for the function.
203
-
Consider the function from [Abadi and Plotkin, 2019](https://dl.acm.org/doi/10.1145/3371106):
204
-
`f(x::Float64) = x == 0.0 ? 0.0 : x`.
205
-
The correct derivative of this function is `1` at all points, but most ADs will say that at `x=0` the derivative is zero.
206
-
The fix for this, is to define a rule which *does* in fact return a sub/super-derivative.
207
-
So one could say a correctly functioning AD with all needed rules does always return a sub/super-differential.
208
-
209
-
210
-
## What is useful ?
211
-
212
-
The sub/super-differential convention had two parts:
213
-
"It is always permissable to return any element of the sub/super-differential.
214
-
**You should return the most useful.**".
215
-
What is the most useful?
216
-
This is a value judgement you as a rule author will have to make.
217
-
218
-
219
-
### It is often zero
220
-
221
-
If zero is a sub/super-derivative, then it is often the most useful one.
222
-
Especially if the point is a local minima/maxima
223
-
For a function like `abs` or `x -> x<0 ? x : -2x` setting the non-differentiable point to either side would result in it leaving that maxima.
224
-
225
-
Further, a nice (albeit generally intractable) algorithm for finding the global optima is to take the set of all stationary points (i.e. points where the derivative is zero), combine that with the boundary points and evaluate the primal function at all of them.
226
-
The extrema of this set are the global optima.
227
-
This algorithm is only guaranteed correct for functions that are differentiable everywhere *or* that apply the sub/super-derivative convention and make all non-differentiable local optima claim to have a zero derivative.
228
-
It is also correct if you make other non-differentiable points have zero derivative, just slower.
229
-
230
-
### It is sometimes the non-zero, especially if it boarders a flat section
231
-
232
-
If a function has derivative zero in some large section of it's domain, like `relu` or `x->clamp(x, -1, 1)`,
233
-
a case can be made for choosing the non-zero derivative branch.
234
-
Depending exactly on the larger function being optimized, it is often the case that a zero gradient for this function means a total zero gradient (e.g. if the greater function is a chain of composed calls).
235
-
So once things move into the flat-region they stay there.
236
-
If we chose the other branch we move them into (just barely) the nonflat region.
237
-
If moving into the non-flat region was good they will move further there later.
238
-
If it is not good then they well be rapidly cast deeper into the flat region and we will not be at this boundary case.
239
-
240
-
### It is sometimes the mean
241
-
Either the mean of the branches, or the overall mean of the sub/super-differential (which may be equal).
242
-
243
-
A nice advantage of it is that it will agree with the result you will get from central finite differencing.
244
-
Which is what [ChainRulesTestUtils.jl](https://github.com/JuliaDiff/ChainRulesTestUtils.jl) defaults to.
245
-
246
-
### All else being equal it is just one of the branches
247
-
Pick one.
248
-
It will be fast.
249
-
It will agree with either the forwards or reverse finite differencing results.
250
-
251
-
### It is almost never `Inf`, `NaN` or an error
252
-
Practically speaking, people are generally using AD to perform some gradient descent like optimization procedure.
253
-
`NaN` and `Inf` do not generally take us to nice places.
254
-
Erroring can be worse, especially if it is a local minima -- the optimization fully converges to that minima and then throws an error rather than reporting the result.
255
-
If the primal function takes a non-finite value or errors on one side, then we are in the case that we are at a boundary of the domain.
256
-
Which means we are free to chose *any* value.
257
-
In particular we often want to chose value of the derivative from **the other side where the primal is defined.**
258
-
259
-
260
-
## Why is the sub/super-differential convention permissible?
261
-
262
-
Math isn't real, and AD doesn't do calculus.
263
-
We are trying to accomplish some goal, and this approach works well.
264
-
265
-
One way to think about this convention is to take an infinitely sharp discontinuity in a function and replace that discontinuty with a very (infinitesimally) small, smooth corner.
266
-
Due to the intermediate value theorem, we can then say that over this tiny interval, any derivative between the two extremes is realized and we are free to pick any one of them that we find useful as the 'canonical' value.
267
-
268
-

0 commit comments