TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Softmax Function

Activation Functions
Medium

Convert logits into probabilities. For a one-dimensional input, normalize the full vector. For a two-dimensional input, normalize each row independently.

pi=exi−m∑jexj−mp_i = \frac{e^{x_i - m}}{\sum_j e^{x_j - m}}pi​=∑j​exj​−mexi​−m​

Here, xix_ixi​ is logit iii, mmm is the maximum logit in the same vector or row, and pip_ipi​ is the resulting probability. Subtracting mmm prevents overflow without changing the probabilities. Return a NumPy array with the same shape as the input.

Loading visualization...

Examples

Input: x = [1, 2, 3]

Output: [0.090031, 0.244728, 0.665241]

Explanation: Subtracting 3 gives [-2, -1, 0]; exponentiating and dividing by the sum produces the probability vector.

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

Output: [[0.090031, 0.244728, 0.665241], [0.333333, 0.333333, 0.333333]]

Hint 1

Use np.max(x) for a vector and np.max(x, axis=1, keepdims=True) for a matrix.

Hint 2

Compute exp_values / exp_values.sum(...) with the same axis used for the maximum.

Requirements

  • Normalize a one-dimensional input across all elements
  • Normalize a two-dimensional input independently across each row
  • Subtract the relevant maximum before exponentiation
  • Return a NumPy array with the same shape as the input

Constraints

  • The input is a finite one-dimensional or two-dimensional numeric list
  • The input contains at most 10610^6106 values
  • Use NumPy only
Try Similar Problems
Cross Entropy LossLog Loss Per SampleKl DivergenceLabel Smoothing LossSigmoid Numpy

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Implement Softmax Function

Activation Functions
Medium

Convert logits into probabilities. For a one-dimensional input, normalize the full vector. For a two-dimensional input, normalize each row independently.

pi=exi−m∑jexj−mp_i = \frac{e^{x_i - m}}{\sum_j e^{x_j - m}}pi​=∑j​exj​−mexi​−m​

Here, xix_ixi​ is logit iii, mmm is the maximum logit in the same vector or row, and pip_ipi​ is the resulting probability. Subtracting mmm prevents overflow without changing the probabilities. Return a NumPy array with the same shape as the input.

Loading visualization...

Examples

Input: x = [1, 2, 3]

Output: [0.090031, 0.244728, 0.665241]

Explanation: Subtracting 3 gives [-2, -1, 0]; exponentiating and dividing by the sum produces the probability vector.

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

Output: [[0.090031, 0.244728, 0.665241], [0.333333, 0.333333, 0.333333]]

Hint 1

Use np.max(x) for a vector and np.max(x, axis=1, keepdims=True) for a matrix.

Hint 2

Compute exp_values / exp_values.sum(...) with the same axis used for the maximum.

Requirements

  • Normalize a one-dimensional input across all elements
  • Normalize a two-dimensional input independently across each row
  • Subtract the relevant maximum before exponentiation
  • Return a NumPy array with the same shape as the input

Constraints

  • The input is a finite one-dimensional or two-dimensional numeric list
  • The input contains at most 10610^6106 values
  • Use NumPy only
Try Similar Problems
Cross Entropy LossLog Loss Per SampleKl DivergenceLabel Smoothing LossSigmoid Numpy

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.