Topics / 🔬 LLM Engineering II / 🎛️ Level 81: LLM Engineering II: Fine-Tuning

Python exercise · about 5 minutes · free

LoRA: Training a Small Update Instead of the Whole Matrix

Freeze the huge weight matrix and learn two skinny ones whose product is the change. Millions of trainable numbers instead of billions.

Full fine-tuning updates every weight. LoRA (low-rank adaptation) freezes the original matrix W (shape d_out x d_in) and learns a low-rank update delta = (alpha / r) x B A, where A is r x d_in, B is d_out x r and the rank r is small (4 to 64). Trainable parameters drop from d_in x d_out to r x (d_in + d_out). B starts at zero, so training begins exactly at the base model. After training, delta can be merged into W for free inference.

Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.

Flow: frozen W (d_out x d_in) → A (r x d_in) and B (d_out x r) (trainable, tiny) → W + (alpha/r) B A (merge for inference)frozen Wd_out x d_inA (r x d_in) and B (d_out x r)trainable, tinyW + (alpha/r) B Amerge for inference
Your quest: Write lora_param_count(d_in, d_out, r), lora_ratio(d_in, d_out, r) (full parameters divided by LoRA parameters) and lora_delta(A, B, alpha, r) returning the matrix (alpha / r) * (B times A) as a list of rows.
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
HintMatrix product: <code>sum(B[i][k] * A[k][j] for k in range(r))</code>, then multiply by <code>alpha / r</code>.
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 620 lessons.

In this level you'll learn:
More in Level 81: LLM Engineering II: Fine-Tuning

Share on LinkedIn