模型与 Agent 编程
- 原文作者
- Matthew Lamm 其中第 42 题来自 Matthew Lamm 编写的 CS224N Word Window Classification Notebook 练习。
Numerically stable softmax and cross-entropy
原题Linear regression with gradient descent
原题Logistic regression with gradient descent
原题k-nearest neighbors
原题k-means clustering
原题Decision-tree split
原题Principal component analysis
原题2D convolution
原题Binary metrics and ROC-AUC
原题Reservoir sampling
原题Linear SVM and hinge loss
原题Perceptron learning rule
原题Feedforward neural network and backpropagation
原题Multiclass metrics and macro averaging
原题Multinomial Naive Bayes for text
原题Matrix factorization for recommendations
原题Gradient-boosting residual step
原题Train/validation/test split without leakage
原题Feature standardization with training statistics
原题Missing values and unseen categories
原题Uniform, weighted, and stratified sampling
原题Sample-weighted losses and streaming metrics
原题TF-IDF
原题Scaled dot-product attention
原题Learn and apply byte-pair encoding
原题Build a causal attention mask
原题Sinusoidal positional encoding
原题Temperature, top-k, and top-p sampling
原题Append-only KV cache
原题Pad variable-length token batches
原题LoRA update for a linear layer
原题Symmetric contrastive loss
原题Exact cosine top-k retrieval for RAG
原题Direct Preference Optimization loss
原题Classifier-free guidance combination
原题Validate JSON tool arguments
原题Permission-aware tool registry
原题Planner/executor boundary
原题Retry with idempotency
原题Sliding-window conversation memory
原题Bounded agent tool-use loop
原题The task at hand is to assign a label of 1 to words in a sentence that correspond with a LOCATION, and a label of 0 to everything else. In this simplified example, we only ever see spans of length 1.
原题来源定位:markdown cell 3。此条为 CS224N 课程背景的单词跨度 LOCATION 二分类练习。
Implement LoRA on a Linear Layer
原题完整原题
# Implement LoRA on a Linear Layer **Difficulty**: 🟡 Medium --- ### Problem Statement Low-Rank Adaptation (LoRA) is one of the most important parameter-efficient fine-tuning techniques in modern deep learning. Instead of fine-tuning all parameters of a pretrained model, LoRA freezes the original weights and injects small, trainable low-rank matrices into each layer. This dramatically reduces the number of trainable parameters while maintaining model quality. The key idea: instead of learning a full weight update ΔW (d_in × d_out), LoRA decomposes it into two low-rank matrices: ΔW = B × A, where A is (d_in, r) and B is (r, d_out), with rank r << min(d_in, d_out). The forward pass becomes: output = Wx + (α/r) × x @ A @ B Your task is to implement LoRA from scratch, apply it to an existing model, and merge the adapted weights back. --- ### Requirements 1. **LoRALinear** — A module that wraps an existing `nn.Linear`, adds low-rank matrices A and B, and freezes the original weights. 2. **apply_lora** — A function that injects LoRA into specified layers of an existing model. 3. **merge_lora** — A function that merges LoRA weights back into the original linear layer (W_new = W + (α/r) × BᵀAᵀ transposed appropriately). 4. **Parameter Efficiency** — Show that trainable parameters are much fewer than total parameters. 5. **Training** — Train a simple model with LoRA on synthetic data. --- ### Constraints - A should be initialized with Kaiming uniform; B should be initialized to zeros (so ΔW starts at zero). - Original weights must be frozen (requires_grad=False). - Merged model must produce identical outputs to the LoRA model. - Do **not** use any existing LoRA libraries (peft, loralib, etc.). ---
来源定位:markdown cell 2。该 Notebook 标注来自 TorchLeet 的派生练习。
暂无题目