← 返回 amazon 的题目列表Implement Numerically Stable Softmax
类型:online_judge
Problem: Implement Numerically Stable Softmax
Given a floating-point array x of length n, implement softmax:
[ \operatorname{softmax}(x_i) = \frac{e^{x_i}}{\sum_{j=1}^{n} e^{x_j}} ]
Print the softmax probability for every element, rounded to 6 decimal places.
The input may contain large values. Your implementation must be numerically stable and must not overflow when computing exponentials.
Input Format
First line: integer n
Second line: n floating-point values x_1, x_2, ..., x_n
Output Format
Print n softmax values separated by spaces, each rounded to 6 decimal places.
Constraints
1 <= n <= 10^5
-10^4 <= x_i <= 10^4
Example
Input
3
1 2 3
Output
0.090031 0.244728 0.665241
Example
Input
3
1 2 3
Output
0.090031 0.244728 0.665241