PIXELBANKv9.1.0
Menu

Implement LoRA (Low-Rank Adaptation) weight decomposition.

LoRA decomposes a weight update ΔW into two low-rank matrices: ΔW=A×B\Delta W = A \times B

where A has shape (d, r) and B has shape (r, d), with r << d being the rank.

Given d (dimension) and r (rank), initialize A from a normal distribution (seed 42) and B as zeros. Compute the effective weight update ΔW = A × B.

Input: d r (dimension, rank) Output: The ΔW matrix (d × d), rounded to 4 decimal places.

Since B is initialized to zeros, ΔW should be all zeros initially. Then set B = np.random.randn(r, d) * 0.01 (using the same seed state after A) and recompute.

Example:

Input:
3 2
Output:
[[-0.0014  0.0006 -0.0112]
 [-0.0090  0.0082  0.0035]
 [ 0.0063 -0.0048 -0.0035]]
Reasoning:
  • Initialize AA with shape (d,r)=(3,2)(d, r) = (3, 2) from a normal distribution with seed 42, and BB with shape (r,d)=(2,3)(r, d) = (2, 3) as zeros.
  • Compute the initial ΔW=A×B\Delta W = A \times B, which results in a (3,3)(3, 3) matrix of all zeros, since BB is all zeros.
  • Update BB with B=np.random.randn(r,d)⋅0.01B = \text{np.random.randn}(r, d) \cdot 0.01, using the same seed state after AA, to get a new (2,3)(2, 3) matrix.
  • Recompute ΔW=A×B\Delta W = A \times B using the updated BB to get the final (3,3)(3, 3) matrix, which is then rounded to 4 decimal places to produce the output.

Constraints:

  • np.random.seed(42), A = np.random.randn(d, r), B = np.random.randn(r, d) * 0.01
  • Output the ΔW = A @ B matrix
  • Round to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
LoRA Weight Decomposition - Easy | PixelBank