문제
Backpropagation multiplies gradients by the derivative of each activation function. Implement activation_derivative(name, values) and return the derivative at every value:
| name | derivative |
|---|---|
"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
예시
입력:
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.
입력:
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].
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.