-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.go
75 lines (60 loc) · 1.69 KB
/
utils.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package mind
import (
"math"
"math/rand"
"time"
"github.com/gonum/matrix/mat64"
)
// Format the examples.
func Format(examples [][][]float64) (*mat64.Dense, *mat64.Dense) {
var input, output []float64
rows := len(examples)
inCols := len(examples[0][0])
outCols := len(examples[0][1])
for _, example := range examples {
output = append(output, example[1]...)
input = append(input, example[0]...)
}
return mat64.NewDense(rows, inCols, input), mat64.NewDense(rows, outCols, output)
}
// Normals returns a DenseMatrix filled with random values.
func Normals(rows, cols int) *mat64.Dense {
rand.Seed(time.Now().UTC().UnixNano())
ret := mat64.NewDense(rows, cols, nil)
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
ret.Set(i, j, rand.NormFloat64())
}
}
return ret
}
// Activator returns the activation function.
func Activator(f func(float64) float64) func(*mat64.Dense) *mat64.Dense {
return func(m *mat64.Dense) *mat64.Dense {
rows, cols := m.Dims()
res := mat64.NewDense(rows, cols, nil)
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
val := m.At(i, j)
res.Set(i, j, f(val))
}
}
return res
}
}
// Sigmoid calculates the sigmoid of `z`.
func Sigmoid(z float64) float64 {
return 1 / (1 + math.Exp(-z))
}
// SigmoidPrime calculates the sigmoid prime of `z`.
func SigmoidPrime(z float64) float64 {
return Sigmoid(z) * (1 - Sigmoid(z))
}
// Htan calculates the hyperbolic tangent of `z`.
func Htan(z float64) float64 {
return (math.Exp(2*z) - 1) / (math.Exp(2*z) + 1)
}
// Htanprime calculates the derivative of the hyperbolic tangent of `z`.
func Htanprime(z float64) float64 {
return 1 - (math.Pow((math.Exp(2*z)-1)/(math.Exp(2*z)+1), 2))
}