-
Notifications
You must be signed in to change notification settings - Fork 5
/
consopt.go
339 lines (298 loc) · 7.39 KB
/
consopt.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package golgi
import (
"fmt"
"github.com/pkg/errors"
G "gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
// ConsOpt is a construction option for layers
type ConsOpt func(Layer) (Layer, error)
// ReshapeFn defines a function to reshape a tensor
type ReshapeFn func(s tensor.Shape) tensor.Shape
type namesetter interface {
SetName(a string) error
}
type sizeSetter interface {
SetSize(int) error
}
type actSetter interface {
SetActivationFn(act ActivationFunction) error
}
type dropoutConfiger interface {
SetDropout(prob float64) error
}
type computeFLOPsSetter interface {
SetComputeFLOPs(toCompute bool) error
}
// WithName creates a layer that is named.
//
// If the layer is unnameable (i.e. trivial layers), then there is no effect.
func WithName(name string) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *FC:
l.name = name
return layer, nil
case *Embedding:
l.name = name
return layer, nil
case *layerNorm:
l.name = name
return layer, nil
case *LSTM:
case unnameable:
return layer, nil
case namesetter:
err := l.SetName(name)
return layer, err
case Pass:
return layer, nil
}
return nil, errors.Errorf("WithName Unhandled Layer type: %T", layer)
}
}
// AsBatched defines a layer that performs batched operation or not.
// By default most layers in this package are batched.
func AsBatched(batched bool) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *FC:
l.batched = batched
return layer, nil
case *LSTM:
case *Conv:
case reshape:
return layer, nil
case Pass:
return layer, nil
}
return nil, errors.Errorf("AsBatched Unhandled Layer type: %T", layer)
}
}
// WithBias defines a layer with or without a bias
func WithBias(withbias bool) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *FC:
l.nobias = !withbias
return layer, nil
}
return layer, nil
}
}
// WithSize automatically creates a layer of the given sizes.
func WithSize(size ...int) ConsOpt {
return func(layer Layer) (Layer, error) {
// NO RESHAPE ALLOWED
switch l := layer.(type) {
case *FC:
l.size = size[0]
return l, nil
case *Embedding:
l.dims = size[0]
return l, nil
case sizeSetter:
if err := l.SetSize(size[0]); err != nil {
return nil, err
}
return layer, nil
case Pass:
return layer, nil
case *Conv:
if err := l.SetSize(size...); err != nil {
return nil, err
}
return layer, nil
case *LSTM:
l.size = size[0]
return l, nil
}
return nil, errors.Errorf("WithSize Unhandled Layer type: %T", layer)
}
}
// WithBatchSize creates a layer with a given batch size.
func WithBatchSize(bs int) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *Embedding:
l.bs = bs
return l, nil
case Pass:
return layer, nil
}
return nil, errors.Errorf("WithBatchSize does not handle Layer type %T", layer)
}
}
// WithActivation sets the activation function of a layer
func WithActivation(act ActivationFunction) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *FC:
l.act = act
return layer, nil
case reshape:
return layer, nil
case actSetter:
return layer, l.SetActivationFn(act)
case Pass:
return layer, nil
}
return nil, errors.Errorf("WithActivation Unhandled Layer type: %T", layer)
}
}
// Of sets the type of the internal tensor
func Of(dt tensor.Dtype) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *Embedding:
l.of = dt
return layer, nil
case Pass:
return layer, nil
default:
return nil, errors.Errorf("Of does not yet support Layer type %T", layer)
}
}
}
// ToShape is a ConsOpt for Reshape only.
func ToShape(shp ...int) ConsOpt {
return func(layer Layer) (Layer, error) {
switch layer.(type) {
case reshape:
return reshape(tensor.Shape(shp)), nil
case Pass:
return layer, nil
}
return nil, errors.Errorf("ToShape Unhandled Layer type: %T", layer)
}
}
// WithProbability is a ConsOpt for Dropout only.
func WithProbability(prob float64) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case dropout:
return dropout(prob), nil
case dropoutConfiger:
err := l.SetDropout(prob)
if err != nil {
return nil, err
}
return layer, nil
case Pass:
return layer, nil
}
return nil, errors.Errorf("WithProbability Unhandled Layer type: %T", layer)
}
}
// WithEps is a ConsOpt for constructing Layer Norms only.
func WithEps(eps float64) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *layerNorm:
l.eps = eps
return l, nil
case Pass:
return layer, nil
}
return nil, errors.Errorf("WithEps Unhandled Layer type: %T", layer)
}
}
// WithConst is a construction option for the skip Layer
func WithConst(c *G.Node) ConsOpt {
return func(l Layer) (Layer, error) {
if a, ok := l.(*skip); ok {
a.b = c
return l, nil
}
return nil, errors.Errorf("WithConst expects a *skip. Got %T instead", l)
}
}
// WithWeights constructs a layer with the given weights.
func WithWeights(w *G.Node) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *FC:
l.w = w
l.initialized = true
case *Embedding:
l.w = w
// l.initialized = true
// this cannot be true unless l.oh has been set.
default:
return nil, errors.Errorf("WithWeights does not handle layer of type %T", layer)
}
return layer, nil
}
}
// WithKernelShape sets the kernel shape for convolution layers (Conv, MaxPool)
func WithKernelShape(s tensor.Shape) ConsOpt {
return func(l Layer) (Layer, error) {
switch c := l.(type) {
case *Conv:
c.kernelShape = s
return c, nil
case *MaxPool:
c.kernelShape = s
return c, nil
}
return nil, fmt.Errorf("Setting kernel shape is not supported by this layer: %T", l)
}
}
// WithPad sets the pad for convolution layers (Conv, MaxPool)
func WithPad(p []int) ConsOpt {
return func(l Layer) (Layer, error) {
switch c := l.(type) {
case *Conv:
c.pad = p
return c, nil
case *MaxPool:
c.pad = p
return c, nil
}
return nil, fmt.Errorf("Setting pad is not supported by this layer")
}
}
// WithStride sets the stride for convolution layers (Conv, MaxPool)
func WithStride(s []int) ConsOpt {
return func(l Layer) (Layer, error) {
switch c := l.(type) {
case *Conv:
c.stride = s
return c, nil
case *MaxPool:
c.stride = s
return c, nil
}
return nil, fmt.Errorf("Setting stride is not supported by this layer")
}
}
// WithDilation sets the dilation for convolution layers
func WithDilation(s []int) ConsOpt {
return func(l Layer) (Layer, error) {
switch c := l.(type) {
case *Conv:
c.dilation = s
return c, nil
}
return nil, fmt.Errorf("Setting dilation is not supported by this layer")
}
}
// ComputeFLOPs tells the layer to also compute FLOPS as the input is forwarded through it.
func ComputeFLOPs(toCompute bool) ConsOpt {
return func(layer Layer) (Layer, error) {
switch l := layer.(type) {
case *Embedding:
l.computeFLOPs = toCompute
return l, nil
case *Conv:
l.computeFLOPs = toCompute
return l, nil
case computeFLOPsSetter:
err := l.SetComputeFLOPs(toCompute)
return layer, err
case Pass:
return layer, nil
}
return nil, fmt.Errorf("Cannot set ComputeFLOPs - layer %T doesn't support this option.", layer)
}
}