-
Book Overview & Buying
-
Table Of Contents
Python Data Analysis - Fourth Edition
By :
In this chapter, we worked through practical Python workflows to understand and apply Large Language Models and Generative AI. We began by implementing the core ideas behind transformers, including scaled dot-product attention and a simplified multi-head attention mechanism with NumPy. We also examined how different transformer architectures support modern LLM families and used the Hugging Face Transformers library to load a tokenizer and a causal language model for text generation.
We then focused on ways to improve and adapt LLM behavior in Python. Using a review dataset, we fine-tuned a pretrained DistilBERT model for sentiment classification with the Hugging Face Trainer API. We also practiced prompt engineering through zero-shot and few-shot prompting, reusable prompt templates, and structured outputs. To make adaptation more efficient, we introduced LoRA and PEFT, then built a lightweight fine-tuning workflow that trains only a small set of additional parameters...