This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 949
/
batchnorm.ts
422 lines (401 loc) · 15 KB
/
batchnorm.ts
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {deprecationWarn, ENV} from '../environment';
import {Tensor, Tensor1D, Tensor2D, Tensor3D, Tensor4D} from '../tensor';
import {convertToTensor} from '../tensor_util_env';
import {Rank, TensorLike} from '../types';
import * as util from '../util';
import {tile} from './array_ops';
import {getReductionAxes} from './broadcast_util';
import {op} from './operation';
import {scalar} from './tensor_ops';
import {rsqrt} from './unary_ops';
/**
* Batch normalization, strictly for 2D. For the more relaxed version, see
* `tf.batchNorm`.
*
* @param x The input Tensor.
* @param mean A mean Tensor.
* @param variance A variance Tensor.
* @param offset An offset Tensor.
* @param scale A scale Tensor.
* @param varianceEpsilon A small float number to avoid dividing by 0.
*/
function batchNorm2d_(
x: Tensor2D|TensorLike, mean: Tensor2D|Tensor1D|TensorLike,
variance: Tensor2D|Tensor1D|TensorLike,
offset?: Tensor2D|Tensor1D|TensorLike, scale?: Tensor2D|Tensor1D|TensorLike,
varianceEpsilon?: number): Tensor2D {
const $x = convertToTensor(x, 'x', 'batchNorm');
const $mean = convertToTensor(mean, 'mean', 'batchNorm');
const $variance = convertToTensor(variance, 'variance', 'batchNorm');
let $scale: Tensor2D|Tensor1D;
if (scale != null) {
$scale = convertToTensor(scale, 'scale', 'batchNorm');
}
let $offset: Tensor2D|Tensor1D;
if (offset != null) {
$offset = convertToTensor(offset, 'offset', 'batchNorm');
}
util.assert(
$x.rank === 2,
() => `Error in batchNorm3D: x must be rank 3 but got rank ` +
`${$x.rank}.`);
util.assert(
$mean.rank === 2 || $mean.rank === 1,
() => `Error in batchNorm2D: mean must be rank 2 or rank 1 but ` +
`got rank ${$mean.rank}.`);
util.assert(
$variance.rank === 2 || $variance.rank === 1,
() => `Error in batchNorm2D: variance must be rank 2 or rank 1 ` +
`but got rank ${$variance.rank}.`);
if ($scale != null) {
util.assert(
$scale.rank === 2 || $scale.rank === 1,
() => `Error in batchNorm2D: scale must be rank 2 or rank 1 ` +
`but got rank ${$scale.rank}.`);
}
if ($offset != null) {
util.assert(
$offset.rank === 2 || $offset.rank === 1,
() => `Error in batchNorm2D: offset must be rank 2 or rank 1 ` +
`but got rank ${$offset.rank}.`);
}
return batchNorm_($x, $mean, $variance, $offset, $scale, varianceEpsilon);
}
/**
* Batch normalization, strictly for 3D. For the more relaxed version, see
* `tf.batchNorm`.
*
* @param x The input Tensor.
* @param mean A mean Tensor.
* @param variance A variance Tensor.
* @param offset An offset Tensor.
* @param scale A scale Tensor.
* @param varianceEpsilon A small float number to avoid dividing by 0.
*/
function batchNorm3d_(
x: Tensor3D|TensorLike, mean: Tensor3D|Tensor1D|TensorLike,
variance: Tensor3D|Tensor1D|TensorLike,
offset?: Tensor3D|Tensor1D|TensorLike, scale?: Tensor3D|Tensor1D|TensorLike,
varianceEpsilon?: number): Tensor3D {
const $x = convertToTensor(x, 'x', 'batchNorm');
const $mean = convertToTensor(mean, 'mean', 'batchNorm');
const $variance = convertToTensor(variance, 'variance', 'batchNorm');
let $scale: Tensor3D|Tensor1D;
if (scale != null) {
$scale = convertToTensor(scale, 'scale', 'batchNorm');
}
let $offset: Tensor3D|Tensor1D;
if (offset != null) {
$offset = convertToTensor(offset, 'offset', 'batchNorm');
}
util.assert(
$x.rank === 3,
() => `Error in batchNorm3D: x must be rank 3 but got rank ` +
`${$x.rank}.`);
util.assert(
$mean.rank === 3 || $mean.rank === 1,
() => `Error in batchNorm3D: mean must be rank 3 or rank 1 but ` +
`got rank ${$mean.rank}.`);
util.assert(
$variance.rank === 3 || $variance.rank === 1,
() => `Error in batchNorm3D: variance must be rank 3 or rank 1 ` +
`but got rank ${$variance.rank}.`);
if ($scale != null) {
util.assert(
$scale.rank === 3 || $scale.rank === 1,
() => `Error in batchNorm3D: scale must be rank 3 or rank 1 ` +
`but got rank ${$scale.rank}.`);
}
if ($offset != null) {
util.assert(
$offset.rank === 3 || $offset.rank === 1,
() => `Error in batchNorm3D: offset must be rank 3 or rank 1 ` +
`but got rank ${$offset.rank}.`);
}
return batchNorm_($x, $mean, $variance, $offset, $scale, varianceEpsilon);
}
/**
* Batch normalization, strictly for 4D. For the more relaxed version, see
* `tf.batchNorm`.
*
* @param x The input Tensor.
* @param mean A mean Tensor.
* @param variance A variance Tensor.
* @param offset An offset Tensor.
* @param scale A scale Tensor.
* @param varianceEpsilon A small float number to avoid dividing by 0.
*/
function batchNorm4d_(
x: Tensor4D|TensorLike, mean: Tensor4D|Tensor1D|TensorLike,
variance: Tensor4D|Tensor1D|TensorLike,
offset?: Tensor4D|Tensor1D|TensorLike, scale?: Tensor4D|Tensor1D|TensorLike,
varianceEpsilon?: number): Tensor4D {
const $x = convertToTensor(x, 'x', 'batchNorm');
const $mean = convertToTensor(mean, 'mean', 'batchNorm');
const $variance = convertToTensor(variance, 'variance', 'batchNorm');
let $scale: Tensor4D|Tensor1D;
if (scale != null) {
$scale = convertToTensor(scale, 'scale', 'batchNorm');
}
let $offset: Tensor4D|Tensor1D;
if (offset != null) {
$offset = convertToTensor(offset, 'offset', 'batchNorm');
}
util.assert(
$x.rank === 4,
() => `Error in batchNorm4D: x must be rank 4 but got rank ` +
`${$x.rank}.`);
util.assert(
$mean.rank === 4 || $mean.rank === 1,
() => `Error in batchNorm4D: mean must be rank 4 or rank 1 but ` +
`got rank ${$mean.rank}.`);
util.assert(
$variance.rank === 4 || $variance.rank === 1,
() => `Error in batchNorm4D: variance must be rank 4 or rank 1 ` +
`but got rank ${$variance.rank}.`);
if ($scale != null) {
util.assert(
$scale.rank === 4 || $scale.rank === 1,
() => `Error in batchNorm4D: scale must be rank 4 or rank 1 ` +
`but got rank ${$scale.rank}.`);
}
if ($offset != null) {
util.assert(
$offset.rank === 4 || $offset.rank === 1,
() => `Error in batchNorm4D: offset must be rank 4 or rank 1 ` +
`but got rank ${$offset.rank}.`);
}
return batchNorm_($x, $mean, $variance, $offset, $scale, varianceEpsilon);
}
/**
* @deprecated Please use `tf.batchNorm` instead and note the positional
* argument change of scale, offset, and varianceEpsilon.
*/
function batchNormalization_<R extends Rank>(
x: Tensor<R>|Tensor1D|TensorLike, mean: Tensor<R>|Tensor1D|TensorLike,
variance: Tensor<R>|Tensor1D|TensorLike, varianceEpsilon = .001,
scale?: Tensor<R>|Tensor1D|TensorLike,
offset?: Tensor<R>|Tensor1D|TensorLike): Tensor<R> {
warnDeprecation();
return batchNorm_(x, mean, variance, offset, scale, varianceEpsilon);
}
/**
* Batch normalization.
*
* As described in
* [http://arxiv.org/abs/1502.03167](http://arxiv.org/abs/1502.03167).
*
* Mean, variance, scale, and offset can be of two shapes:
* - The same shape as the input.
* - In the common case, the depth dimension is the last dimension of x, so
* the values would be an `tf.Tensor1D` of shape [depth].
*
* Also available are stricter rank-specific methods with the same signature
* as this method that assert that parameters passed are of given rank
* - `tf.batchNorm2d`
* - `tf.batchNorm3d`
* - `tf.batchNorm4d`
*
* @param x The input Tensor.
* @param mean A mean Tensor.
* @param variance A variance Tensor.
* @param offset An offset Tensor.
* @param scale A scale Tensor.
* @param varianceEpsilon A small float number to avoid dividing by 0.
*/
/** @doc {heading: 'Operations', subheading: 'Normalization'} */
function batchNorm_<R extends Rank>(
x: Tensor<R>|Tensor1D|TensorLike, mean: Tensor<R>|Tensor1D|TensorLike,
variance: Tensor<R>|Tensor1D|TensorLike,
offset?: Tensor<R>|Tensor1D|TensorLike,
scale?: Tensor<R>|Tensor1D|TensorLike,
varianceEpsilon?: number): Tensor<R> {
if (varianceEpsilon == null) {
varianceEpsilon = 0.001;
}
const $x = convertToTensor(x, 'x', 'batchNorm');
const $mean = convertToTensor(mean, 'mean', 'batchNorm');
const $variance = convertToTensor(variance, 'variance', 'batchNorm');
let $scale: Tensor<R>|Tensor1D;
if (scale != null) {
$scale = convertToTensor(scale, 'scale', 'batchNorm');
}
let $offset: Tensor<R>|Tensor1D;
if (offset != null) {
$offset = convertToTensor(offset, 'offset', 'batchNorm');
}
util.assert(
$mean.rank === $variance.rank,
() => 'Batch normalization gradient requires mean and variance to have ' +
'equal ranks.');
util.assert(
$offset == null || $mean.rank === $offset.rank,
() => 'Batch normalization gradient requires mean and offset to have ' +
'equal ranks.');
util.assert(
$scale == null || $mean.rank === $scale.rank,
() => 'Batch normalization gradient requires mean and scale to have ' +
'equal ranks.');
let x4D: Tensor4D;
if ($x.rank === 0 || $x.rank === 1) {
x4D = $x.as4D(1, 1, 1, $x.size);
} else if ($x.rank === 2) {
x4D = $x.as4D(1, 1, $x.shape[0], $x.shape[1]);
} else if ($x.rank === 3) {
x4D = $x.as4D(1, $x.shape[0], $x.shape[1], $x.shape[2]) as Tensor4D;
} else {
x4D = $x as Tensor4D;
}
const der = (dy: Tensor) => {
const scaleValue = $scale == null ? scalar(1) : $scale;
const reductionAxes = getReductionAxes($mean.shape, x4D.shape);
const tileShape: number[] = [];
if ($mean.rank === 1) {
for (let i = 0; i < x4D.shape.length - 1; ++i) {
tileShape.push(x4D.shape[i]);
}
tileShape.push(1);
}
const xMinusMean = $x.sub($mean);
const dyTimesScaleValue = dy.mul(scaleValue);
const oneOverSqrtVariance = rsqrt($variance.add(scalar(varianceEpsilon)));
const minusHalfRCube = oneOverSqrtVariance.mul(oneOverSqrtVariance)
.mul(oneOverSqrtVariance)
.mul(scalar(-0.5));
const derX = () => {
if ($mean.rank === 1) {
return dy
.mul(tile(
oneOverSqrtVariance.as4D(1, 1, 1, $mean.shape[0]), tileShape))
.mul(scaleValue)
.reshape($x.shape);
} else {
return dy.mul(oneOverSqrtVariance).mul(scaleValue).reshape($x.shape);
}
};
const derMean = () => {
let meanDer = oneOverSqrtVariance.mul(scalar(-1)).mul(dyTimesScaleValue);
if ($mean.rank === 1) {
meanDer = meanDer.sum(reductionAxes);
}
return meanDer.reshape($mean.shape);
};
const derVariance = () => {
let varianceDer = minusHalfRCube.mul(xMinusMean).mul(dyTimesScaleValue);
if ($mean.rank === 1) {
varianceDer = varianceDer.sum(reductionAxes);
}
return varianceDer.reshape($mean.shape);
};
const derScale = () => {
const xMinusMean2TimesRsqrt = xMinusMean.mul(oneOverSqrtVariance);
let scaleDer = dy.mul(xMinusMean2TimesRsqrt);
if ($mean.rank === 1) {
scaleDer = scaleDer.sum(reductionAxes);
}
return scaleDer.reshape($mean.shape);
};
const derOffset = () => {
let offsetDer = dy;
if ($mean.rank === 1) {
offsetDer = offsetDer.sum(reductionAxes);
}
return offsetDer.reshape($mean.shape);
};
return {
$x: derX,
$mean: derMean,
$variance: derVariance,
$scale: derScale,
$offset: derOffset
};
};
const res = ENV.engine.runKernel(
backend => backend.batchNormalization(
x4D, batchnormReshape4D($mean), batchnormReshape4D($variance),
varianceEpsilon, batchnormReshape4D($scale),
batchnormReshape4D($offset)),
{$x, $mean, $variance, $scale, $offset}, der);
return res.reshape($x.shape);
}
function batchnormReshape4D(x: Tensor): Tensor4D|Tensor1D {
if (x == null) {
return null;
}
if (x.rank === 0) {
return x.as1D();
} else if (x.rank === 1) {
return x as Tensor1D;
} else if (x.rank === 2) {
return x.as4D(1, 1, x.shape[0], x.shape[1]);
} else if (x.rank === 3) {
return x.as4D(1, x.shape[0], x.shape[1], x.shape[2]);
}
return x as Tensor4D;
}
/**
* @deprecated Please use `tf.batchNorm2d` instead and note the positional
* argument change of scale, offset, and varianceEpsilon.
*/
function batchNormalization2d_(
x: Tensor2D|TensorLike, mean: Tensor2D|Tensor1D|TensorLike,
variance: Tensor2D|Tensor1D|TensorLike, varianceEpsilon = .001,
scale?: Tensor2D|Tensor1D|TensorLike,
offset?: Tensor2D|Tensor1D|TensorLike): Tensor2D {
warnDeprecation();
return batchNorm2d_(x, mean, variance, offset, scale, varianceEpsilon);
}
/**
* @deprecated Please use `tf.batchNorm3d` instead and note the positional
* argument change of scale, offset, and varianceEpsilon.
*/
function batchNormalization3d_(
x: Tensor3D|TensorLike, mean: Tensor3D|Tensor1D|TensorLike,
variance: Tensor3D|Tensor1D|TensorLike, varianceEpsilon = .001,
scale?: Tensor3D|Tensor1D|TensorLike,
offset?: Tensor3D|Tensor1D|TensorLike): Tensor3D {
warnDeprecation();
return batchNorm3d_(x, mean, variance, offset, scale, varianceEpsilon);
}
/**
* @deprecated Please use `tf.batchNorm4d` instead and note the positional
* argument change of scale, offset, and varianceEpsilon.
*/
function batchNormalization4d_(
x: Tensor4D|TensorLike, mean: Tensor4D|Tensor1D|TensorLike,
variance: Tensor4D|Tensor1D|TensorLike, varianceEpsilon = .001,
scale?: Tensor4D|Tensor1D|TensorLike,
offset?: Tensor4D|Tensor1D|TensorLike): Tensor4D {
warnDeprecation();
return batchNorm4d_(x, mean, variance, offset, scale, varianceEpsilon);
}
function warnDeprecation() {
deprecationWarn(
'tf.batchNormalization() is going away. ' +
'Use tf.batchNorm() instead, and note the positional argument change ' +
'of scale, offset, and varianceEpsilon');
}
export const batchNormalization2d = op({batchNormalization2d_});
export const batchNormalization3d = op({batchNormalization3d_});
export const batchNormalization4d = op({batchNormalization4d_});
export const batchNormalization = op({batchNormalization_});
export const batchNorm = op({batchNorm_});
export const batchNorm2d = op({batchNorm2d_});
export const batchNorm3d = op({batchNorm3d_});
export const batchNorm4d = op({batchNorm4d_});