Appendix G: Answers to Selected Exercises

This appendix provides brief answers and solution sketches for the odd-numbered conceptual exercises (Part A) from each chapter. These are intended to help readers verify their understanding, not to replace the full problem-solving process. We encourage readers to attempt each exercise thoroughly before consulting these answers.


Part I: Foundations

Chapter 1: What Is AI Engineering?

1.1 AI engineering differs from traditional software engineering in that its core behavior is defined by data and learned parameters rather than explicit rules. While traditional software follows deterministic logic written by developers, AI systems learn statistical patterns from data, making their behavior probabilistic and sometimes unpredictable. This distinction affects every phase of the development lifecycle: requirements must account for statistical performance metrics, testing requires evaluation on representative data distributions, and deployment must include monitoring for data drift and model degradation.

1.3 The three pillars of AI engineering are: (a) data engineering, which concerns the collection, cleaning, and management of training data; (b) model development, which covers architecture selection, training, and evaluation; and (c) ML operations (MLOps), which addresses deployment, monitoring, and maintenance of models in production. All three are necessary for a successful AI system because a model is only as good as its data, and even an excellent model provides no value if it cannot be reliably deployed and maintained.

1.5 A machine learning engineer focuses primarily on model training and experimentation, whereas an AI engineer takes a broader view that includes system architecture, production infrastructure, and end-to-end delivery. The AI engineer must understand how models integrate with applications, how to manage serving infrastructure, how to build feedback loops, and how to handle the operational challenges of running AI in production.

Chapter 2: The AI Engineering Landscape

2.1 The foundation model paradigm shifted AI development from training task-specific models from scratch to adapting large pre-trained models for downstream tasks. This reduces the barrier to entry because practitioners can leverage powerful models without the compute resources needed for pre-training, but it also introduces new challenges around prompt engineering, fine-tuning efficiency, and dependency on model providers.

2.3 Open-source models provide transparency (auditable weights and code), customizability (fine-tuning to specific domains), data privacy (on-premises deployment), and freedom from vendor lock-in. Closed-source API-based models offer higher performance on some benchmarks, managed infrastructure, regular updates, and lower upfront engineering effort. The choice depends on requirements for data privacy, customization, cost structure, and the importance of understanding model internals.

Chapter 3: Python and Tools for AI Engineering

3.1 Virtual environments isolate project dependencies, preventing conflicts between packages required by different projects. Without them, upgrading a library for one project might break another. For AI projects, this is especially important because frameworks like PyTorch and TensorFlow have strict version compatibility requirements with CUDA drivers, and different projects may require different versions.

3.3 Git is essential for AI projects for the same reasons as traditional software (version control, collaboration, code review), but AI projects have additional needs: tracking experiment configurations, managing notebook versions, and coordinating between model code and training scripts. DVC (Data Version Control) extends Git to handle large data files and model artifacts that should not be stored directly in Git.

Chapter 4: Supervised Learning Fundamentals

4.1 In classification, the model predicts a discrete label from a finite set of categories (e.g., spam/not-spam, sentiment class). In regression, the model predicts a continuous numerical value (e.g., house price, temperature). The distinction affects the choice of loss function (cross-entropy vs. MSE), the output layer (softmax vs. linear), and the evaluation metrics (accuracy/F1 vs. MAE/RMSE).

4.3 Precision measures the fraction of positive predictions that are actually positive (TP / (TP + FP)), while recall measures the fraction of actual positives that are correctly identified (TP / (TP + FN)). In a medical diagnosis setting, high recall is critical because missing a disease (false negative) could be life-threatening, even if it means some healthy patients are flagged for further testing (lower precision). In a spam filter, high precision may be preferred because falsely blocking a legitimate email (false positive) is more costly than letting an occasional spam through.

4.5 The softmax function converts a vector of raw scores (logits) into a probability distribution by exponentiating each element and normalizing by the sum. It ensures all outputs are positive and sum to 1. The cross-entropy loss then measures the negative log-probability assigned to the true class. Together, maximizing the probability of the correct class (minimizing cross-entropy) is equivalent to maximum likelihood estimation.

Chapter 5: Probability and Statistics for AI

