Attention visualization techniques
Attention mechanisms are a key component of transformer-based LLMs (see Chapter 1). Visualizing attention patterns can provide insights into how the model processes and attends to different parts of the input.
Here’s an example of how to visualize attention in a transformer-based model:
import torch from transformers import BertTokenizer, BertModel import matplotlib.pyplot as plt import seaborn as sns def visualize_attention(model, tokenizer, text): inputs = tokenizer(text, return_tensors="pt") outputs = model(inputs, output_attentions=True) attention = outputs.attentions[-1].squeeze().detach().numpy() tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) plt.figure(figsize=(10, 8)) sns.heatmap(attention, xticklabels=tokens, &...