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

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

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

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