forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoftMax.cs
45 lines (40 loc) · 1.54 KB
/
SoftMax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
namespace Algorithms.Numeric;
/// <summary>
/// Implementation of the SoftMax function.
/// Its a function that takes as input a vector of K real numbers, and normalizes
/// it into a probability distribution consisting of K probabilities proportional
/// to the exponentials of the input numbers. After softmax, the elements of the vector always sum up to 1.
/// https://en.wikipedia.org/wiki/Softmax_function.
/// </summary>
public static class SoftMax
{
/// <summary>
/// Compute the SoftMax function.
/// The SoftMax function is defined as:
/// softmax(x_i) = exp(x_i) / sum(exp(x_j)) for j = 1 to n
/// where x_i is the i-th element of the input vector.
/// The elements of the output vector are the probabilities of the input vector, the output sums up to 1.
/// </summary>
/// <param name="input">The input vector of real numbers.</param>
/// <returns>The output vector of real numbers.</returns>
public static double[] Compute(double[] input)
{
if (input.Length == 0)
{
throw new ArgumentException("Array is empty.");
}
var exponentVector = new double[input.Length];
var sum = 0.0;
for (var index = 0; index < input.Length; index++)
{
exponentVector[index] = Math.Exp(input[index]);
sum += exponentVector[index];
}
for (var index = 0; index < input.Length; index++)
{
exponentVector[index] /= sum;
}
return exponentVector;
}
}