Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Fitting a Line with Gradient Descent | Section
Python Math Module Essentials: Trigonometry, Logarithms, and Constants - 1769704232288
Osio 1. Luku 27
single

single

Challenge: Fitting a Line with Gradient Descent

Pyyhkäise näyttääksesi valikon

Tehtävä

Pyyhkäise aloittaaksesi koodauksen

A student wants to use gradient descent to fit a straight line to a dataset showing years of experience versus salary (in thousands). The goal is to find the best-fitting line by adjusting the slope (mm) and intercept (bb) using iterative updates.

You need to minimize the loss function:

1ni=1n(yi(mxi+b))2\frac{1}{n}\sum^n_{i=1}(y_i - (mx_i + b))^2

The gradient descent update rules are:

mmαJmbbαJbm \larr m - \alpha \frac{\partial J}{\partial m} \\[6 pt] b \larr b - \alpha \frac{\partial J}{\partial b}

Where:

  • α\alpha is the learning rate (step size);
  • Jm\frac{\raisebox{1pt}{$\partial J$}}{\raisebox{-1pt}{$\partial m$}} is the partial derivative of the loss function with respect to mm;
  • Jb\frac{\raisebox{1pt}{$\partial J$}}{\raisebox{-1pt}{$\partial b$}} is the partial derivative of the loss function with respect to bb.

Your task:

  1. Complete the Python code below to implement the gradient descent steps.
  2. Fill in missing expressions using basic Python operations.
  3. Track how m and b change as the algorithm runs.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 27
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt