Deep Learning · Module 11
Two bridges beyond the core: reuse and attention
This lesson gives concrete mental models for two common extensions. It does not train a production vision system or a language model. You already know enough to ask better questions about them: what representation is computed, which parameters are updated, how data is split and what is actually evaluated?
Transfer learning reuses a representation
Imagine a network separated into a feature extractor and a prediction head. The extractor maps an input to a feature vector; the head maps that vector to the target required by the current task. In transfer learning, some parameters were learned on a source task before adaptation to a target task. A useful source representation can reduce target-data requirements, but success depends on how relevant the source and target are. “Pretrained” does not mean universally suitable.
The simplest adaptation freezes the extractor and trains a new head. Setting the extractor parameters' requires_grad to false prevents their gradients being recorded for optimization. Constructing the optimizer from only trainable parameters makes the intended update scope explicit. That still leaves a separate question about module mode. An extractor containing dropout or batch normalization can behave differently in training mode even when its weights are frozen. For a fixed feature extractor, keep it in evaluation mode while training the head. Calling model.train() on the entire parent module can accidentally switch it back.
Fine-tuning instead allows some source parameters to change. This increases flexibility and can erase useful representations if adaptation is too aggressive. Learning rates, layer selection and checkpoint choice belong to validation decisions. The original source training data may overlap an evaluation dataset, so provenance matters as well as code.
The runnable examples/06_freeze_features.py uses a small, explicitly handcrafted extractor. It verifies which parameters change and demonstrates the mode boundary. It is a mechanics demonstration, not an empirical transfer-learning result, because those extractor weights were not learned on a source dataset. We deliberately make that distinction rather than download a large model and imply the difficult evaluation problem has disappeared.
Sequence data introduces order and variable length
A sequence is an ordered collection, such as readings over time or token IDs in a sentence. A typical hidden-state tensor is [batch, sequence_length, features]. Padding can make different lengths fit one rectangular batch, but padding positions should not become meaningful evidence. A mask tells an operation which positions to ignore. For prediction over time, the split must also respect the direction of information: a feature from tomorrow cannot help a model that must decide today.
Attention builds a weighted combination of value vectors. A query expresses what the current position is looking for; keys are compared with that query; the resulting scores become nonnegative weights summing to one; values are averaged using those weights. These names describe different roles, not separate databases or hidden intelligence.
Consider query [1,0], two keys [1,0] and [0,1], and values [2,0] and [0,4]. Dot products are 1 and 0. With key dimension 2, scaled scores are 1/sqrt(2) and 0, approximately 0.7071 and 0. Softmax exponentiates and normalizes them, producing weights approximately 0.6698 and 0.3302. The result is approximately [1.3395,1.3210]. It is a blend of the two values, with more weight on the key that matched the query better.
Read the diagram explanation
Query is one, zero; scaled scores are 0.7071 and 0; softmax weights are 0.6698 and 0.3302; value vectors two, zero and zero, four yield output 1.3395, 1.3210.
Run python examples/05_attention_arithmetic.py and compare the numbers. In a learned model, linear projections usually produce queries, keys and values from representations. In a causal next-token model, positions must not attend to future tokens; an attention mask removes that information path before normalization. An unmasked demonstration of two keys is not an implementation of a causal language model.
Know what is still missing
A transformer combines attention with additional operations, including position information, feed-forward transformations, residual connections and normalization. Multiple heads allow several learned comparison spaces. Training a useful language model also requires tokenization, a suitable objective, substantial data, compute, evaluation and safety work. Reading one weighted-average example does not establish transformer implementation competence, much less LLM mastery.
Attention weights can help inspect a computation, but they are not automatically a faithful explanation of a prediction or proof of causal importance. A model's behavior depends on all its transformations. Carry forward the same habits from this course: inspect shapes, track information availability and make claims only at the scale of the evidence.
Exercises
- 11A · Freeze is not eval. A frozen feature extractor contains dropout. Why can repeated outputs still differ? What two separate settings should you inspect?
- 11B · Equal attention. If both scores are zero and values remain
[2,0]and[0,4], compute weights and output. Explain why the output is not either original value. - 11C · Future leakage. A model predicting the next token allows the current position to attend to later ground-truth tokens during training. Explain why this changes the prediction problem and how a causal mask addresses the information path.
Exit check: state exactly what the frozen-extractor and attention scripts demonstrate, and one major capability neither establishes.
Read the separate answers and explanations
Reading progress
12 lessons remain open to guests. Marking a lesson read records reading only; it does not award assessment credit or a certificate.
Device reading marks require browser storage. Reading is always available.
Sign in or create an account to save separate account progress. Your current page is kept.
Your earlier place on this device suggests these lessons. No new lesson is marked read.