TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Sigmoid in NumPy

Activation Functions
Easy

Implement the sigmoid activation function:

σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+e−x1​

Return a float when x is a scalar. For a list or nested list, return a NumPy array of floats with the same shape as x.

Loading visualization...

Examples

Input: x = [0, 2, -2]

Output: [0.5, 0.88079708, 0.11920292]

Input: x = 0

Output: 0.5

Input: x = [[-1, 0], [1, 2]]

Output: [[0.26894142, 0.5], [0.73105858, 0.88079708]]

Hint 1

Convert the input with np.asarray(x, dtype=float) before applying elementwise operations.

Hint 2

Use np.exp on the negated array when computing the sigmoid expression.

Requirements

  • Input x may be a scalar, a Python list, or a nested Python list
  • Return a float when x is a scalar
  • Return a NumPy array of floats with the same shape when x is a list
  • Use a fully vectorized implementation without Python loops

Constraints

  • Vectorized implementation only
  • Time limit: 200 ms; Memory: 64 MB
  • Allowed library: NumPy only
Try Similar Problems
Relu ActivationSoftmax FunctionLog Loss Per SampleCross Entropy LossLogistic Regression Training

Sign in to take notes on this problem

Accepts: any

You must run your code first.
PrevNext

Implement Sigmoid in NumPy

Activation Functions
Easy

Implement the sigmoid activation function:

σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+e−x1​

Return a float when x is a scalar. For a list or nested list, return a NumPy array of floats with the same shape as x.

Loading visualization...

Examples

Input: x = [0, 2, -2]

Output: [0.5, 0.88079708, 0.11920292]

Input: x = 0

Output: 0.5

Input: x = [[-1, 0], [1, 2]]

Output: [[0.26894142, 0.5], [0.73105858, 0.88079708]]

Hint 1

Convert the input with np.asarray(x, dtype=float) before applying elementwise operations.

Hint 2

Use np.exp on the negated array when computing the sigmoid expression.

Requirements

  • Input x may be a scalar, a Python list, or a nested Python list
  • Return a float when x is a scalar
  • Return a NumPy array of floats with the same shape when x is a list
  • Use a fully vectorized implementation without Python loops

Constraints

  • Vectorized implementation only
  • Time limit: 200 ms; Memory: 64 MB
  • Allowed library: NumPy only
Try Similar Problems
Relu ActivationSoftmax FunctionLog Loss Per SampleCross Entropy LossLogistic Regression Training

Sign in to take notes on this problem

Accepts: any

You must run your code first.