Skip to content
DeepForgeby svx
All problems
HardOptimizationproj-015

Linear Training Loop

Problem

Train a single-layer linear model with full-batch gradient descent.

x is a list of input vectors and y is a list of target vectors, with W of shape out x in and b of length out. Each epoch computes the mean squared error over all outputs and samples, records it, then updates W and b with gradient descent using learning rate lr. Return [final_W, final_b, losses] where losses has one entry per epoch and is empty when epochs is 0.

Hint

Record the loss before the update so losses[0] reflects the initial parameters.

Test cases

case 1
in: [[[1],[2]],[[3],[5]],[[0]],[0],0.1,2]
expected: [[[1.71]],[1.05],[17,1.685]]
case 2
in: [[[0],[1]],[[1],[1]],[[0]],[0],1,1]
expected: [[[1]],[2],[1]]
case 3
in: [[[1],[2],[3]],[[3],[5],[7]],[[0]],[0],0.05,3]
expected: [[[1.8621481481481483]],[0.8234444444444443],[27.666666666666668,5.488518518518517,1.089693415637859]]
case 4
in: [[[1]],[[2]],[[1]],[0.5],0.1,0]
expected: [[[1]],[0.5],[]]
Starter codePython
def train_linear(x, y, W, b, lr, epochs):
    # Return [final_W, final_b, losses]
    # Your code here
    pass

4 test cases · run your code against them in the editor