liveBook · Manning

Bias

As explained in Transformers in Action

Bias is a systematic deviation or tendency in data, measurements, estimates, judgments, or outputs that skews them relative to a relevant target or impartial baseline.

In an LLM, bias is a systematic tendency in behavior that can reflect imbalances in its training data. Pretraining data strongly influences later outputs, so inspecting that data helps identify where cultural or demographic bias may originate. Bias matters when a model is used with people whose profiles are poorly represented in its training data. section 10.1 section 10.1.1

Table 10.1 Distribution of content by nationality, race and ethnicity, and religion
Nationality (14.83%)
Race and Ethnicity (19.51%)
Religion (7.93%)
Descriptor
% Doc
Descriptor
% Doc
Descriptor
% Doc
American
69.4%
European
20.7%
Christian
33.2%
Indian
16.5%
African
11.5%
Religious
28.8%
Chinese
16.3%
Asian
7.4%
Spiritual
20.6%
Korean
5.1%
Latin
6.2%
Catholic
15.4%
Mexican
4.9%
Indigenous
3.7%
Jewish
13.0%

Compare the listed demographic descriptors to see why training-data composition warrants scrutiny. The percentages for individual descriptors are frequencies within documents referencing that demographic category, not percentages of the full dataset. section 10.1.1

A practical bias check has several parts:

  • Inspect pretraining-data representation, including nationality, race and ethnicity, religion, and other demographics relevant to the intended users. section 10.1 section 10.1.1
  • Check outputs for toxic, rude, adversarial, or implicitly hateful content. A toxicity classifier can help screen responses, but its score addresses toxicity rather than measuring every form of bias. section 10.1.1
  • Consider methods such as reinforcement learning from human feedback (RLHF) or direct preference optimization (DPO) when adapting a chatbot for underrepresented profiles; continue evaluating its behavior afterward. section 10.1.1 section 10.5
Listing 10.1 Detecting hate speech with RoBERTA-Toxicity
model_path = "s-nlp/roberta_toxicity_classifier" #1
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path).eval()

def evaluate_text(text):
    input_ids = tokenizer(text, return_tensors="pt")["input_ids"] #2

    with torch.no_grad():  #3
     outputs = model(input_ids)

    probabilities = torch.softmax(outputs.logits, dim=1) #4

    score = 100 * float(probabilities[:, 1].detach().numpy()) #5

    return score

score = evaluate_text(response)
print(f"Hate speech probability: {score:.3g}%")  

The listing loads a RoBERTa-based toxicity classifier, tokenizes a response, runs inference, and converts its logits into a reported hate-speech probability. Use that output as a targeted content check, not as proof that the response or model is unbiased. section 10.1.1

Interpretability tools such as Captum can help investigate why particular outputs occur and reveal possible biases or errors. This behavioral meaning of bias is distinct from a model’s *bias parameters*: those are learned alongside weights through gradient descent. section 10.2 section 8.1.1

How Transformers in Action covers it

How deep it goes
Explains training-data origins and practical detection, but does not provide a general quantitative measure of bias.
Examples
Uses a Llama demographic-distribution table and a RoBERTa-based toxicity-classifier listing.
What it assumes
Assumes familiarity with LLM training and inference while explaining how pretraining data can shape later behavior.
What you can do after
Evaluate/understand it by inspecting training-data representation and screening outputs for toxic content.
How it connects
Connects bias to pretraining data, demographic representation, toxicity detection, model interpretability, RLHF, and DPO.

Also explained in

Compare how 28 books explain bias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.