-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.go
187 lines (158 loc) · 3.51 KB
/
vector.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package vec
import (
"fmt"
"math"
)
// Vector is the vector struct
type Vector struct {
slice []float64
}
// At returns the ith element
func (v Vector) At(i int) float64 {
return v.slice[i]
}
// Set sets the ith element to the given float
func (v Vector) Set(i int, f float64) {
v.slice[i] = f
}
// SetData replaces the data of the vector
func (v Vector) SetData(data []float64) {
copy(v.slice, data)
}
// Add does the element wise addition with the given vector
func (v Vector) Add(other Vector) {
for i, n := range other.slice {
v.slice[i] += n
}
}
// AddScalar adds the given scalar to each element
func (v Vector) AddScalar(f float64) {
for i := range v.slice {
v.slice[i] += f
}
}
// Sub does the element wise substactions with the given vector
func (v Vector) Sub(other Vector) {
for i, f := range other.slice {
v.slice[i] -= f
}
}
// Mul does the element wise multiplication with the other vector
func (v Vector) Mul(other Vector) {
for i, f := range other.slice {
v.slice[i] *= f
}
}
// Len returns length of the vector
func (v Vector) Len() int {
return len(v.slice)
}
// Scale scales each element by the given float
func (v Vector) Scale(f float64) {
for i := range v.slice {
v.slice[i] *= f
}
}
// Pow applies the power function to each element with the given exponent
func (v Vector) Pow(f float64) {
for i := range v.slice {
v.slice[i] = math.Pow(v.slice[i], f)
}
}
// Exp applies the exponent function to each element
func (v Vector) Exp() {
for i := range v.slice {
v.slice[i] = math.Exp(v.slice[i])
}
}
// Sigmoid applies the sigmoid activation function to the elements
func (v Vector) Sigmoid() {
v.Scale(-1)
v.Exp()
v.AddScalar(1)
v.Pow(-1)
}
// SigmoidDer applies the sigmoid derivative function to the elements
func (v Vector) SigmoidDer() {
v.Sigmoid()
copy := Copy(v)
copy.Pow(2)
v.Sub(copy)
}
// Sum returns the sum of the elements
func (v Vector) Sum() float64 {
sum := 0.
for _, n := range v.slice {
sum += n
}
return sum
}
// Ln applies the natural logorithm to the receiver
func (v Vector) Ln() {
for i, n := range v.slice {
v.slice[i] = math.Log(n)
}
}
// Swap swaps the ith and jth elements
func (v Vector) Swap(i, j int) {
v.slice[i], v.slice[j] = v.slice[j], v.slice[i]
}
// ReLU applies the ReLU activation function to the receiver
func (v Vector) ReLU() {
for i, n := range v.slice {
v.slice[i] = math.Max(n, 0)
}
}
// ReLUDer applies the ReLU derivative to the receiver
func (v Vector) ReLUDer() {
for i, n := range v.slice {
if n > 0 {
v.slice[i] = 1
} else {
v.slice[i] = 0
}
}
}
// String returns a string representation of the Vector
func (v Vector) String() string {
return fmt.Sprintf("%v", v.slice)
}
// Index returns the first index of the given float, otherwise -1
func (v Vector) Index(value float64) int {
for i, f := range v.slice {
if f == value {
return i
}
}
return -1
}
// Append appends the given float to the vector
func (v *Vector) Append(f float64) {
v.slice = append(v.slice, f)
}
// Contains returns whether or not the vector contains the given value
func (v Vector) Contains(f float64) bool {
for _, n := range v.slice {
if n == f {
return true
}
}
return false
}
func (v Vector) Indices(value float64) []int {
indices := make([]int, 0)
for i, f := range v.slice {
if f == value {
indices = append(indices, i)
}
}
return indices
}
func (v *Vector) Remove(index int) {
v.slice = append(v.slice[:index], v.slice[index+1:]...)
}
func (v Vector) Softmax() {
v.Exp()
sum := v.Sum()
v.Scale(1. / sum)
}