チャレンジ一覧に戻る
簡単 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 つずつ開いてみましょう。

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

他の人の解答から学ぶ

コミュニティの解答

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