-
Notifications
You must be signed in to change notification settings - Fork 0
/
util32.go
56 lines (45 loc) · 1 KB
/
util32.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
// Copyright 2016 The Neural Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package neural
import (
"math"
"math/rand"
)
func matrix32(I, J int) [][]float32 {
m, dense, offset := make([][]float32, I), make([]float32, I*J), 0
for i := 0; i < I; i++ {
m[i] = dense[offset : offset+J]
offset += J
}
return m
}
func vector32(I int, fill float32) []float32 {
v := make([]float32, I)
for i := 0; i < I; i++ {
v[i] = fill
}
return v
}
func random32(a, b float32) float32 {
return (b-a)*rand.Float32() + a
}
func sigmoid32(x float32) float32 {
return 1 / (1 + float32(math.Exp(-float64(x))))
}
func dsigmoid32(y float32) float32 {
return y * (1 - y)
}
func tanh32(x float32) float32 {
a, b := math.Exp(float64(x)), math.Exp(-float64(x))
return float32((a - b) / (a + b))
}
func dtanh32(x float32) float32 {
return 1 - x*x
}
func identity(x float32) float32 {
return x
}
func one(x float32) float32 {
return 1
}