5.1 The likelihood function L(theta | data) gives the probability of the observed data as a function of the model parameters. Maximum likelihood estimation finds the parameter values that maximize this function. In practice, we maximize the log-likelihood (equivalently, minimize the negative log-likelihood) because logarithms convert products into sums, which is both numerically stable and mathematically convenient.

5.3 A Type I error (false positive) occurs when we reject a true null hypothesis, while a Type II error (false negative) occurs when we fail to reject a false null hypothesis. In A/B testing for a deployed model, a Type I error means concluding a new model is better when it is not (wasting resources on a needless change), while a Type II error means failing to detect a genuine improvement (missing an opportunity). The significance level alpha controls the Type I error rate.

Chapter 6: Feature Engineering and Classical ML

6.1 The bias-variance tradeoff describes the tension between a model's capacity to fit training data (low bias) and its stability across different training samples (low variance). A model that is too simple (e.g., linear regression for a highly non-linear relationship) has high bias and underfits. A model that is too complex (e.g., a deep tree on a small dataset) has high variance and overfits. The optimal model complexity balances both sources of error to minimize total generalization error.

6.3 Feature scaling (e.g., standardization or min-max normalization) is important for algorithms that are sensitive to the magnitude of features, such as gradient descent-based methods, SVMs, KNN, and PCA. Without scaling, features with larger numerical ranges dominate the learning process. Decision trees and random forests are largely insensitive to feature scaling because they make splits based on feature ordering rather than magnitude.

6.5 K-fold cross-validation partitions the data into K equal folds. The model is trained K times, each time using K-1 folds for training and the remaining fold for validation. The K validation scores are averaged to estimate generalization performance. This is superior to a single train/test split because it uses all data for both training and validation, provides an estimate of performance variance, and is more robust to the particular split chosen. The standard choice K=5 or K=10 provides a good balance between bias and computational cost.


Part II: Deep Learning Foundations

Chapter 7: Neural Networks from Scratch

7.1 Backpropagation computes the gradient of the loss with respect to each parameter by applying the chain rule layer by layer, from the output back to the input. For a network with layers l = 1, ..., L, the gradient with respect to weights in layer l depends on the local gradient at layer l and the upstream gradient propagated from layer l+1. Without backpropagation, computing gradients would require O(N) forward passes per parameter (using finite differences), making training infeasible for networks with millions of parameters.

7.3 The vanishing gradient problem occurs when gradients become exponentially small as they propagate through many layers during backpropagation. With sigmoid or tanh activations, whose derivatives are bounded well below 1, repeated multiplication causes gradients to shrink toward zero, making early layers learn extremely slowly. ReLU mitigates this because its derivative is exactly 1 for positive inputs (and 0 for negative), preventing gradient shrinkage in the active region. However, ReLU introduces the "dying neuron" problem where neurons with permanently negative inputs have zero gradient.

7.5 Weight initialization is critical because it determines the starting point for optimization and the initial distribution of activations and gradients. If weights are too large, activations and gradients explode; if too small, they vanish. Xavier/Glorot initialization sets weights from a distribution with variance 2/(n_in + n_out), maintaining activation variance across layers for linear activations. Kaiming/He initialization uses variance 2/n_in, which is appropriate for ReLU activations.

Chapter 8: Convolutional Neural Networks

8.1 A convolution operation slides a small learnable filter (kernel) across the input, computing the dot product between the filter and each local patch. This provides two key properties: (a) parameter sharing, where the same filter is applied at every spatial position, dramatically reducing the number of parameters compared to a fully connected layer; and (b) translation equivariance, meaning a shifted input produces a correspondingly shifted output, which is desirable for image processing.

8.3 Pooling layers (max pooling, average pooling) reduce the spatial dimensions of feature maps, providing a form of translation invariance (small shifts in input do not change the pooled output) and reducing computational cost in subsequent layers. Modern architectures sometimes replace pooling with strided convolutions, which learn how to downsample rather than using a fixed rule.

Chapter 9: Recurrent Neural Networks and Sequence Models

9.1 An LSTM cell contains three gates: the forget gate (decides what information to discard from the cell state), the input gate (decides what new information to store), and the output gate (decides what to output based on the cell state). The cell state provides a direct path for gradient flow across time steps, allowing the LSTM to learn long-range dependencies that vanilla RNNs cannot capture due to vanishing gradients.

