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

Gradients of a Single Neuron

Use the chain rule to find how the loss changes with each weight and the bias of a sigmoid neuron.

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

  • Apply the chain rule to a neuron
  • Get gradients for each weight and the bias
  • Understand how learning signals flow

โจทย์

ดูฉบับแปล

To train a neuron we need to know how each weight affects the error. For one training example:

  • z = inputs · weights + bias
  • y = sigmoid(z) = 1 / (1 + e^(-z))
  • loss L = (y - target)²

By the chain rule, with delta = dL/dz = 2 × (y - target) × y × (1 - y):

  • dL/dw_i = delta × x_i
  • dL/dbias = delta

Implement neuron_gradients(inputs, weights, bias, target).

Input

inputs, weights: Lists of the same length n.

bias: The neuron's bias.

target: The correct output for this example (between 0 and 1).

Output

Return a list of n + 1 numbers: the gradient for each weight in order, then the gradient for the bias.

เงื่อนไข

1 ≤ n ≤ 100

-10 ≤ each input, weight and bias ≤ 10

0 ≤ target ≤ 1

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
inputs = [1.0, 2.0]
weights = [0.5, -0.25]
bias = 0.0
target = 1.0

ผลลัพธ์ที่คาดหวัง:
expected = [-0.25, -0.5, -0.25]
อินพุต:
std::vector<double> inputs = {1.0, 2.0};
std::vector<double> weights = {0.5, -0.25};
double bias = 0.0;
double target = 1.0;

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {-0.25, -0.5, -0.25};
อินพุต:
double[] inputs = new double[] {1.0, 2.0};
double[] weights = new double[] {0.5, -0.25};
double bias = 0.0;
double target = 1.0;

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

คำอธิบาย

z = 1×0.5 + 2×(-0.25) = 0, so y = 0.5. delta = 2 × (0.5 - 1) × 0.5 × 0.5 = -0.25. Gradients: [-0.25×1, -0.25×2, -0.25] = [-0.25, -0.5, -0.25].

ตัวอย่างที่ 2
อินพุต:
inputs = [3.0]
weights = [0.0]
bias = 0.0
target = 0.0

ผลลัพธ์ที่คาดหวัง:
expected = [0.75, 0.25]
อินพุต:
std::vector<double> inputs = {3.0};
std::vector<double> weights = {0.0};
double bias = 0.0;
double target = 0.0;

ผลลัพธ์ที่คาดหวัง:
std::vector<double> expected = {0.75, 0.25};
อินพุต:
double[] inputs = new double[] {3.0};
double[] weights = new double[] {0.0};
double bias = 0.0;
double target = 0.0;

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

คำอธิบาย

z = 0 and y = 0.5. delta = 2 × (0.5 - 0) × 0.5 × 0.5 = 0.25, so the gradients are [0.25×3, 0.25] = [0.75, 0.25]. Positive gradients mean the weight should go down.

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

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

คำใบ้ 1

Start by computing the neuron's sigmoid output and the loss derivative with respect to its pre-activation value.

คำใบ้ 2

The gradient for each weight is the same delta scaled by that input, and the bias uses the delta alone.

คำใบ้ 3

Keep the weight gradients in input order, then append the bias gradient at the end.

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

O(n) time, O(n) extra space

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

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

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

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

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

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