ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Train a Neuron with Gradient Descent

Repeat forward pass, gradients and weight updates to teach a sigmoid neuron from examples.

สิ่งที่คุณจะได้เรียนรู้

  • Repeat forward pass, gradients and updates
  • Tune a learning rate
  • Watch a neuron learn from examples

โจทย์

ดูฉบับแปล

Gradient descent improves the weights a little at a time by moving against the gradient:

w ← w - learning_rate × dL/dw

Train a sigmoid neuron with stochastic gradient descent. For each epoch, go through the samples in the given order, and for each sample:

  1. z = sample · weights + bias, y = 1 / (1 + e^(-z))
  2. delta = 2 × (y - target) × y × (1 - y)
  3. weights[i] ← weights[i] - learning_rate × delta × sample[i] (for every i), then bias ← bias - learning_rate × delta

Implement train_neuron(samples, targets, weights, bias, learning_rate, epochs).

Input

samples: m training examples, each with n values.

targets: The m correct outputs (between 0 and 1).

weights, bias: The starting weights (n values) and bias.

learning_rate, epochs: The step size and how many passes over the data.

Output

Return the trained weights followed by the trained bias (n + 1 numbers).

เงื่อนไข

1 ≤ m ≤ 50, 1 ≤ n ≤ 10, 1 ≤ epochs ≤ 1,000

0 < learning_rate ≤ 5

-5 ≤ each value ≤ 5

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
bias = 0.0
epochs = 1
samples = [
    [1.0]
]
targets = [1.0]
weights = [0.0]
learning_rate = 1.0

ผลลัพธ์ที่คาดหวัง:
expected = [0.25, 0.25]
อินพุต:
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;

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {0.25, 0.25};
อินพุต:
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;

ผลลัพธ์ที่คาดหวัง:
double[] expected = new double[] {0.25, 0.25};

คำอธิบาย

y = σ(0) = 0.5, delta = 2 × (0.5 - 1) × 0.25 = -0.25. Weight: 0 - 1 × (-0.25) × 1 = 0.25. Bias: 0 - 1 × (-0.25) = 0.25. Result: [0.25, 0.25].

ตัวอย่างที่ 2
อินพุต:
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

ผลลัพธ์ที่คาดหวัง:
expected = [6.346558, 6.339863, -9.592092]
อินพุต:
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;

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {6.346558, 6.339863, -9.592092};
อินพุต:
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;

ผลลัพธ์ที่คาดหวัง:
double[] expected = new double[] {6.346558, 6.339863, -9.592092};

คำอธิบาย

This is the logical AND gate. After 500 epochs both weights are large and positive and the bias is strongly negative, so only the input [1, 1] gives an output above 0.5.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Process the training samples in order for each epoch, updating weights immediately after each example.

คำใบ้ 2

For each sample, compute the sigmoid output, then use its gradient to move weights and bias downhill.

คำใบ้ 3

Because updates are online, later samples in the same epoch see earlier updates.

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก