forked from fumin/ntm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
57 lines (50 loc) · 944 Bytes
/
math.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
package ntm
import (
"fmt"
"math"
)
const (
machineEpsilon = 2.2e-16
machineEpsilonSqrt = 1e-8 // math.Sqrt(machineEpsilon)
)
// Sigmoid computes 1 / (1 + math.Exp(-x))
func Sigmoid(x float64) float64 {
return 1.0 / (1 + math.Exp(-x))
}
func delta(a, b int) float64 {
if a == b {
return 1
}
return 0
}
func cosineSimilarity(u, v []float64) float64 {
var sum float64 = 0
var usum float64 = 0
var vsum float64 = 0
for i := 0; i < len(u); i++ {
sum += u[i] * v[i]
usum += u[i] * u[i]
vsum += v[i] * v[i]
}
return sum / math.Sqrt(usum*vsum)
}
func makeTensor2(n, m int) [][]float64 {
t := make([][]float64, n)
for i := 0; i < len(t); i++ {
t[i] = make([]float64, m)
}
return t
}
// Sprint2 pretty prints a 2 dimensional tensor.
func Sprint2(t [][]float64) string {
s := "["
for _, t1 := range t {
s += "["
for _, t2 := range t1 {
s += fmt.Sprintf(" %.2f", t2)
}
s += "]"
}
s += "]"
return s
}