-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.go
281 lines (266 loc) · 7.67 KB
/
class.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package qol
import (
"fmt"
"reflect"
"gorgonia.org/tensor"
"gorgonia.org/tensor/native"
)
// ClassType returns the tensor.Dtype for Class. This allows it to be used in Gorgonia graphs.
func ClassType() tensor.Dtype {
var c Class
return tensor.Dtype{reflect.TypeOf(c)}
}
// Class represents the class ID of a dataset. It is an unbound type with a minimum of 0.
type Class uint
// ToClass converts a OneHotVector to a Class. This function panics if `a` is not a vector.
//
// The default threshold is 0.55 if 0 is passed in. The threshold does not apply to int tensors.
//
// Some behavioural notes:
// This function is NOT an argmax function. If the following vector is passed in,
// [0.1 0.1 0.6 0.7 0.1]
// the class returned will be 2, not 3.
//
// The same behaviour applies to `int` an `uint` tensor.
//
// For int Tensors, it assumes any value larger or equal to 1 is 1, and any value < 0 is 0. So the Class of the following:
// [0 -1 3 3 1 0]
// will also be 2.
func ToClass(a tensor.Tensor, threshold float64) Class {
if a.RequiresIterator() {
panic(fmt.Sprintf("NYI: ToClass for a Tensor that requires an iterator. \n%v", a))
}
if !a.Shape().IsVector() {
panic(fmt.Sprintf("ToClass only works on vectors. The shape of `a` was %v", a.Shape()))
}
if threshold == 0 {
threshold = 0.55
}
data := a.Data()
switch d := data.(type) {
case []float32:
thresh := float32(threshold)
for i := range d {
if d[i] > thresh {
return Class(i)
}
}
case []float64:
thresh := threshold
for i := range d {
if d[i] > thresh {
return Class(i)
}
}
case []int:
// no threshold
for i := range d {
if d[i] >= 1 {
return Class(i)
}
}
case []uint:
// no threshold
for i := range d {
if d[i] >= 1 {
return Class(i)
}
}
default:
panic(fmt.Sprintf("Data of type %T not implemented for ToClass()", data))
}
panic("Unreachable")
}
// ToClass converts a OneHotMatrix to Classes. This function panics if `a` is not a matrix.
// The default threshold is 0.55 if 0 is passed in. The threshold does not apply to int tensors.
//
// Some behavioural notes:
// This function is NOT an argmax function. If the following matrix is passed in,
// [0.1 0.1 0.6 0.7 0.1]
// [0.6 0.1 0.1 0.7 0.1]
// the class returned will be [2 0], not [3 3].
//
// The same behaviour applies to `int` an `uint` tensor.
//
// For int Tensors, it assumes any value larger or equal to 1 is 1, and any value < 0 is 0. So the Class of the following:
// [0 -1 3 3 1 0]
// [2 0 -1 3 0 0]
// will also be [2 0] and not [2 3] or [3 3].
func ToClasses(a tensor.Tensor, threshold float64) []Class {
if a.RequiresIterator() {
panic(fmt.Sprintf("NYI: ToClass for a Tensor that requires an iterator. \n%v", a))
}
if !a.Shape().IsMatrix() {
panic(fmt.Sprintf("ToClass only works on vectors. The shape of `a` was %v", a.Shape()))
}
if threshold == 0 {
threshold = 0.55
}
iter, err := native.Matrix(a.(*tensor.Dense))
if err != nil {
panic(err)
}
retVal := make([]Class, a.Shape()[0])
switch d := iter.(type) {
case [][]float32:
thresh := float32(threshold)
for i := range d {
for j := range d[i] {
if d[i][j] > thresh {
retVal[i] = Class(j)
break
}
if j == len(d[i])-1 {
panic(fmt.Sprintf("Unreachable class in matrix at row: %d", i))
}
}
}
case [][]float64:
thresh := threshold
for i := range d {
for j := range d[i] {
if d[i][j] > thresh {
retVal[i] = Class(j)
break
}
if j == len(d[i])-1 {
panic(fmt.Sprintf("Unreachable class in matrix at row: %d", i))
}
}
}
case [][]int:
for i := range d {
for j := range d[i] {
if d[i][j] >= 1 {
retVal[i] = Class(j)
break
}
if j == len(d[i])-1 {
panic(fmt.Sprintf("Unreachable class in matrix at row: %d", i))
}
}
}
case [][]uint:
for i := range d {
for j := range d[i] {
if d[i][j] >= 1 {
retVal[i] = Class(j)
break
}
if j == len(d[i])-1 {
panic(fmt.Sprintf("Unreachable class in matrix at row: %d", i))
}
}
}
default:
panic(fmt.Sprintf("Data of type %T not implemented for ToClasses()", iter))
}
return retVal
}
// ToOneHotVector converts a Class to a OneHotVector.
//
// The dtype defaults to tensor.Float64 if an empty Dtype was passed in.
func ToOneHotVector(a Class, numClasses uint, dtype tensor.Dtype) *tensor.Dense {
if dtype.Type == nil {
dtype = tensor.Float64
}
retVal := tensor.New(tensor.Of(dtype), tensor.WithShape(int(numClasses)))
return UnsafeToOneHotVector(a, numClasses, retVal)
}
// ToOneHotMatrix converts a slice of Class to a OneHotMatrix.
//
// The dtype defaults to tensor.Float64 if an empty Dtype was passed in.
func ToOneHotMatrix(a []Class, numClasses uint, dtype tensor.Dtype) *tensor.Dense {
if dtype.Type == nil {
dtype = tensor.Float64
}
retVal := tensor.New(tensor.Of(dtype), tensor.WithShape(len(a), int(numClasses)))
return UnsafeToOneHotMatrix(a, numClasses, retVal)
}
// UnsafeToOneHotVector converts a class to a OneHotVector, in the given
// tensor.Tensor. It expects the MaxClass the length of the given vector. Panics
// otherwise.
func UnsafeToOneHotVector(a Class, numClasses uint, reuse *tensor.Dense) *tensor.Dense {
if !reuse.Shape().IsVector() {
panic(fmt.Sprintf("UnsafeToOneHotVector only works on vectors. The shape of `reuse` was %v", reuse.Shape()))
}
if reuse.Shape()[0] != int(numClasses) {
panic(fmt.Sprintf("UnsafeToOneHotVector expects length of `reuse`: %d to equal `numClasses`: %d", reuse.Shape()[0], int(numClasses)))
}
dt := reuse.Dtype()
id := int(a)
reuse.Zero()
var err error
switch dt {
case tensor.Float32:
err = reuse.SetAt(float32(1), id)
case tensor.Float64:
err = reuse.SetAt(float64(1), id)
case tensor.Int64:
err = reuse.SetAt(int64(1), id)
case tensor.Int:
err = reuse.SetAt(int(1), id)
case tensor.Int32:
err = reuse.SetAt(int32(1), id)
default:
panic(fmt.Sprintf("UnsafeToOneHotVector not implemented for %v", dt))
}
if err != nil {
panic(err.Error())
}
return reuse
}
// UnsafeToOneHotMatrix converts a slice of Class to a OneHotMatrix, in the given tensor.Tensor. It expects a matrix of shape (len(a), numClasses). Panics otherwise.
func UnsafeToOneHotMatrix(a []Class, numClasses uint, reuse *tensor.Dense) *tensor.Dense {
if !reuse.Shape().IsMatrix() {
panic(fmt.Sprintf("UnsafeToOneHotMatrix only works on matracies. The shape of `reuse` was %v", reuse.Shape()))
}
if reuse.Shape()[0] != len(a) {
panic(fmt.Sprintf("UnsafeToOneHotMatrix expects `len(a)` %d to be the number of rows in `reuse`: %d", len(a), reuse.Shape()[0]))
}
if reuse.Shape()[1] != int(numClasses) {
panic(fmt.Sprintf("UnsafeToOneHotMatrix expects class dim (columns) of `reuse`: %d to equal `numClasses`: %d", reuse.Shape()[1], int(numClasses)))
}
dt := reuse.Dtype()
reuse.Zero()
var err error
// Handline shapes of [1, numClasses]
if reuse.IsRowVec() {
switch dt {
case tensor.Float32:
reuse.Set(int(a[0]), float32(1))
case tensor.Float64:
reuse.Set(int(a[0]), float64(1))
case tensor.Int64:
reuse.Set(int(a[0]), int64(1))
case tensor.Int:
reuse.Set(int(a[0]), int(1))
case tensor.Int32:
reuse.Set(int(a[0]), int32(1))
default:
panic(fmt.Sprintf("UnsafeToOneHotVector not implemented for %v", dt))
}
return reuse
}
for i := range a {
id := int(a[i]) //+
switch dt {
case tensor.Float32:
err = reuse.SetAt(float32(1), i, id)
case tensor.Float64:
err = reuse.SetAt(float64(1), i, id)
case tensor.Int64:
err = reuse.SetAt(int64(1), i, id)
case tensor.Int:
err = reuse.SetAt(int(1), i, id)
case tensor.Int32:
err = reuse.SetAt(int32(1), i, id)
default:
panic(fmt.Sprintf("UnsafeToOneHotVector not implemented for %v", dt))
}
if err != nil {
panic(err.Error())
}
}
return reuse
}