110 points · by noelwelsh · 6y ago · 42 comments
I haven't seen this discussed before, except in the context of category theory (https://ncatlab.org/nlab/show/differentiation) I'm not sure how this ties into the programming language point of view.
If this "explanation" (scare quotes because it's not really an explanation, just a code dump) doesn't work for you let me know and I can try a different approach.
final case class AD[A](v: A, k: Double => Double) {
// Chain rule
def flatMap[B](f: A => AD[B]): AD[B] = {
val self = this
val next = f(v)
AD(next.v, (d: Double) => next.k(d) * self.k(d))
}
def +(that: AD[Double])(implicit ev: A =:= Double): AD[Double] =
AD(this.v + that.v, (d: Double) => this.k(d) + that.k(d))
def *(that: AD[Double])(implicit ev: A =:= Double): AD[Double] =
AD(this.v * that.v, (d: Double) => (this.k(d) * that.v) + (that.k(d) * this.v))
def sin(implicit ev: A =:= Double): AD[Double] =
this.flatMap(x => AD(Math.sin(x), (d) => Math.cos(x) * d))
def gradient: Double =
this.k(1.0)
}
object AD {
def pure(x: Double): AD[Double] =
AD(x, d => 1.0)
}Shouldn't M actually be a monoid under composition? ie. you should compose like next.k(self.k(d)), not multiply.
Thanks for this paper. Along with the string of "compiling to categories" papers, our understanding of AD has improved greatly.
Is there some parenty between the ideas ?
> The implementation proposed by Pearlmutter and Siskind returns a pair of a value and a backpropagator ... Doing this correctly requires a non-local program transformation ... Further tweaks arerequired if a lambda uses variables from an outer scope ... In contrast to Pearlmutter and Siskind [2008], using delimited continuations enables reverse-mode AD with only local transformations. Any underlying non-local transformations are implicitly resolved by shift and reset.
I'll have to look more carefully to understand how the CPS version avoids non-local program transformations.
http://h2.jaguarpaw.co.uk/posts/automatic-differentiation-wo...
val x = ...
ys.map(y => y + x)
Each loop iteration needs to contribute a gradient update to x, which is defined in an outer scope. And what if y => y + x is not given as an inline lambda, but defined elsewhere. It doesn't seem like your blog posts discusses any such cases.Could you elaborate on what leads you to think this?
With more and more cores coming, I expect to see this sort of thing happen once we accept that we can’t really use all of them.
Reminds me of Dynimize https://news.ycombinator.com/item?id=17025627
I think a few compilers sprouted the ability to take perf logs from previous runs into account when compiling, maybe ten years back now, but nothing is there for off-line optimization in situ. And again my choice of input data will never be perfect for all of my users. I can tune for my biggest customer or be populist.
Consider your thermostat. What do you optimize for daily with it? Is it the same in winter as summer? Why not?
To that end, why do we think there is an "optimum" setting?