PIXELBANKv9.1.0
Menu

Softmax Policy Probabilities

Problem Statement

Convert action preferences h into a policy using the softmax (Boltzmann) distribution with temperature tau:

π(a)=eha/τ∑behb/τ\pi(a) = \frac{e^{h_a / \tau}}{\sum_b e^{h_b / \tau}}

Implement softmax_policy(h, tau) returning a list of probabilities. Use the max-subtraction trick for numerical stability.

Example:

Input:
softmax_policy([1.0, 1.0], 1.0)
Output:
[0.5, 0.5]
Reasoning:
  • Divide each preference by the temperature to obtain the scaled logits: z=[1.0/1.0,1.0/1.0]=[1.0,1.0]z = [1.0/1.0, 1.0/1.0] = [1.0, 1.0].
  • Identify the maximum value in the scaled logits for numerical stability: m=max⁡(1.0,1.0)=1.0m = \max(1.0, 1.0) = 1.0.
  • Compute the exponentials of the shifted values (zi−mz_i - m) to prevent overflow: e1.0−1.0=e0=1.0e^{1.0 - 1.0} = e^0 = 1.0 for both elements, resulting in [1.0,1.0][1.0, 1.0].
  • Sum the exponentials to determine the normalization constant: s=1.0+1.0=2.0s = 1.0 + 1.0 = 2.0.
  • Normalize each exponential by the sum to get the final probabilities: 1.0/2.0=0.51.0 / 2.0 = 0.5 for each action.
  • The final output is [0.5, 0.5]

Constraints:

  • 1 <= len(h) <= 1000, tau > 0
  • Probabilities must sum to 1.
  • Subtract max(h/tau) before exponentiating for stability.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Softmax Policy Probabilities - Easy | PixelBank