Hands-on lab · 1 step
Gradient of the mean-squared error
Implement the vectorised gradient of the mean-squared-error loss of a linear model, namely and with residuals . The function is graded on one visible worked example and five hidden random problems with up to samples and features, to absolute tolerance .
0/1 step passedBest score 0/100
Step 1: Implement mseGradient
Complete the TypeScript function
mseGradient(w: number[], b: number, X: number[][], y: number[]): { gw: number[]; gb: number }
for the linear model and the loss
Shapes. w has entries; X has rows (X[i] is the feature vector ); y has targets. Return gw with entries, gw[k] , and the number gb . Do not modify the inputs.
What to compute. With residuals ,
Tests (all compared with absolute tolerance ):
- worked-example (visible): , , , . The predictions are , the residuals , hence
gw=(-5.1,-7.4) . - random-1 to random-5 (hidden): random problems with samples and features, entries in . Only their arguments are sent to your browser; the expected gradients stay on the server.