forked from fumin/ntm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.go
67 lines (56 loc) · 1.32 KB
/
unit.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
package ntm
import (
"fmt"
)
// A Unit is a node in a neural network, containing fields that are essential to efficiently compute gradients in the backward pass of a stochastic gradient descent training process.
type Unit struct {
Val float64 // value at node
Grad float64 // gradient at node
}
func (u Unit) String() string {
return fmt.Sprintf("{%.3g %.3g}", u.Val, u.Grad)
}
func makeTensorUnit2(n, m int) [][]Unit {
t := make([][]Unit, n)
for i := 0; i < len(t); i++ {
t[i] = make([]Unit, m)
}
return t
}
func makeTensorUnit3(n, m, p int) [][][]Unit {
t := make([][][]Unit, n)
for i := 0; i < len(t); i++ {
t[i] = makeTensorUnit2(m, p)
}
return t
}
func doUnit1(t []Unit, f func(*Unit)) {
for i := range t {
f(&t[i])
}
}
func doUnit2(t [][]Unit, f func(*Unit)) {
for _, v := range t {
doUnit1(v, f)
}
}
func doUnit3(t [][][]Unit, f func(*Unit)) {
for _, v := range t {
doUnit2(v, f)
}
}
func doUnit1Indices(t []Unit, f func([]int, *Unit)) {
for i := 0; i < len(t); i++ {
f([]int{i}, &t[i])
}
}
func doUnit2Indices(t [][]Unit, f func([]int, *Unit)) {
for i, a := range t {
doUnit1Indices(a, func(ids []int, u *Unit) { f(append(ids, i), u) })
}
}
func doUnit3Indices(t [][][]Unit, f func([]int, *Unit)) {
for i, a := range t {
doUnit2Indices(a, func(ids []int, u *Unit) { f(append(ids, i), u) })
}
}