AI Hints
Home›Deep Learning Tutorial›Beyond the Basics
Deep Learning · Lesson 49 of 92

Learning Without Labels

Self-supervised learning.

⏱ 2 min read▶ Video at 1:38:23🎓 Free
Watch this lesson in the Deep Learning courseWatch on YouTube ↗Full Deep Learning course

A child doesn't need a million labelled photos to learn what a dog is. They see the world, and they notice patterns, with almost no labels at all. Self supervised learning tries to do the same. It's how language models learn too. They create their own labels from the data.

Learning Without Labels
From the video: Learning Without Labels
  • Children need few labels
  • They notice patterns
  • Models can create their own labels

Making up your own task

The trick is to invent a task from the data itself. Hide the next word, and predict it. That's how G P T learns. Hide patches of an image, and rebuild them. That's a masked autoencoder. Or show two cropped views of one photo, and teach the network that they're the same, and that they differ from all other photos. That's contrastive learning.

Next wordhow GPT learns
Masked patchesrebuild what's hidden
Contrastivesame photo, two views
Resultgreat features, no labels

Contrastive learning

Here's the core idea in a few lines. We compare every item in one group with every item in the other. The right answer for item one is the matching item, and so on down the diagonal. So it's just cross entropy. We test it with two views of the same things, and then with unrelated things. Run it. Matching views give a low loss. Unrelated ones give a high loss. Training pulls matching views together, and pushes the others apart. No labels are required.

contrastive.pypython
import torch
from torch import nn

torch.manual_seed(0)

def contrastive(a, b, temperature=0.1):
    a = nn.functional.normalize(a, dim=1)
    b = nn.functional.normalize(b, dim=1)
    sim = a @ b.T / temperature
    labels = torch.arange(len(a))
    return nn.functional.cross_entropy(sim, labels)

base = torch.rand(8, 16)
view1 = base + 0.02 * torch.randn(8, 16)
view2 = base + 0.02 * torch.randn(8, 16)
stranger = torch.rand(8, 16)

print("Two views of the same things:", round(contrastive(view1, view2).item(), 3))
print("Unrelated things:           ", round(contrastive(view1, stranger).item(), 3))
Output
Two views of the same things: 0.571
Unrelated things:            2.232

Summary

By inventing tasks from the data, networks can learn rich features with no labels. And the features themselves turn out to be incredibly useful. Next, how embeddings power search, memory, and much more.

Key takeaways
  • Create labels from data
  • Contrastive pulls views together
  • Foundation of modern AI
Next up: Embeddings Everywhere — meaning as a number list