-
Notifications
You must be signed in to change notification settings - Fork 5
/
softmax.go
51 lines (43 loc) · 1.12 KB
/
softmax.go
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
46
47
48
49
50
51
package activation
import (
"fmt"
"github.com/Elvenson/xgboost-go/mat"
"github.com/Elvenson/xgboost-go/protobuf"
"github.com/chewxy/math32"
)
// Softmax is struct contains necessary data for doing logistic calculation
// for now is empty.
type Softmax struct{}
// softmax function.
func softmax(vector mat.Vector) mat.Vector {
var sum float32 = 0.0
r := make([]float32, len(vector))
for i, v := range vector {
exp := math32.Exp(v)
r[i] = exp
sum += exp
}
if sum != 0.0 {
inverseSum := 1.0 / sum
for i := range r {
r[i] *= inverseSum
}
}
return r
}
// Transform passes prediction through softmax function.
func (a *Softmax) Transform(rawPredictions mat.Vector) (mat.Vector, error) {
if len(rawPredictions) == 0 {
return mat.Vector{}, fmt.Errorf("prediction should have at least 1 dimension")
}
p := softmax(rawPredictions)
return p, nil
}
// Type returns activation type.
func (a *Softmax) Type() protobuf.ActivateType {
return protobuf.ActivateType_SOFTMAX
}
// Name returns activation name.
func (a *Softmax) Name() string {
return protobuf.ActivateType_name[int32(protobuf.ActivateType_SOFTMAX)]
}