Lesson 3 of 4 Bias and Class Imbalance

Measuring Bias Across Numeric, Text and Image Data

This lesson explains how to apply bias metrics across tabular, text and image data and adjust data distributions accordingly, since bias can hide in any modality a model uses.

Learning outcomes

After completing this lesson, you will be able to:

  • Measure bias in numeric and categorical data.
  • Derive attributes to measure bias in text data.
  • Measure and correct skew in image data.

Numeric and categorical data

For tabular data, SageMaker Clarify computes bias metrics directly on facet columns, such as class imbalance (CI), difference in proportions of labels (DPL), Kullback-Leibler (KL) divergence and Jensen-Shannon (JS) divergence. Continuous facets, such as age, are divided into groups with a threshold, for example under 40 and 40 or over.

The aim is not identical numbers in every group, which may be impossible or even wrong when groups genuinely differ. The aim is awareness of large gaps and a deliberate, documented decision on whether each gap is justified or must be corrected.

True or false

Facets in numeric data

Exam skills 1.3.4

Check how Clarify treats numeric and categorical facets.

Log in to try this activity Interactive activities are free with an account.

Text data

Text datasets can be biased in language, dialect, topic, sentiment and the demographic groups mentioned. To measure this, derive attributes for each document, for example language with Amazon Comprehend's language detection, sentiment and entities with Comprehend and demographic mentions with keyword lists or classifiers, then compute group sizes and outcome rates per attribute as facets. For generative models, paired prompts that differ only in a demographic term reveal stereotyping in outputs, which Bedrock evaluations and Clarify FM evaluations support.

The following diagram shows a bias finding against Spanish-language insurance claims and the fix for each kind of data: numeric fields, claim notes and photos.

Where the bias against Spanish-language claims came from. Clarify shows a DPL of 0.18 on the numeric data; Comprehend language detection shows Spanish notes are only 4% of the text, read by an English-trained encoder; and photos for Spanish claims come mostly from older, low-resolution phones. Each modality needs its own fix.
The scenario is illustrative.
Scenario questions

Bias beyond tables

Exam skills 1.3.4

Choose how to measure bias in text and image datasets.

Log in to try this activity Interactive activities are free with an account.

Image data

Image datasets can be skewed in lighting, background, camera, viewpoint, and subject attributes such as age or skin tone. Collect attributes from capture metadata, human annotation or Amazon Rekognition labels, then compare group sizes and model accuracy per group. Rebalance by collecting more images of under-represented conditions, augmenting with realistic transformations and weighting the loss.

The following table summarises how to measure and fix distribution bias in each modality.

ModalityDerive facets fromRebalance with
Numeric / categoricalExisting columns, bucketed thresholdsResampling, reweighting, more data
TextComprehend language, sentiment, entities; keyword classifiersTargeted collection, paraphrase, back-translation
ImagesCapture metadata, Rekognition labels, annotationCollection, augmentation, loss weighting
Worked example

A multimodal claims model

An insurer's claims model uses claim amount (numeric), the adjuster's notes (text) and damage photos (images). Approvals are lower for claims written in Spanish.

  • Numeric: Clarify shows DPL of 0.18 between English and Spanish claims for similar amounts.
  • Text: Comprehend language detection shows Spanish notes are 4% of training data; the text encoder was trained mostly on English.
  • Images: photos for Spanish claims come mostly from older phone models with low resolution.

Fixes include a multilingual text encoder, more Spanish training examples (back-translation from English examples where appropriate), image augmentation simulating low-resolution cameras and re-measuring DPL after retraining. Looking only at the numeric columns would have missed two of the three causes.

Check yourself

What must be done before bias can be measured on text or images?

Show answer

Derive facet attributes, such as language or the people shown, from the content.

Exam Tips

Remember for the exam

  • Derive facet attributes for text and images before measuring bias.
  • Comprehend language detection and sentiment help profile text datasets.
  • Rekognition labels and metadata help profile image datasets.
  • Paired prompts reveal stereotyping in generative models.
  • Re-measure bias metrics after rebalancing.