• Decision-Focused Learning


    Every day we make decisions based on uncertain information. Living in the UK, a classic example of this is deciding whether to take an umbrella with you. Weather forecasts are notoriously uncertain. They can predict a clear blue sky and it ends up bucketing it down. That’s why most of the time I have an umbrella in my bag, just in case. It’s a regret-minimising decision: the cost of bringing my umbrella with me and not needing it is much lower than needing it and not having it. Here is a simple diagram outlining the possible states:

    A graph of the decision regret

    If we were to naively automate this system using a typical predict-then-optimise approach there would be two steps:

    1. Create a rain prediction model that outputs a yes or no for whether it will rain.
    2. Create an optimisation process (in this case just a check to see if rain is predicted) to decide whether I need an umbrella.

    I’m sure you can see a number of issues and some obvious ways to improve this approach. We could do more than just predict a binary variable, \(p \in \{0, 1\}\), for whether it will rain; we could predict a probability or how heavy the rain will be. Doing so would complicate our optimisation step, perhaps now using a threshold value. However, this doesn’t address the underlying issue: what if the prediction is wrong? The optimisation process has no knowledge that it’s using predictions, and more importantly, the model has learned to minimise its prediction error (\(L2\)) with no regard for the optimisation. Unlike the predict-then-optimise approach, Decision-Focused Learning is the process of teaching machine learning models to generate predictions that, when used to make a decision, minimise regret.

    read more...

  • GPU Accelerated Genetic Algorithms


    I have started using Genetic Algorithms (GA) in my PhD research. They are a very effective and simple tool used to solve optimisation problems. Using a “natural selection”-like process to explore the solution space, they can quickly arrive at a good candidate solution. The nice thing about GAs is that they are very easy to implement. However, a basic implementation can be a bit slow.

    In deep learning (DL), to speed up the computation we make heavy use of GPUs (and in some cases TPUs). A lot of my work has been doing DL with JAX. If you don’t know, JAX is an autograd and XLA library for Python. It allows you to write Python code with the NumPy API that can be compiled, vectorised, and run on GPUs. Inspired by a blog post from Will Whitney outlining a neat way to use jax.vmap to train multiple models at once, I asked myself: could I use the same technique to speed up my GA and run it on the GPU? It turns out, with a few minor caveats, you can. Check out my code on GitHub.

    read more...

  • Weather Forecasting


    If the tag-line of this post doesn’t make sense then you might not be up to speed on the transformer. I have been aware of the transformer architecture for a while now. I spend time at work exploring LLMs like BART and T5, and like everyone else have seen GPT-3 demos from OpenAI. So, over Christmas I decided it was time to dig into the detail and properly get up to speed.

    The original use case for transformers was NLP; in the original paper it was used for sequence-to-sequence machine translation. However, I’m not an NLP expert. I also know transformers are data-hungry and I have access to a lot of satellite imagery (around 300 GB). Fundamentally, attention operates over sequences, and there has been substantial work applying transformers to images, such as ViT. So why not test them on short-term satellite cloud forecasting?

    read more...

  • Serialise Objects in Python


    I often have data stored in Python objects that aren’t DataFrames, such as config objects. Saving and loading these objects to and from disk is frequently useful. The simplest way to save an arbitrary Python object is pickle, however in most cases you should avoid it. It is a security hazard and in the worst case can lead to arbitrary code execution when loading untrusted data. A safer alternative is to serialise structured data explicitly:

    import json
    s = json.dumps(dict_object)
    dict_obj = json.loads(s)
    

    There are also dump and load functions that operate directly on file handles rather than strings.

    For typed dataclasses, pyserde is also a clean library for serialising and deserialising dataclass objects.