9.3 Teacher forcing is a training strategy where the model receives the ground truth output from the previous time step as input, rather than its own prediction. This stabilizes training and speeds convergence but creates a discrepancy between training (where the model sees perfect inputs) and inference (where it must use its own outputs). This is called exposure bias. Techniques like scheduled sampling gradually transition from teacher forcing to using the model's own predictions during training.


Part III: Transformers and Language Models

Chapter 10: Embeddings and Tokenization

10.1 Subword tokenization (BPE, WordPiece) provides a middle ground between character-level and word-level tokenization. It handles rare and out-of-vocabulary words by breaking them into known subword units, maintains a manageable vocabulary size (typically 32K-64K tokens), and can represent any text. Pure word-level tokenization cannot handle unseen words, while character-level tokenization produces very long sequences that are computationally expensive to process.

10.3 The embedding matrix maps discrete token IDs to dense continuous vectors. In a model with vocabulary size V and embedding dimension d, the embedding layer is a V x d matrix where each row is the learned representation of one token. This is equivalent to multiplying a one-hot vector by the weight matrix, but implemented efficiently as an index lookup.

Chapter 11: The Transformer Architecture

11.1 Scaled dot-product attention computes: Attn(Q, K, V) = softmax(QK^T / sqrt(d_k)) V. The scaling factor 1/sqrt(d_k) is necessary because when d_k is large, the dot products QK^T grow in magnitude, pushing the softmax function into regions where it has extremely small gradients (the saturated regime). Dividing by sqrt(d_k) keeps the variance of the dot products at approximately 1, ensuring the softmax operates in a well-behaved range.

11.3 Multi-head attention runs h parallel attention operations, each with its own learned projections of dimension d_k = d_model / h. This allows the model to simultaneously attend to information from different representation subspaces at different positions. For example, one head might capture syntactic relationships while another captures semantic similarity. The outputs are concatenated and projected through a final linear layer.

11.5 Positional encodings are necessary because self-attention is permutation-equivariant: without position information, the model cannot distinguish between different orderings of the same tokens. Sinusoidal encodings use fixed sine and cosine functions of different frequencies, providing a deterministic pattern that generalizes to unseen sequence lengths. Learned positional encodings are optimized during training but are limited to positions seen during training. RoPE (Rotary Position Embedding) encodes relative positions by rotating query and key vectors, naturally supporting relative position awareness and length extrapolation.

Chapter 12: Pre-Training and Transfer Learning

12.1 BERT uses a masked language modeling (MLM) objective where 15% of input tokens are masked and the model must predict them from bidirectional context. GPT uses a causal language modeling (CLM) objective where the model predicts each token from only the preceding tokens (left-to-right). MLM produces better representations for understanding tasks because it uses bidirectional context, while CLM is natural for generation tasks. The choice of pre-training objective fundamentally determines what downstream tasks the model excels at.

12.3 Fine-tuning a pre-trained model typically outperforms training from scratch on a downstream task because the pre-trained model has already learned general linguistic features (syntax, semantics, world knowledge) from large-scale data. Fine-tuning only needs to adapt these features to the specific task, requiring far less task-specific data and compute. This is especially valuable when labeled data is scarce.

Chapter 13: Self-Supervised and Contrastive Learning

13.1 Contrastive learning trains representations by constructing positive pairs (similar examples that should have close representations) and negative pairs (dissimilar examples that should have distant representations). The loss function (e.g., InfoNCE) pushes positive pairs together and negative pairs apart in embedding space. The quality of representations depends heavily on the augmentation strategy used to create positive pairs and the number and diversity of negative examples.

13.3 In SimCLR, two augmented views of the same image form a positive pair, and all other images in the batch serve as negatives. The contrastive loss for a positive pair (i, j) in a batch of 2N augmented views is: L = -log(exp(sim(z_i, z_j)/tau) / sum_{k != i} exp(sim(z_i, z_k)/tau)). Large batch sizes are critical because they provide more negative examples, improving the quality of the learned representations.


Part IV: Large Language Models

