File: softmax.go

package info (click to toggle)
golang-github-montanaflynn-stats 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: makefile: 27
file content (25 lines) | stat: -rw-r--r-- 517 bytes parent folder | download | duplicates (2)
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
package stats

import "math"

// SoftMax returns the input values in the range of 0 to 1
// with sum of all the probabilities being equal to one. It
// is commonly used in machine learning neural networks.
func SoftMax(input Float64Data) ([]float64, error) {
	if input.Len() == 0 {
		return Float64Data{}, EmptyInput
	}

	s := 0.0
	c, _ := Max(input)
	for _, e := range input {
		s += math.Exp(e - c)
	}

	sm := make([]float64, len(input))
	for i, v := range input {
		sm[i] = math.Exp(v-c) / s
	}

	return sm, nil
}