Implement the Swish activation function. Swish is a smooth, learnable activation function that often improves performance over ReLU due to smoother gradient flow.
Swish Formula:
Swish(x)=x⋅σ(x),where σ(x)=1+e−x1x - Input (scalar, list, or NumPy array)Input: [0, 1, -1, 3]
Output: [0.0, 0.731, -0.269, 2.857]
Smooth activation with both positive and negative outputs
Input: 0.0
Output: [0.0]
Scalar input returns 1D array with shape (1)
Input: [[1, -1], [2, -2]]
Output: [[0.731, -0.269], [1.762, -0.238]]
Works element-wise on multi-dimensional arrays
First implement sigmoid: 1 / (1 + np.exp(-x)), then multiply by x.
For numerical stability, clip extreme values before computing exponential to prevent overflow.
Use np.asarray() to handle different input types consistently.
np.ndarray of floatsscipy.special.expit)Sign in to take notes on this problem
Accepts: array
Implement the Swish activation function. Swish is a smooth, learnable activation function that often improves performance over ReLU due to smoother gradient flow.
Swish Formula:
Swish(x)=x⋅σ(x),where σ(x)=1+e−x1x - Input (scalar, list, or NumPy array)Input: [0, 1, -1, 3]
Output: [0.0, 0.731, -0.269, 2.857]
Smooth activation with both positive and negative outputs
Input: 0.0
Output: [0.0]
Scalar input returns 1D array with shape (1)
Input: [[1, -1], [2, -2]]
Output: [[0.731, -0.269], [1.762, -0.238]]
Works element-wise on multi-dimensional arrays
First implement sigmoid: 1 / (1 + np.exp(-x)), then multiply by x.
For numerical stability, clip extreme values before computing exponential to prevent overflow.
Use np.asarray() to handle different input types consistently.
np.ndarray of floatsscipy.special.expit)Sign in to take notes on this problem
Accepts: array