समस्या
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:
| name | formula |
|---|---|
"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
उदाहरण
इनपुट:
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.
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।