PIXELBANKv9.1.0
Menu

Implement the softmax function for multi-class classification.

Given a list of logits (raw scores) z=[z1,z2,...,zk]z = [z_1, z_2, ..., z_k], compute the softmax probabilities:

softmax(zi)=ezi∑j=1kezj\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^{k} e^{z_j}}

For numerical stability, subtract the maximum value from all logits before exponentiating: softmax(zi)=ezi−max⁡(z)∑j=1kezj−max⁡(z)\text{softmax}(z_i) = \frac{e^{z_i - \max(z)}}{\sum_{j=1}^{k} e^{z_j - \max(z)}}

Return the probability distribution as a list, rounded to 4 decimal places.

Example:

Input:
logits = [2.0, 1.0, 0.1]
Output:
[0.6590, 0.2424, 0.0986]
Reasoning:
  • First, we find the maximum value in the input list: max⁡(z)=2.0\max(z) = 2.0
  • Then, we subtract this maximum value from all logits to ensure numerical stability: z′=[2.0−2.0,1.0−2.0,0.1−2.0]=[0.0,−1.0,−1.9]z' = [2.0-2.0, 1.0-2.0, 0.1-2.0] = [0.0, -1.0, -1.9]
  • Next, we compute the exponentials of these stabilized logits: ez′=[e0.0,e−1.0,e−1.9]≈[1.0,0.368,0.149]e^{z'} = [e^{0.0}, e^{-1.0}, e^{-1.9}] \approx [1.0, 0.368, 0.149]
  • Finally, we calculate the softmax probabilities by dividing each exponential by their sum: softmax(zi)=ezi∑j=1kezj≈[1.0,0.368,0.149]1.0+0.368+0.149≈[0.6590,0.2424,0.0986]\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^{k} e^{z_j}} \approx \frac{[1.0, 0.368, 0.149]}{1.0 + 0.368 + 0.149} \approx [0.6590, 0.2424, 0.0986]

Constraints:

  • Input is a list of floats (logits)
  • Return a list of probabilities that sum to 1 (approximately)
  • Round each value to 4 decimal places
  • Use the numerical stability trick (subtract max)
solution.py

Test Results

0/0
Run code to see test results.