ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก