Two Pulls on Every Weight

Training Deep NetworksHard+30 XP+30

Two Pulls on Every Weight

Regularization adds a charge for large weights to the loss. Gradient descent then feels two pulls on every weight: the data pulls it toward whatever fits the training set, and the penalty pulls it toward zero. This problem runs both pulls for several steps and tracks where the weights end up.

To make the data's pull concrete, each weight wiw_iwi​ has a target tit_iti​, the value the training data on its own would choose for it. The data loss is a bowl centred on the targets:

Ldata=12∑i(wi−ti)2L_{\text{data}} = \tfrac{1}{2} \sum_i (w_i - t_i)^2Ldata​=21​∑i​(wi​−ti​)2

The penalty is one of these, with strength λ\lambdaλ (strength):

penaltypenalty term
"l2"λ∑iwi2\lambda \sum_i w_i^{2}λ∑i​wi2​
"l1"λ∑i∣wi∣\lambda \sum_i \lvert w_i \rvertλ∑i​∣wi​∣

Each step updates every weight in two stages, both using learning rate η\etaη (lr):

  1. Data stage. An ordinary gradient-descent step on LdataL_{\text{data}}Ldata​ alone, starting from wiw_iwi​. Call the result uiu_iui​.
  2. Penalty stage. A gradient-descent step on the penalty term alone, starting from uiu_iui​. The penalty's slope is measured at uiu_iui​, not at the old wiw_iwi​. The result is the new wiw_iwi​.

The L1 penalty needs one extra rule. Its slope is +λ+\lambda+λ wherever the weight is positive and −λ-\lambda−λ wherever it is negative, so its stage moves uiu_iui​ a fixed distance toward zero. If that distance would reach or cross zero, the weight stops at exactly 000 instead of going past it. If uiu_iui​ is exactly 000, it stays 000 through the penalty stage.

Task: write penalised_descent(weights, targets, lr, strength, penalty, steps). Starting from weights, run steps full steps and return the final weights as a list of floats, rounded to 4 decimal places. weights and targets have the same length (at least 1), lr and strength are positive, penalty is "l1" or "l2", and steps is a whole number of at least 1.

Worked through, penalised_descent([1.0, 0.2], [0.8, 0.1], 0.5, 0.2, "l1", 2). The L1 stage moves each weight ηλ=0.1\eta\lambda = 0.1ηλ=0.1 toward zero:

stepweightdata stagepenalty stage
1w1w_1w1​1.0−0.5(1.0−0.8)=0.91.0 - 0.5(1.0 - 0.8) = 0.91.0−0.5(1.0−0.8)=0.90.9−0.1=0.80.9 - 0.1 = 0.80.9−0.1=0.8
1w2w_2w2​0.2−0.5(0.2−0.1)=0.150.2 - 0.5(0.2 - 0.1) = 0.150.2−0.5(0.2−0.1)=0.150.15−0.1=0.050.15 - 0.1 = 0.050.15−0.1=0.05
2w1w_1w1​0.8−0.5(0.8−0.8)=0.80.8 - 0.5(0.8 - 0.8) = 0.80.8−0.5(0.8−0.8)=0.80.8−0.1=0.70.8 - 0.1 = 0.70.8−0.1=0.7
2w2w_2w2​0.05−0.5(0.05−0.1)=0.0750.05 - 0.5(0.05 - 0.1) = 0.0750.05−0.5(0.05−0.1)=0.0750.075≤0.10.075 \le 0.10.075≤0.1, so it stops at 000

The answer is [0.7, 0.0].

Why stop at zero? Without that rule, the L1 stage subtracts the same fixed amount every step. A weight smaller than that amount jumps across zero, gets pushed back, and jumps again. It flickers around zero forever without ever being zero. The stop rule is what makes L1 switch weights off exactly, and exact zeros are the whole reason to choose L1 over L2.

Examples

weights = [2.0, -1.0]
targets = [1.5, -0.5]
lr = 0.5
strength = 0.1
penalty = "l2"
steps = 3
returns [1.2977, -0.4629]
weights = [0.3, 1.0]
targets = [0.05, 0.9]
lr = 0.5
strength = 0.1
penalty = "l1"
steps = 4
returns [0.0, 0.8125]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run