모든 챌린지
보통 1명 해결 정답률 100.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에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 850 ms - 5/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 1개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.