簡単 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。