챌린지 목록으로 돌아가기
쉬움 0명 해결 정답률 0%

Derivatives of Activation Functions

Compute the derivative of sigmoid, ReLU, Leaky ReLU or tanh at each value, the slopes backpropagation needs.

배우게 될 내용

  • Compute slopes of common activations
  • Reuse the sigmoid output for its derivative
  • Prepare the pieces backpropagation needs

문제

번역본 보기

Backpropagation multiplies gradients by the derivative of each activation function. Implement activation_derivative(name, values) and return the derivative at every value:

namederivative
"sigmoid"s × (1 - s), where s = 1 / (1 + e^(-x))
"relu"1 if x > 0, otherwise 0
"leaky_relu"1 if x > 0, otherwise 0.01
"tanh"1 - tanh(x)²

Input

name: One of "sigmoid", "relu", "leaky_relu" or "tanh".

values: The inputs x of the activation.

Output

Return the derivative at each value, in the same order.

제약 조건

1 ≤ length ≤ 1,000

-50 ≤ each value ≤ 50

예시

예시 1
입력:
name = "sigmoid"
values = [0.0, 2.0]

예상 출력:
expected = [0.25, 0.104994]
입력:
std::string name = "sigmoid";
std::vector<double> values = {0.0, 2.0};

예상 출력:
std::vector<double> expected = {0.25, 0.104994};
입력:
string name = "sigmoid";
double[] values = new double[] {0.0, 2.0};

예상 출력:
double[] expected = new double[] {0.25, 0.104994};

설명

At 0, s = 0.5 and the slope is 0.5 × 0.5 = 0.25, the steepest point of the sigmoid. At 2, s ≈ 0.880797 and the slope is about 0.104994.

예시 2
입력:
name = "relu"
values = [-3.0, 0.0, 4.5]

예상 출력:
expected = [0.0, 0.0, 1.0]
입력:
std::string name = "relu";
std::vector<double> values = {-3.0, 0.0, 4.5};

예상 출력:
std::vector<double> expected = {0.0, 0.0, 1.0};
입력:
string name = "relu";
double[] values = new double[] {-3.0, 0.0, 4.5};

예상 출력:
double[] expected = new double[] {0.0, 0.0, 1.0};

설명

ReLU is flat for x ≤ 0 (slope 0) and has slope 1 for x > 0, so the result is [0, 0, 1].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

The derivative depends on the activation type, so handle each function separately for every input value.

힌트 2

For sigmoid and tanh, reuse the activation output or its standard function value instead of recomputing from scratch in a complicated way.

힌트 3

Watch the sign thresholds: ReLU uses a strict positive test, while leaky ReLU keeps a small slope for nonpositive values.

예상 복잡도

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

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.