Skip to content

Instantly share code, notes, and snippets.

@jogleasonjr
Created October 31, 2017 17:49
Show Gist options
  • Save jogleasonjr/55641e503142be19c9d3692b6579f221 to your computer and use it in GitHub Desktop.
Save jogleasonjr/55641e503142be19c9d3692b6579f221 to your computer and use it in GitHub Desktop.
C# Softmax
// Simplest C# Softmax example
// Based on the Python example here: https://en.wikipedia.org/wiki/Softmax_function
void Main()
{
var z = new[] { 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0 };
var z_exp = z.Select(Math.Exp);
// [2.72, 7.39, 20.09, 54.6, 2.72, 7.39, 20.09]
var sum_z_exp = z_exp.Sum();
// 114.98
var softmax = z_exp.Select(i => i / sum_z_exp);
// [0.024, 0.064, 0.175, 0.475, 0.024, 0.064, 0.175]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment