모든 챌린지
쉬움 0명 해결 정답률 0%

Activation Functions

Apply sigmoid, ReLU, Leaky ReLU or tanh to every value in a list.

배우게 될 내용

  • Implement common activation functions
  • Understand why networks need non-linearity
  • Avoid overflow in the sigmoid

문제

번역본 보기

After a neuron adds up its weighted inputs, an activation function decides its output. Without one, a neural network could only learn straight lines.

Implement activate(name, values) and apply the named function to every value:

nameformula
"sigmoid"1 / (1 + e^(-x))
"relu"max(0, x)
"leaky_relu"x if x > 0, otherwise 0.01 × x
"tanh"(e^x - e^(-x)) / (e^x + e^(-x))

Input

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

values: A list of numbers.

Output

Return a list with the activation applied to each value, in the same order.

제약 조건

1 ≤ length of values ≤ 1,000

-50 ≤ each value ≤ 50

예시

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

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

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

예상 출력:
double[] expected = new double[] {0.5, 0.880797, 0.119203};

설명

sigmoid(0) = 1 / (1 + 1) = 0.5, sigmoid(2) ≈ 0.880797 and sigmoid(-2) ≈ 0.119203. Sigmoid squeezes any number into the range 0 to 1.

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

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

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

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

설명

ReLU keeps positive values and turns negative values into 0: [0, 0, 3].

접근 방법

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

힌트 1

Each activation is applied independently to every number, so focus on one value at a time.

힌트 2

Use the formula matching the given name and build a new list from the results.

힌트 3

Be careful with negative inputs, especially for ReLU and the leaky slope on the nonpositive side.

예상 복잡도

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

참고 자료

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

다른 사람의 풀이 보기

커뮤니티 솔루션

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