Alek's Thoughts

deep learning class -- some things I've learned

A lot of people have asked me lately "what cool stuff have you been learning in your deep learning class"? I haven't had a very good answer. In this post I plan to discuss some stuff that we've talked about and highlight some things that I have found cool. Will serve the triple purpose of (1) helping me solidify understanding of various deep learning things, (2) sate (or maybe stoke!) the curiosity of friends interested in what I'm learning, (3) serve as conversation fodder.

1 Diffusion#

This is a fake shoe! Pasted image 20241108232950.png

2 VAEs#

We have some complicated data distribution $D:\mathbb{R}^{d}\to [0,1]$. Or rather, $D$ exists in the world and we have some samples from $D$. We'd like to be able to generate new synthetic samples from this distribution. Sampling a Gaussian point in $\mathbb{R}^{d}$ doesn't work.

But we're going to model our data as being a simple function applied to some latent variables, where the latent variables are actually Gaussian.

Pasted image 20241108193852.png

This will give us a generative model. Specifically, we can sample $z$ in latent space (because latent space is simple), and then apply our "decoder" to the latent representation to get our actual representation.

VAEs are going to be similar to AE's but an AE is more like this: Pasted image 20241108193939.png

Ok, so how are we going to learn the encoder and decoder?

Let's call the encoder $f_\phi$ and the decoder $g_\phi$. Our model is

The things that we want are:

In other words, if we encode to a latent rep of a point, then when we decode back that latent rep we should get about the same thing that we started with, and also we want the latent reps to live in a nice unit bubble around the origin.

You can write down some loss function that drives us to achieve both of these things, although these things are in tension so we're not going to be perfect -- reps that are more squished are harder to decode.

You might say "why do we even need an encoder?" The reason is that we are going to do importance sampling to estimate an integral.

On our homework we had some data that looked vaguely like this (actually this is fake data generated by my VAE, which you can probably tell bc some of the items look kind of sus. But it mostly looks believable). Pasted image 20241109124237.png

It learns "interpretable" directions in the latent space!

I had quite a bit of trouble training this. My intuition is that lots of optimization problems where you have two fighting objectives (e.g., VAEs, GANs) suffer from a similar sort of instability. For me, I finally got it to work by setting the loss to be 100*reconstruction_loss + KL-divergence. I'm pretty proud of the 100 -- I was debugging and realized that the KL divergence, despite being really small, was somehow still taking over the gradients, and the reconstruction loss just wasn't improving. Maybe I just needed more epochs but idk. Anyways, I turned up the heat by which I mean I multiplied the reconstruction gradients by 100 and then my little VAE obligingly learned some reasonable latent representation of FashionMNIST.

3 Why does deep learning find general circuits?#

Supervised learning initially may seem kind of magical -- sure neural networks are universal function approximators, so of course we can get them to fit some training data, but why do they generalize to unseen data?!!

One thing that some people say is:

Neural networks are super huge, so when you randomly initialize them, they'll have all the simple circuits. When you train a neural network, you're pruning away the garbage and revealing the simple underlying circuit.

To be a bit more precise, it's more like this:

Neel Nanda has some really cool work on Grokking with Modular addition that gives some good evidence for this hypothesis.

I think that's a pretty neat idea.

A phenomenon related to this is "grokking" -- when a model suddenly, after having loss plateued for quite a while, starts doing much better.

s-curve 1.png [Adam's excellent mathematical explanation of why this happens](https://www.alignmentforum.org/posts/RKDQCB6smLWgs2Mhr/multi-component-learning-and-s-curves)

4 Unsupervised / semi-supervised stuff#

Unsupervised learning and semi-supervised learning is pretty cool.

Before this class I guess I thought that unsupervised learning just meant "do kmeans clustering".

But this doesn't really make sense -- usually the challenging part would be to learn semantically interesting features! clustering once you have such features is the easy part.

But there's some pretty nice techniques for doing this.

Basically contrastive learning seems like the main really good approach.

6 Transformers#

Transformers are pretty interesting. I've given really garbled explanations of what they are doing a couple of times, and so here is an attempt at a better explanation.

Disclaimer -- I'm going to describe a conceptually clean way of thinking about what a transformer does. At some level a transformer is just a giant stack of matrices and doesn't have to act like I'm about to say it acts. However, (1) I think that this is a really helpful way of thinking about what a transformer does, and (2) as discussed above, NNs often do actually learn simple circuits. One thing that can complicate this is superposition. (3) I'm not aware of any experiments that support the following way of thinking about transformers. But I believe that at least some parts are true and could be experimentally verified, and probably have been. ok, that's a lot of disclaimers. Anyways.

Let me describe BERT "Bidirectional Encoder Representations from Transformers". Specifically I'll discuss BERT-base (not BERT-large). Pasted image 20241108161841.png

A helpful graphic.

First, what does BERT even do?

BERT is trained to do "Masked Language Modelling". Which means, you have a sentence, mask some tokens, and then ask it to predict those. The reason you'd want to do this task is actually just because it forces the model to learn really rich representations of sentences that are useful for downstream tasks.

What's the architecture of BERT?

For reference GPT4 probably has about 500B parameters, and the human brain has about 100T axons. So BERT is tiny. Figures! It's already about 6 years old!

Anyways, what happens inside an attention block?

So, basically you have a sequence of token embedings, which you can think of as "deep representations" of the input tokens. A multiheaded attention unit is composed of multiple heads. You can think of these heads as all serving different purposes.

For instance, ChatGPT recomended I think about it like this:

What does a single head do?

Each token embedding $x_i$ computes a KEY $Kx_i$ and a QUERY $Qx_i$. They then go ask everyone else "here's my question, are you the answer?" By which I mean they compute $\langle Kx_i, Qx_j \rangle$. These are called scores, and the dude softmaxes these to decide how much to attend to each of the vectors. Then it takes a weighted combinations of some VALUE vectors $Vx_i$, where the weights are the softmaxed attention scores.

One other thing to note is that residual connections are pretty important, and that thinking of attention heads as writing to a residual stream seems reasonable.

Also positional embeddings are pretty important.

7 misc#

Here's a creepy toaster drawn by ChatGPT: Pasted image 20241109130749.png