Differentiable reduction utility

I wrote a differentiable reduction utility in case someone needs it:

Consider it a short-term workaround for the lack of control flow differentiation.

Usage:

let xx = [1.0, 2.0, 3.0, 4.0, 5.0]
valueWithGradient(at: xx) { xx in
    xx.differentiableReduce(1) { $0 * $1 }
}

This produces:

▿ 2 elements
  - value : 120.0
  ▿ gradient : DifferentiableView
    ▿ _base : 5 elements
      - 0 : 120.0
      - 1 : 60.0
      - 2 : 40.0
      - 3 : 30.0
      - 4 : 24.0
4 Likes