Back to Problems

Softmax Activation Function Implementation (easy)

Write a Python function that computes the softmax activation for a given list of scores. The function should return the softmax values as a list, each rounded to four decimal places.

Example

Example:
        input: scores = [1, 2, 3]
        output: [0.0900, 0.2447, 0.6652]
        reasoning: The softmax function converts a list of values into a probability distribution. The probabilities are proportional to the exponential of each element divided by the sum of the exponentials of all elements in the list.

Understanding the Softmax Activation Function

The softmax function is a generalization of the sigmoid function and is used in the output layer of a neural network model that handles multi-class classification tasks.

Mathematical Definition

The softmax function is mathematically represented as: \[ \text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j} e^{z_j}} \]

Characteristics

  • Output Range: Each output value is between 0 and 1, and the sum of all outputs is 1.
  • Purpose: It transforms scores into probabilities, which are easier to interpret and are useful for classification.
This function is essential for models where the output needs to represent a probability distribution across multiple classes.
import math
def softmax(scores: list[float]) -> list[float]:
    exp_scores = [math.exp(score) for score in scores]
    sum_exp_scores = sum(exp_scores)
    probabilities = [round(score / sum_exp_scores, 4) for score in exp_scores]
    return probabilities
Contributors

No current contributors.

Want to help out? Contribute here.

Your Solution

Output will be shown here.