PIXELBANKv9.1.0
Menu

Implement the softmax function, a crucial component in neural networks that converts logits to probabilities. This process is essential for making predictions in classification problems.

The softmax function takes a vector of real numbers as input and outputs a vector of values in the range (0, 1) that add up to 1, making it suitable for representing a probability distribution. The softmax function is often used in the output layer of a neural network to ensure that the output values can be interpreted as probabilities.

To apply the softmax function, the following steps are involved:

  1. Exponentiate each input value,
  2. Calculate the sum of these exponentiated values,
  3. Divide each exponentiated value by the sum.
softmax(xi)=exiβˆ‘jexj\text{softmax}(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}

This technique is widely used in image classification and natural language processing tasks.

Example:

Input:
softmax([1, 2, 3])
Output:
[0.0900, 0.2447, 0.6652]
Reasoning:
  • First, exponentiate each input: e1β‰ˆ2.7183e^1 \approx 2.7183, e2β‰ˆ7.3891e^2 \approx 7.3891, e3β‰ˆ20.0855e^3 \approx 20.0855.
  • Then, sum these exponentials: 2.7183+7.3891+20.0855β‰ˆ30.19292.7183 + 7.3891 + 20.0855 \approx 30.1929.
  • Finally, divide each exponential by this sum:
    • 2.7183/30.1929β‰ˆ0.09002.7183 / 30.1929 \approx 0.0900
    • 7.3891/30.1929β‰ˆ0.24477.3891 / 30.1929 \approx 0.2447
    • 20.0855/30.1929β‰ˆ0.665220.0855 / 30.1929 \approx 0.6652
      giving the output [0.0900,Β 0.2447,Β 0.6652][0.0900,\ 0.2447,\ 0.6652].

Constraints:

  • Return probabilities rounded to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Softmax - Medium | PixelBank