-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbatch_normalization.go
More file actions
230 lines (191 loc) · 5.65 KB
/
Copy pathbatch_normalization.go
File metadata and controls
230 lines (191 loc) · 5.65 KB
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
package opset13
import (
"github.com/advancedclimatesystems/gonnx/onnx"
"github.com/advancedclimatesystems/gonnx/ops"
"gorgonia.org/tensor"
)
const (
MinBatchNormalizationInputs = 5
MaxBatchNormalizationInputs = 5
BatchNormalizationDefaultEpsilon = 1e-5
BatchNormalizationDefaultMomentum = 0.9
)
// BatchNormalization represents the ONNX batchNormalization operator.
type BatchNormalization struct {
epsilon float32
momentum float32
testMode bool
}
// newBatchNormalization creates a new batchNormalization operator.
func newBatchNormalization() ops.Operator {
return &BatchNormalization{
epsilon: BatchNormalizationDefaultEpsilon,
momentum: BatchNormalizationDefaultMomentum,
}
}
// Init initializes the batchNormalization operator.
func (b *BatchNormalization) Init(n *onnx.NodeProto) error {
hasMomentum := false
for _, attr := range n.GetAttribute() {
switch attr.GetName() {
case "epsilon":
b.epsilon = attr.GetF()
case "momentum":
hasMomentum = true
b.momentum = attr.GetF()
default:
return ops.ErrInvalidAttribute(attr.GetName(), b)
}
}
if !hasMomentum {
b.testMode = true
}
// We only support test mode, as this is by far the most common for inference models.
if !b.testMode {
return ops.ErrUnsupportedAttribute("momentum", b)
}
return nil
}
// Apply applies the batchNormalization operator.
func (b *BatchNormalization) Apply(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
X := inputs[0]
scale := inputs[1]
B := inputs[2]
mean := inputs[3]
variance := inputs[4]
out, err := b.testModeCalculation(X, scale, B, mean, variance)
if err != nil {
return nil, err
}
return []tensor.Tensor{out}, nil
}
// ValidateInputs validates the inputs that will be given to Apply for this operator.
func (b *BatchNormalization) ValidateInputs(inputs []tensor.Tensor) ([]tensor.Tensor, error) {
return ops.ValidateInputs(b, inputs)
}
// GetMinInputs returns the minimum number of input tensors this operator expects.
func (b *BatchNormalization) GetMinInputs() int {
return MinBatchNormalizationInputs
}
// GetMaxInputs returns the maximum number of input tensors this operator expects.
func (b *BatchNormalization) GetMaxInputs() int {
return MaxBatchNormalizationInputs
}
// GetInputTypeConstraints returns a list. Every element represents a set of allowed tensor dtypes
// for the corresponding input tensor.
func (b *BatchNormalization) GetInputTypeConstraints() [][]tensor.Dtype {
return [][]tensor.Dtype{
{tensor.Float32, tensor.Float64},
{tensor.Float32, tensor.Float64},
{tensor.Float32, tensor.Float64},
{tensor.Float32, tensor.Float64},
{tensor.Float32, tensor.Float64},
}
}
// String implements the stringer interface, and can be used to format errors or messages.
func (b *BatchNormalization) String() string {
return "batchNormalization operator"
}
func (b *BatchNormalization) reshapeTensors(X, scale, bias, mean, variance tensor.Tensor) (newScale, newBias, newMean, newVariance tensor.Tensor, err error) {
nNonSpatialDims := 2
nSpatialDims := len(X.Shape()) - nNonSpatialDims
if nSpatialDims <= 0 {
return scale, bias, mean, variance, nil
}
// The new shape for the `scale`, `bias`, `mean` and `variance` tensors should
// be (C, 1, 1, ...), such that they can be broadcasted to match the shape of `X`.
newShape := make([]int, 1+nSpatialDims)
// Here we set the channel dimension. The channel dimension is the same
// for all `X`, `scale`, `bias`, `mean` and `variance` tensors.
newShape[0] = scale.Shape()[0]
// Set all the remaining dimensions to 1 to allow for broadcasting.
for i := 1; i < len(newShape); i++ {
newShape[i] = 1
}
// Now we create new tensors for all the input tensors (except `X`) and reshape
// them.
newScale, ok := scale.Clone().(tensor.Tensor)
if !ok {
return nil, nil, nil, nil, ops.ErrTypeAssert("tensor.Tensor", scale.Clone())
}
newBias, ok = bias.Clone().(tensor.Tensor)
if !ok {
return nil, nil, nil, nil, ops.ErrTypeAssert("tensor.Tensor", bias.Clone())
}
newMean, ok = mean.Clone().(tensor.Tensor)
if !ok {
return nil, nil, nil, nil, ops.ErrTypeAssert("tensor.Tensor", mean.Clone())
}
newVariance, ok = variance.Clone().(tensor.Tensor)
if !ok {
return nil, nil, nil, nil, ops.ErrTypeAssert("tensor.Tensor", variance.Clone())
}
err = newScale.Reshape(newShape...)
if err != nil {
return nil, nil, nil, nil, err
}
err = newBias.Reshape(newShape...)
if err != nil {
return nil, nil, nil, nil, err
}
err = newMean.Reshape(newShape...)
if err != nil {
return nil, nil, nil, nil, err
}
err = newVariance.Reshape(newShape...)
if err != nil {
return nil, nil, nil, nil, err
}
return
}
func (b *BatchNormalization) testModeCalculation(X, scale, bias, mean, variance tensor.Tensor) (tensor.Tensor, error) {
newScale, newBias, newMean, newVariance, err := b.reshapeTensors(X, scale, bias, mean, variance)
if err != nil {
return nil, err
}
numerator, err := ops.ApplyBinaryOperation(
X,
newMean,
ops.Sub,
ops.UnidirectionalBroadcasting,
)
if err != nil {
return nil, err
}
numerator, err = ops.ApplyBinaryOperation(
numerator[0],
newScale,
ops.Mul,
ops.UnidirectionalBroadcasting,
)
if err != nil {
return nil, err
}
denominator, err := tensor.Add(newVariance, b.epsilon)
if err != nil {
return nil, err
}
denominator, err = tensor.Sqrt(denominator)
if err != nil {
return nil, err
}
outputs, err := ops.ApplyBinaryOperation(
numerator[0],
denominator,
ops.Div,
ops.UnidirectionalBroadcasting,
)
if err != nil {
return nil, err
}
outputs, err = ops.ApplyBinaryOperation(
outputs[0],
newBias,
ops.Add,
ops.UnidirectionalBroadcasting,
)
if err != nil {
return nil, err
}
return outputs[0], nil
}