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

Perceptron Learning Algorithm

Train the original perceptron, which updates its weights only when it makes a mistake.

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

  • Update weights only on mistakes
  • Train over several epochs
  • Learn how the first neural model worked

โจทย์

ดูฉบับแปล

The perceptron (1958) is the first learning algorithm for an artificial neuron. It predicts with a step function and learns from its mistakes.

Start with all weights and the bias at 0. For each epoch, go through the samples in order, and for each sample x with label y (0 or 1):

  1. prediction = 1 if x · weights + bias > 0, otherwise 0
  2. error = y - prediction (so -1, 0 or 1)
  3. If error ≠ 0: weights[i] ← weights[i] + learning_rate × error × x[i] for every i, and bias ← bias + learning_rate × error

Implement train_perceptron(samples, labels, learning_rate, epochs).

Input

samples: m samples with n features each.

labels: The m labels, each 0 or 1.

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

Output

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

เงื่อนไข

1 ≤ m ≤ 100, 1 ≤ n ≤ 10, 1 ≤ epochs ≤ 100

0 < learning_rate ≤ 1

-10 ≤ each feature ≤ 10

ตัวอย่าง

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

ผลลัพธ์ที่คาดหวัง:
expected = [1.0, 1.0, 1.0]
อินพุต:
long long epochs = 1;
std::vector<long long> labels = {0, 0, 0, 1};
std::vector<std::vector<double>> samples = {
    {0.0, 0.0},
    {0.0, 1.0},
    {1.0, 0.0},
    {1.0, 1.0}
};
double learning_rate = 1.0;

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {1.0, 1.0, 1.0};
อินพุต:
long epochs = 1;
long[] labels = new long[] {0, 0, 0, 1};
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 learning_rate = 1.0;

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

คำอธิบาย

[0, 0]: z = 0, predicts 0, correct. [0, 1] and [1, 0]: also predict 0, correct. [1, 1]: predicts 0 but the label is 1, so error = 1: weights become [1, 1] and the bias becomes 1. Result: [1, 1, 1].

ตัวอย่างที่ 2
อินพุต:
epochs = 10
labels = [0, 0, 0, 1]
samples = [
    [0.0, 0.0],
    [0.0, 1.0],
    [1.0, 0.0],
    [1.0, 1.0]
]
learning_rate = 1.0

ผลลัพธ์ที่คาดหวัง:
expected = [2.0, 1.0, -2.0]
อินพุต:
long long epochs = 10;
std::vector<long long> labels = {0, 0, 0, 1};
std::vector<std::vector<double>> samples = {
    {0.0, 0.0},
    {0.0, 1.0},
    {1.0, 0.0},
    {1.0, 1.0}
};
double learning_rate = 1.0;

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {2.0, 1.0, -2.0};
อินพุต:
long epochs = 10;
long[] labels = new long[] {0, 0, 0, 1};
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 learning_rate = 1.0;

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

คำอธิบาย

After 10 epochs the perceptron has learned the AND gate: with weights [2, 1] and bias -2, only [1, 1] gives z > 0.

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

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

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

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

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