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

Mini-Batch Gradient Descent

Train a linear model by updating the weights after every small batch of samples instead of the whole dataset.

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

  • Split data into batches
  • Update weights after each batch
  • Compare batch, mini-batch and stochastic training

Đề bài

Xem bản dịch

Mini-batch gradient descent is how almost every neural network is trained: split the data into small batches and update the weights after each one.

Train y = w × x + b on the mean squared error, starting from w = 0 and b = 0. In each epoch, split the samples in order into batches of batch_size (the last batch may be smaller). For each batch of size n:

  1. errorᵢ = w × xᵢ + b - yᵢ for each sample in the batch
  2. dw = (2 / n) × Σ errorᵢ × xᵢ and db = (2 / n) × Σ errorᵢ
  3. w ← w - learning_rate × dw and b ← b - learning_rate × db

Implement train_mini_batch(xs, ys, batch_size, learning_rate, epochs).

Input

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

batch_size: Samples per batch.

learning_rate, epochs: The step size and the number of passes over the data.

Output

Return [w, b] after training.

Ràng buộc

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

0 < learning_rate ≤ 1

-100 ≤ each value ≤ 100

Ví dụ

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

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

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

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

Giải thích

Batch 1 is x = [1, 2]: errors -2 and -4, dw = -10, db = -6, so w = 1 and b = 0.6. Batch 2 is x = [3] alone: error 1×3 + 0.6 - 6 = -2.4, dw = 2 × (-2.4) × 3 = -14.4 and db = -4.8, so w = 2.44 and b = 1.08. Result: [2.44, 1.08].

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

Kết quả mong đợi:
expected = [1.75, 0.6]
Đầu vào:
std::vector<double> xs = {1.0, 2.0, 3.0, 4.0};
std::vector<double> ys = {3.0, 5.0, 7.0, 9.0};
long long batch_size = 4;
double learning_rate = 0.05;
long long epochs = 1;

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

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

Giải thích

A batch as big as the dataset is ordinary batch gradient descent: one update per epoch. Result: [1.75, 0.6].

Hướng tiếp cận

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

Gợi ý 1

Process the samples in the given order, updating the parameters after every batch instead of after every single point.

Gợi ý 2

For each batch, compute prediction errors first, then use their averages with x values to get the two gradients.

Gợi ý 3

The last batch may be smaller than the others, so its gradient scaling must use that batch's size.

Độ 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.