Đề bài
Hạ gradient cải thiện trọng số từng chút một bằng cách đi ngược hướng gradient:
w ← w - learning_rate × dL/dw
Hãy huấn luyện một nơ-ron sigmoid bằng hạ gradient ngẫu nhiên. Ở mỗi epoch, duyệt các mẫu theo đúng thứ tự đã cho, và với mỗi mẫu:
- z = sample · weights + bias, y = 1 / (1 + e^(-z))
- delta = 2 × (y - target) × y × (1 - y)
- weights[i] ← weights[i] - learning_rate × delta × sample[i] (với mọi i), sau đó bias ← bias - learning_rate × delta
Hãy cài đặt train_neuron(samples, targets, weights, bias, learning_rate, epochs).
Đầu vào
samples: m ví dụ huấn luyện, mỗi ví dụ có n giá trị.
targets: m đầu ra đúng (từ 0 đến 1).
weights, bias: Trọng số ban đầu (n giá trị) và bias.
learning_rate, epochs: Bước nhảy và số lượt duyệt qua dữ liệu.
Đầu ra
Trả về các trọng số sau khi huấn luyện, tiếp theo là bias sau khi huấn luyện (n + 1 số).
Ràng buộc
1 ≤ m ≤ 50, 1 ≤ n ≤ 10, 1 ≤ epochs ≤ 1.000
0 < learning_rate ≤ 5
-5 ≤ mỗi giá trị ≤ 5
Ví dụ
Đầu vào:
bias = 0.0
epochs = 1
samples = [
[1.0]
]
targets = [1.0]
weights = [0.0]
learning_rate = 1.0
Kết quả mong đợi:
expected = [0.25, 0.25]
Đầu vào:
double bias = 0.0;
long long epochs = 1;
std::vector<std::vector<double>> samples = {
{1.0}
};
std::vector<double> targets = {1.0};
std::vector<double> weights = {0.0};
double learning_rate = 1.0;
Kết quả mong đợi:
std::vector<double> expected = {0.25, 0.25};
Đầu vào:
double bias = 0.0;
long epochs = 1;
double[][] samples = new double[][] {
new double[] {1.0}
};
double[] targets = new double[] {1.0};
double[] weights = new double[] {0.0};
double learning_rate = 1.0;
Kết quả mong đợi:
double[] expected = new double[] {0.25, 0.25};
Giải thích
y = σ(0) = 0.5, delta = 2 × (0.5 - 1) × 0.25 = -0.25. Trọng số: 0 - 1 × (-0.25) × 1 = 0.25. Bias: 0 - 1 × (-0.25) = 0.25. Kết quả: [0.25, 0.25].
Đầu vào:
bias = 0.0
epochs = 500
samples = [
[0.0, 0.0],
[0.0, 1.0],
[1.0, 0.0],
[1.0, 1.0]
]
targets = [0.0, 0.0, 0.0, 1.0]
weights = [0.0, 0.0]
learning_rate = 2.0
Kết quả mong đợi:
expected = [6.346558, 6.339863, -9.592092]
Đầu vào:
double bias = 0.0;
long long epochs = 500;
std::vector<std::vector<double>> samples = {
{0.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{1.0, 1.0}
};
std::vector<double> targets = {0.0, 0.0, 0.0, 1.0};
std::vector<double> weights = {0.0, 0.0};
double learning_rate = 2.0;
Kết quả mong đợi:
std::vector<double> expected = {6.346558, 6.339863, -9.592092};
Đầu vào:
double bias = 0.0;
long epochs = 500;
double[][] samples = new double[][] {
new double[] {0.0, 0.0},
new double[] {0.0, 1.0},
new double[] {1.0, 0.0},
new double[] {1.0, 1.0}
};
double[] targets = new double[] {0.0, 0.0, 0.0, 1.0};
double[] weights = new double[] {0.0, 0.0};
double learning_rate = 2.0;
Kết quả mong đợi:
double[] expected = new double[] {6.346558, 6.339863, -9.592092};
Giải thích
Đây là cổng logic AND. Sau 500 epoch, cả hai trọng số đều lớn và dương, còn bias âm mạnh, nên chỉ đầu vào [1, 1] cho đầu ra lớn hơn 0.5.
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.