Function minimization with dfx

We are slowly approaching the holiday season, which means two things: it is a time for gifts, and the yearly return of the F# Advent Calendar! Thank you Sergey Tihon for organizing this once again.

Santa was good to me, with a very nice early surprise: the release of dfx. dfx, an F# automatic differentiation library, did not appear in a vacuum: it is the continuation of DiffSharp, the wonderful work of Dr. Atılım Güneş Baydin. As such, it uses the same core concepts as DiffSharp, making it easy to pick up if you are familiar with it, but brings renewed energy into the project, which is awesome news.

So for this F# Advent post, I figured I would take a look at dfx, from the perspective of “simple” function minimization. This is a very narrow angle on what dfx has to offer. In particular, dfx includes very elegant building blocks geared towards specifying and training neural networks. However, minimizing a bog-standard function is a problem I encounter regularly, and a problem where dfx shines, because one classic technique - gradient descent - relies on differentiation.

Note: this post is revisiting and updating an older post of mine.

Why care about function minimization

For since the fabric of the universe is most perfect, and is the work of a most wise Creator, nothing whatsoever takes place in the universe in which some relation of maximum and minimum does not appear.
Leonhard Euler

The universe being most perfect is what the kids today would call a hot take by Leonhard Euler. However, many interesting problems can indeed be seen through the lens of minimization or maximization.

More...

Study notes: automatic differentiation (part 3)

The side quest into automatic differentiation continues! In my previous post, I attempted a different design direction, and landed on something promising. Instead of representing functions using expressions, I introduced a type, V, which “augmented” floating point values to carry both their value, x, together with the evaluation of their derivative, dx.

After adding a battery of math functions to the type V, we could then do the following:

let f (x: V) = sin (x + 42.0)
let f' = diff f
f' 10.0
> val it: float = -0.1629907808

While this felt like an improvement, this has a major issue. We can get the value of f', which is great, but we can’t derive further. If we were for instance interested in the second order derivative, f'', we would be out of luck. So let’s see if we can fix that.

The root of the problem

The reason we can’t differentiate repeatedly boils down to this:

  • to apply the diff function we need to pass in a function V -> V,
  • the diff function returns a function float - float.

To get out of this pickle, we would need to transform the diff function into something like this:

diff: (V -> V) -> (V -> V)

So how do we do that?

More...

Study notes: automatic differentiation (part 2)

In my last post, I began my side quest into automatic differentiation, trying to implement it from scratch as a learning exercise, in F#, to understand how it works.

In part 1, I decided to start with the smallest relevant problem I could think of, computing the derivative of a function of a single value. Once we get that figured out, we will move to bigger functions.

My first take was to model functions as expressions, using an F# discriminated union. This was a useful exercises. First, it worked! I ended up with a function diff that correctly evaluated the derivative of a function. Then, implementing it gave me a chance to refresh my mind on the mechanics of derivatives.

There are a few things I didn’t like about that take. First, it requires writing functions as expressions, essentially using a DSL, which is a bit awkward to use, and not ideal for extensibility. Then, it does not handle composition very well. Ideally, I would like to be able to compute the derivative of a derivative, and so on. I can achieve that by modifying my initial code a little, but that modification is essentially symbolic derivation, which is, as I understand it, not automatic differentiation.

So let’s go back to the drawing board, with take 2.

More...

Study notes: automatic differentiation (part 1)

A few months ago, I came across the microgpt post by Andrej Karpathy, where he implements a GPT model from scratch, in “a single file of 200 lines of pure Python with no dependencies”. This is a super nice post, and something I was looking for - a bare-bones GPT model where nothing is hidden by libraries. My understanding of GPT models is hand-wavy, so I was excited to be able to step through everything, and understand the mechanics better.

Unsurprisingly, a good part of microgpt is dedicated to an implementation of autograd, that is, a version of automatic differentiation. The topic is not new to me (I have used the awesome DiffSharp before), but, as I looked through microgpt, I realized that, beyond the fact that it relies on the chain rule to propagate derivatives in composite functions, I was not entirely clear on how exactly that worked.

I also took a look at the nice series MicroGPT in F#, by Jonas Lara. That series starts by mimicking the original Python closely, which from a certain angle is great, and helped me some. However, I realized I was more interested in understanding automatic differentiation than in how it is implemented in microgpt, so I figured it was time for another of my side-quests: taking a stab at implementing autodiff from scratch, in F#, as a learning exercise.

This is, of course, rather pointless. DiffSharp already exists, and it is both very impressive and very complete. My goal here is not to replace it. I want to attempt to write a library from zero, try out ideas, see what works and what doesn’t, and learn from the mistakes as I go, hopefully gaining a better understanding of automatic differentiation in the process.

With that preamble out of the way, let’s get this side quest started!

More...

Correcting an additive correction model

As part of a project I am working on, I ran into a small issue that, while not of consequence, bothered me. Being prone to obsessing over details, I ended up digging into it to figure out if I could address the issue. In this post, I will go over the solution I ended up with.

In a nutshell, here is the problem. Suppose you are working on an application where you need information such as “how long does it take to go from point A to point B”. You have an external service available, which you can call to get estimates. So far, so good.

Now suppose that you look at the data, and realize that the time it takes to go from A to B depends on when the trip takes place. This is not entirely unexpected. Roads are typically busier at certain times of the day, or certain days of the week.

To avoid getting bogged down in unimportant details, let’s simplify a bit, and ignore the time of the day part. You look at the data, and notice that trips taking place on different days of the week behave differently.

As an example, you might observe something like “on Mondays, trips take 1 extra minute on average than what the service predicts; on Sundays, trips take 2 minutes less on average.”

An easy way to model this would be to layer a simple correction on top of the estimate you obtain from the service, adding an error correction value (a bias term), based on the day of the week, along these lines:

let driveTime (origin, destination, dateTime) =
    // use the external service to get a baseline
    let estimate = averageDriveTime(origin, destination)
    // get a correction for the day of the week
    let correction = dayCorrection dateTime
    // apply the correction
    estimate + correction

So for instance, a trip predicted to last 7 minutes taking place on a Sunday would end up being corrected to 7 minutes - 2 minutes = 5 minutes. Converserly, the same trip happening on a Monday would be predicted to take 7 minutes + 1 minute = 8 minutes. Simple enough.

More...