-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb45f4a
commit 62ac483
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using Algorithms.Numeric; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Numeric; | ||
|
||
public static class SoftMaxTests | ||
{ | ||
[TestCase(new[] {5.0, 5.0}, new[] {0.5, 0.5})] | ||
[TestCase(new[] {1.0, 2.0, 3.0}, new[] {0.09003057317038046, 0.24472847105479767, 0.6652409557748219})] | ||
[TestCase(new[] {0.0}, new[] {1.0})] | ||
public static void SoftMaxFunction(double[] input, double[] expected) | ||
{ | ||
// Act | ||
var result = SoftMax.Compute(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected).Within(1e-9)); | ||
} | ||
|
||
[Test] | ||
public static void SoftMaxFunctionThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var input = Array.Empty<double>(); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentException>(() => SoftMax.Compute(input)); | ||
} | ||
|
||
[TestCase(new[] {1.0, 2.0, 3.0, 4.0, 5.0})] | ||
[TestCase(new[] {0.0, 0.0, 0.0, 0.0, 0.0})] | ||
[TestCase(new[] {5.0})] | ||
public static void SoftMaxFunctionSumsToOne(double[] input) | ||
{ | ||
// Act | ||
var result = SoftMax.Compute(input); | ||
|
||
var sum = 0.0; | ||
foreach (var value in result) | ||
{ | ||
sum += value; | ||
} | ||
|
||
// Assert | ||
Assert.That(sum, Is.EqualTo(1.0).Within(1e-9)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters