すべてのチャレンジ
中級 0件解決済み 正答率0%

Linear Regression with Gradient Descent

Learn the slope and intercept of a line by repeatedly stepping down the mean squared error.

このチャレンジで学べること

  • Minimize mean squared error step by step
  • Compute gradients for slope and intercept
  • See how the learning rate affects training

問題

翻訳を表示

Instead of a formula, train the line y = w × x + b with batch gradient descent on the mean squared error MSE = Σ(w × x + b - y)² / n.

Start with w = 0 and b = 0. In each epoch, using all points:

  1. errorᵢ = w × xᵢ + b - yᵢ
  2. dw = (2 / n) × Σ errorᵢ × xᵢ and db = (2 / n) × Σ errorᵢ
  3. w ← w - learning_rate × dw and b ← b - learning_rate × db (both computed from the same errors)

Implement train_linear_regression(xs, ys, learning_rate, epochs).

Input

xs, ys: The data points (same length).

learning_rate, epochs: The step size and the number of updates.

Output

Return [w, b] after training.

制約

1 ≤ number of points ≤ 1,000, 0 ≤ epochs ≤ 10,000

0 < learning_rate ≤ 1

-100 ≤ each value ≤ 100

例

例 1
入力:
xs = [1.0, 2.0]
ys = [2.0, 4.0]
learning_rate = 0.1
epochs = 1

期待される出力:
expected = [1.0, 0.6]
入力:
std::vector<double> xs = {1.0, 2.0};
std::vector<double> ys = {2.0, 4.0};
double learning_rate = 0.1;
long long epochs = 1;

期待される出力:
std::vector<double> expected = {1.0, 0.6};
入力:
double[] xs = new double[] {1.0, 2.0};
double[] ys = new double[] {2.0, 4.0};
double learning_rate = 0.1;
long epochs = 1;

期待される出力:
double[] expected = new double[] {1.0, 0.6};

解説

With w = b = 0 the errors are -2 and -4. dw = (2 / 2) × (-2×1 - 4×2) = -10 and db = (2 / 2) × (-2 - 4) = -6, so w = 0 - 0.1 × (-10) = 1 and b = 0 - 0.1 × (-6) = 0.6. Result: [1, 0.6].

例 2
入力:
xs = [1.0, 2.0, 3.0]
ys = [3.0, 5.0, 7.0]
learning_rate = 0.05
epochs = 2000

期待される出力:
expected = [2.0, 1.0]
入力:
std::vector<double> xs = {1.0, 2.0, 3.0};
std::vector<double> ys = {3.0, 5.0, 7.0};
double learning_rate = 0.05;
long long epochs = 2000;

期待される出力:
std::vector<double> expected = {2.0, 1.0};
入力:
double[] xs = new double[] {1.0, 2.0, 3.0};
double[] ys = new double[] {3.0, 5.0, 7.0};
double learning_rate = 0.05;
long epochs = 2000;

期待される出力:
double[] expected = new double[] {2.0, 1.0};

解説

The data lies on y = 2x + 1. After 2,000 epochs gradient descent has converged to w = 2 and b = 1, the same answer as the least squares formula.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Each epoch uses every point to measure how wrong the current line is.

ヒント 2

Accumulate the gradient from all samples before updating both parameters together.

ヒント 3

Be careful not to update one parameter early, because both updates must use the same epoch’s errors.

目安の計算量

O(epochs × n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。