PIXELBANKv9.1.0
Menu

Implement common activation functions and their derivatives.

Given a list of values and an activation name, compute both the activation output and its derivative:

  • ReLU: f(x)=max⁡(0,x)f(x) = \max(0, x), f′(x)=1f'(x) = 1 if x>0x > 0, else 00
  • Sigmoid: f(x)=11+e−xf(x) = \frac{1}{1 + e^{-x}}, f′(x)=f(x)(1−f(x))f'(x) = f(x)(1 - f(x))
  • Tanh: f(x)=tanh⁡(x)f(x) = \tanh(x), f′(x)=1−f(x)2f'(x) = 1 - f(x)^2

Return a tuple (outputs, derivatives), each rounded to 4 decimal places.

Example:

Input:
values = [-1, 0, 1]
activation = "relu"
Output:
([0, 0, 1], [0, 0, 1])
Reasoning:
  • The input values is [-1, 0, 1] and the chosen activation is "relu", which has the function f(x)=max⁡(0,x)f(x) = \max(0, x).
  • We apply the ReLU function to each value: f(−1)=max⁡(0,−1)=0f(-1) = \max(0, -1) = 0, f(0)=max⁡(0,0)=0f(0) = \max(0, 0) = 0, f(1)=max⁡(0,1)=1f(1) = \max(0, 1) = 1.
  • Next, we calculate the derivatives: f′(−1)=0f'(-1) = 0 since −1≤0-1 \leq 0, f′(0)=0f'(0) = 0 since 00 is not greater than 00, f′(1)=1f'(1) = 1 since 1>01 > 0.
  • The final output is a tuple of the activation outputs and their derivatives, both rounded to 4 decimal places: ([0, 0, 1], [0, 0, 1]).

Constraints:

  • values: list of floats
  • activation: "relu", "sigmoid", or "tanh"
  • Return tuple of two lists (outputs, derivatives)
  • Round to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Activation Functions - Easy | PixelBank