-
Notifications
You must be signed in to change notification settings - Fork 0
/
numgo.go
83 lines (72 loc) · 1.34 KB
/
numgo.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
76
77
78
79
80
81
82
83
package main
import (
"math"
"math/rand"
)
type Obj struct {
data []float64
}
/*func (o *Obj) Init(d []float64) {
o.size = len(d)
if d == nil {
o.data = make([]float64, o.size)
} else {
o.data = d
}
}*/
func (o *Obj) Vector() []float64 {
return o.data
}
func VectorInit(size int, n float64) []float64 {
v := make([]float64, size)
for i := 0; i < size; i++ {
v[i] = n
}
return v
}
func (o *Obj) Matrix(m, n int) [][]float64 {
if len(o.data) != m*n {
return nil //error
}
mat := make([][]float64, len(o.data))
for i := 0; i < m; i++ {
mat[i] = make([]float64, n)
ind := m * i
for j := 0; j < n; j++ {
mat[i][j] = o.data[ind+j]
}
}
return mat
}
func MatrixInit(m, n int, v float64) [][]float64 {
mat := make([][]float64, m)
for i := 0; i < m; i++ {
mat[i] = make([]float64, n)
for j := 0; j < n; j++ {
mat[i][j] = v
}
}
return mat
}
func Tensor3Init(l, m, n int, v float64) [][][]float64 {
ten := make([][][]float64, l)
for i := 0; i < l; i++ {
ten[i] = make([][]float64, m)
for j := 0; j < m; j++ {
ten[i][j] = make([]float64, n)
for k := 0; k < n; k++ {
ten[i][j][k] = v
}
}
}
return ten
}
func Sigmoid(x float64) float64 {
return 1 / (1 + math.Exp(-x))
}
func Dsigmoid(y float64) float64 {
return y * (1 - y)
}
func Random(a, b float64) float64 {
return (b-a)*rand.Float64() + a
}