-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
106 lines (81 loc) · 1.85 KB
/
helpers.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package lib
import (
"fmt"
"math"
"math/rand"
"strconv"
"gonum.org/v1/gonum/mat"
)
func log(matrix mat.Matrix) mat.Dense {
var res mat.Dense
res.Apply(func(i, j int, v float64) float64 {
return math.Log(v)
}, matrix)
return res
}
func subtract(value float64, matrix mat.Matrix) mat.Dense {
var res mat.Dense
res.Apply(func(i, j int, v float64) float64 {
return value - v
}, matrix)
return res
}
func multiply(matrix mat.Matrix, value float64) mat.Dense {
var res mat.Dense
res.Apply(func(i, j int, v float64) float64 {
return v * value
}, matrix)
return res
}
func divide(matrix mat.Matrix, value float64) mat.Dense {
var res mat.Dense
res.Apply(func(i, j int, v float64) float64 {
return v / value
}, matrix)
return res
}
func sumRows(matrix mat.Matrix) mat.Dense {
rows, _ := matrix.Dims()
res := make([]float64, rows)
for i := 0; i < rows; i++ {
row := matrix.(*mat.Dense).RawRowView(i)
var sum float64
for r := 0; r < len(row); r++ {
sum += row[r]
}
res[i] = sum
}
return *mat.NewDense(rows, 1, res)
}
func columnBroadcast(bias mat.Matrix, cols int) mat.Dense {
b := bias.(*mat.Dense)
rows, _ := bias.Dims()
res := mat.NewDense(rows, cols, nil)
for i := 0; i < rows; i++ {
row := make([]float64, cols)
val := b.RawRowView(i)[0]
for i := range row {
row[i] = val
}
res.SetRow(i, row)
}
return *res
}
func normRand(len int) []float64 {
res := make([]float64, len)
for i := 0; i < len; i++ {
res[i] = rand.NormFloat64()
}
return res
}
func printMatrix(name string, matrix mat.Matrix) {
space := " "
formatted := mat.Formatted(matrix, mat.Prefix(space), mat.Squeeze())
fmt.Printf("%s:\r\n%s%v\r\n", name, space, formatted)
}
func printMatrices(name string, matrices []mat.Dense) {
fmt.Printf("%s:\r\n", name)
for i, matrix := range matrices {
printMatrix(strconv.Itoa(i), &matrix)
}
}