AI Hints
Home›NLP Tutorial›Practical Modern NLP
NLP · Lesson 72 of 106

Bias in Language Models

What the data taught them.

⏱ 2 min read▶ Video at 2:05:46🎓 Free
Watch this lesson in the NLP courseWatch on YouTube ↗Full NLP course

Embeddings learn from billions of sentences written by people. So they learn what people wrote, including our stereotypes. In twenty sixteen, researchers showed that a famous model completed man is to programmer as woman is to homemaker. Nobody put that in. It was simply in the data.

Bias in Language Models
From the video: Bias in Language Models
  • They learn from what people wrote
  • Including our stereotypes
  • It was in the data

Measuring a bias

We make a direction in meaning, by subtracting a sentence about a man from a sentence about a woman. Then we project some job titles onto that direction. Plus means closer to the woman sentence, and minus means closer to the man sentence. Run it. Nurse and receptionist lean one way. Mechanic, engineer, and carpenter lean the other. This is a modern model, in twenty twenty six. If you ranked job applicants with these embeddings, that lean could quietly affect the results.

bias.pypython
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
jobs = ["nurse", "engineer", "receptionist", "carpenter", "librarian", "mechanic"]
words = model.encode(["she is a woman", "he is a man"] + [f"a {j}" for j in jobs], normalize_embeddings=True)
gap = words[0] - words[1]

for job, v in sorted(zip(jobs, words[2:]), key=lambda p: -float(p[1] @ gap)):
    print(f"{job:<14} {float(v @ gap):+.3f}")
Output
nurse          +0.233
receptionist   +0.165
librarian      +0.040
mechanic       -0.053
engineer       -0.057
carpenter      -0.117

What to do about it

You can't fully remove this, but you can manage it. Test your system across names, genders, and dialects. Swap words in test sentences, and check that outputs don't change unfairly. Keep people involved in decisions that affect someone's life. Be especially careful with hiring, lending, and medical uses. And check your training data, as much of today's text is written by a small part of the world.

Test with swapped namesdoes the output change?
Human decisionshiring, lending, health
Dialectsfair to all speakers
Check the datawho wrote it?

Summary

Models mirror their training data, so test them for fairness. Next, some practical tricks for when you need more data.

Key takeaways
  • Embeddings absorb stereotypes
  • Measure with swapped terms
  • Keep people involved
Next up: Text Augmentation — more data from what you have