Chapter 14: Language Model Pre-Training

14.1 Scaling laws describe the empirical relationships between model size (parameters N), dataset size (tokens D), compute budget (FLOPs C), and test loss L. Kaplan et al. found power-law relationships: L proportional to N^{-alpha} for model size and D^{-beta} for dataset size. Chinchilla scaling (Hoffmann et al.) showed that for compute-optimal training, the model size and dataset size should be scaled roughly equally, suggesting many early LLMs were over-parameterized relative to their training data.

14.3 Perplexity is defined as 2^{H(p, q)} where H(p, q) is the cross-entropy of the model's predictions against the true distribution. Equivalently, it is exp(average negative log-likelihood per token). A perplexity of K means the model is, on average, as uncertain as if it were choosing uniformly among K tokens at each step. Lower perplexity indicates better language modeling. It is the standard intrinsic evaluation metric for language models.

Chapter 15: Text Generation and Decoding

15.1 Greedy decoding selects the highest-probability token at each step. It is fast but often produces repetitive and suboptimal text because local choices may not lead to globally optimal sequences. Beam search maintains the top-B candidates at each step, exploring more of the search space. Nucleus (top-p) sampling draws from the smallest set of tokens whose cumulative probability exceeds p, providing diversity while excluding the long tail of unlikely tokens. Temperature controls the sharpness of the distribution: T < 1 makes it peakier (more deterministic), T > 1 makes it flatter (more random).

15.3 Repetition in generated text occurs because language models can enter self-reinforcing loops where generating a phrase increases the probability of generating it again. Mitigation strategies include: (a) repetition penalty, which reduces the logits of previously generated tokens; (b) frequency penalty, which penalizes tokens proportionally to how often they have appeared; (c) presence penalty, which applies a fixed penalty to any token that has appeared at all; and (d) n-gram blocking, which prevents any n-gram from being repeated.

Chapter 16: Parameter-Efficient Fine-Tuning

16.1 LoRA (Low-Rank Adaptation) inserts trainable low-rank decomposition matrices A (d x r) and B (r x d) alongside the frozen weight matrix W, so the effective weight becomes W + BA. The rank r is much smaller than d, so the number of trainable parameters is 2 * d * r instead of d * d. This is based on the hypothesis that weight changes during fine-tuning have low intrinsic dimensionality. Typical rank values (r = 8 to 64) achieve performance comparable to full fine-tuning while training less than 1% of the parameters.

16.3 QLoRA combines 4-bit NormalFloat quantization of the base model with LoRA adapters. It introduces three key innovations: (a) 4-bit NormalFloat (NF4) quantization, which is information-theoretically optimal for normally distributed weights; (b) double quantization, which quantizes the quantization constants themselves; and (c) paged optimizers, which use CPU memory to handle GPU memory spikes. This enables fine-tuning a 65-billion parameter model on a single 48GB GPU.

Chapter 17: Alignment and RLHF

17.1 RLHF involves three stages: (a) supervised fine-tuning (SFT) of a pre-trained model on high-quality demonstrations; (b) training a reward model on human preference comparisons between model outputs; and (c) optimizing the SFT model using reinforcement learning (typically PPO) with the reward model as the reward signal, while penalizing deviation from the SFT model (KL penalty). The reward model converts subjective human preferences into a scalar signal that RL can optimize.

17.3 DPO simplifies RLHF by eliminating the separate reward model and RL training phase. It directly optimizes the policy using the observation that the optimal policy under the RLHF objective has a closed-form relationship to the reward function. The DPO loss is a simple binary cross-entropy objective applied to preference pairs: it increases the log-probability of preferred responses relative to dispreferred responses, with a KL-divergence constraint built into the loss. DPO is simpler to implement, more stable to train, and requires fewer hyperparameters than PPO-based RLHF.


Part V: Applied AI Systems

Chapter 18: Uncertainty and Bayesian Methods

18.1 Epistemic uncertainty arises from limited knowledge (insufficient data or model capacity) and can be reduced with more data. Aleatonic uncertainty arises from inherent randomness in the data-generating process and cannot be reduced. Distinguishing between them matters in deployment: high epistemic uncertainty should trigger requests for human review or more data collection, while high aleatoric uncertainty indicates a fundamentally noisy prediction where even the best model would be uncertain.

