Tất cả thử thách
Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Linear Regression with Gradient Descent

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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

0 < learning_rate ≤ 1

-100 ≤ each value ≤ 100

Ví dụ

Ví dụ 1
Đầu vào:
xs = [1.0, 2.0]
ys = [2.0, 4.0]
learning_rate = 0.1
epochs = 1

Kết quả mong đợi:
expected = [1.0, 0.6]
Đầu vào:
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;

Kết quả mong đợi:
std::vector<double> expected = {1.0, 0.6};
Đầu vào:
double[] xs = new double[] {1.0, 2.0};
double[] ys = new double[] {2.0, 4.0};
double learning_rate = 0.1;
long epochs = 1;

Kết quả mong đợi:
double[] expected = new double[] {1.0, 0.6};

Giải thích

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].

Ví dụ 2
Đầu vào:
xs = [1.0, 2.0, 3.0]
ys = [3.0, 5.0, 7.0]
learning_rate = 0.05
epochs = 2000

Kết quả mong đợi:
expected = [2.0, 1.0]
Đầu vào:
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;

Kết quả mong đợi:
std::vector<double> expected = {2.0, 1.0};
Đầu vào:
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;

Kết quả mong đợi:
double[] expected = new double[] {2.0, 1.0};

Giải thích

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.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.