模型与 Agent 编程

题集来源:Alireza Dirafzoon(alirezadir)收录项目:AIMLInterviews查看原题集

原文作者
Matthew Lamm 其中第 42 题来自 Matthew Lamm 编写的 CS224N Word Window Classification Notebook 练习。

题目目录

43 道原题
  1. Numerically stable softmax and cross-entropy

    原题
  2. Linear regression with gradient descent

    原题
  3. Logistic regression with gradient descent

    原题
  4. k-nearest neighbors

    原题
  5. k-means clustering

    原题
  6. Decision-tree split

    原题
  7. Principal component analysis

    原题
  8. 2D convolution

    原题
  9. Binary metrics and ROC-AUC

    原题
  10. Reservoir sampling

    原题
  11. Linear SVM and hinge loss

    原题
  12. Perceptron learning rule

    原题
  13. Feedforward neural network and backpropagation

    原题
  14. Multiclass metrics and macro averaging

    原题
  15. Multinomial Naive Bayes for text

    原题
  16. Matrix factorization for recommendations

    原题
  17. Gradient-boosting residual step

    原题
  18. Train/validation/test split without leakage

    原题
  19. Feature standardization with training statistics

    原题
  20. Missing values and unseen categories

    原题
  21. Uniform, weighted, and stratified sampling

    原题
  22. Sample-weighted losses and streaming metrics

    原题
  23. TF-IDF

    原题
  24. Scaled dot-product attention

    原题
  25. Learn and apply byte-pair encoding

    原题
  26. Build a causal attention mask

    原题
  27. Sinusoidal positional encoding

    原题
  28. Temperature, top-k, and top-p sampling

    原题
  29. Append-only KV cache

    原题
  30. Pad variable-length token batches

    原题
  31. LoRA update for a linear layer

    原题
  32. Symmetric contrastive loss

    原题
  33. Exact cosine top-k retrieval for RAG

    原题
  34. Direct Preference Optimization loss

    原题
  35. Classifier-free guidance combination

    原题
  36. Validate JSON tool arguments

    原题
  37. Permission-aware tool registry

    原题
  38. Planner/executor boundary

    原题
  39. Retry with idempotency

    原题
  40. Sliding-window conversation memory

    原题
  41. Bounded agent tool-use loop

    原题
  42. 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 二分类练习。

  43. 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 的派生练习。