Chapter 19: Prompt Engineering

19.1 A well-structured prompt for a complex task should include: (a) a system prompt defining the role and constraints; (b) clear task instructions with specific output format requirements; (c) few-shot examples demonstrating the expected input-output mapping; and (d) the actual input. Chain-of-thought prompting additionally instructs the model to reason step-by-step before giving the final answer, which substantially improves performance on tasks requiring multi-step reasoning.

Chapter 20: Information Retrieval for AI

20.1 BM25 is a sparse retrieval method based on term frequency and inverse document frequency, using exact keyword matching. Dense retrieval encodes queries and documents into dense vectors using learned embeddings and retrieves based on vector similarity. BM25 excels at exact match and requires no training data but misses semantic similarity. Dense retrieval captures semantic meaning but requires training data and can fail on out-of-distribution queries. Hybrid approaches combine both, typically using reciprocal rank fusion, to get the benefits of each.


Part VI: Production ML Systems

Chapter 26: RAG Systems

26.1 A basic RAG pipeline consists of: (a) document ingestion (parsing, chunking, cleaning); (b) embedding (converting chunks to dense vectors); (c) indexing (storing vectors in a vector database); (d) retrieval (finding the most relevant chunks for a query); and (e) generation (providing retrieved chunks as context to an LLM to generate the answer). The key design decisions are chunk size, overlap, embedding model selection, number of retrieved chunks (top-k), and the prompt template for the generation step.

Chapter 28: Data Engineering for AI

28.1 Data quality issues in AI projects include: missing values, label noise (incorrect annotations), class imbalance, data leakage (test data information bleeding into training), distribution shift (training data not representing deployment conditions), and contamination (test set examples appearing in training data). Each requires different mitigation strategies, and ignoring data quality will undermine even the best model architecture.


Part VII: Deployment and Operations

Chapter 33: Model Compression and Optimization

33.1 Quantization reduces the numerical precision of model weights (e.g., from 32-bit float to 8-bit or 4-bit integer). Post-training quantization (PTQ) applies quantization after training with no or minimal calibration, while quantization-aware training (QAT) simulates quantization during training so the model learns to be robust to reduced precision. PTQ is simpler but can degrade performance, especially at very low bit widths. QAT preserves more accuracy but requires a training phase.

Chapter 34: Model Serving and Inference

34.1 Key concerns for LLM serving include: (a) latency (time to first token and inter-token latency); (b) throughput (requests per second); (c) memory management (KV cache grows linearly with sequence length and batch size); (d) batching strategy (continuous batching vs. static batching); and (e) cost efficiency (GPU utilization). Systems like vLLM address these through PagedAttention (efficient KV cache memory management), continuous batching, and tensor parallelism.


Part VIII: Ethics, Safety, and the Future

Chapter 37: Responsible AI

37.1 A model card should include: model name and version, intended use cases and out-of-scope uses, training data description, evaluation results across demographic groups, known limitations and failure modes, ethical considerations, and environmental impact (compute used). Model cards promote transparency and help downstream users make informed decisions about whether a model is appropriate for their use case.

Chapter 39: AI Regulation and Policy

39.1 The EU AI Act classifies AI systems into risk tiers: unacceptable risk (banned, e.g., social scoring), high risk (subject to strict requirements, e.g., hiring systems, medical devices), limited risk (transparency obligations, e.g., chatbots must disclose they are AI), and minimal risk (no specific requirements). For AI engineers, the practical implications depend on the use case: high-risk systems require conformity assessments, data governance, human oversight, and detailed technical documentation.

Chapter 40: The Future of AI Engineering

40.1 Key trends shaping the future of AI engineering include: (a) continued scaling of foundation models with emergent capabilities; (b) multimodal models that unify text, image, audio, and video understanding; (c) agentic AI systems that can plan and execute multi-step tasks; (d) increased focus on efficiency through better architectures, training methods, and inference optimization; and (e) growing emphasis on safety, alignment, and responsible deployment. AI engineers must continuously adapt to these trends while maintaining focus on building reliable, useful, and safe systems.