-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
batchnorm4d.ts
79 lines (76 loc) · 2.95 KB
/
batchnorm4d.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
/**
* @license
* Copyright 2020 Google LLC. 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 {Tensor1D, Tensor4D} from '../tensor';
import {convertToTensor} from '../tensor_util_env';
import {TensorLike} from '../types';
import * as util from '../util';
import {batchNorm} from './batchnorm';
import {op} from './operation';
/**
* 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);
}
export const batchNorm4d = /* @__PURE__ */ op({batchNorm4d_});