UNPKG

2.64 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import { ENGINE } from '../engine';
18import { LRN } from '../kernel_names';
19import { convertToTensor } from '../tensor_util_env';
20import * as util from '../util';
21import { op } from './operation';
22import { reshape } from './reshape';
23/**
24 * Normalizes the activation of a local neighborhood across or within
25 * channels.
26 *
27 * @param x The input tensor. The 4-D input tensor is treated as a 3-D array
28 * of 1D vectors (along the last dimension), and each vector is
29 * normalized independently.
30 * @param depthRadius The number of adjacent channels in the 1D normalization
31 * window.
32 * @param bias A constant bias term for the basis.
33 * @param alpha A scale factor, usually positive.
34 * @param beta An exponent.
35 *
36 * @doc {heading: 'Operations', subheading: 'Normalization'}
37 */
38function localResponseNormalization_(x, depthRadius = 5, bias = 1, alpha = 1, beta = 0.5) {
39 const $x = convertToTensor(x, 'x', 'localResponseNormalization');
40 util.assert($x.rank === 4 || $x.rank === 3, () => `Error in localResponseNormalization: x must be rank 3 or 4 but got
41 rank ${$x.rank}.`);
42 util.assert(util.isInt(depthRadius), () => `Error in localResponseNormalization: depthRadius must be an ` +
43 `integer but got depthRadius ${depthRadius}.`);
44 let x4D = $x;
45 let reshapedTo4D = false;
46 if ($x.rank === 3) {
47 reshapedTo4D = true;
48 x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
49 }
50 const inputs = { x: x4D };
51 const attrs = { depthRadius, bias, alpha, beta };
52 // tslint:disable-next-line: no-unnecessary-type-assertion
53 const res = ENGINE.runKernel(LRN, inputs, attrs);
54 if (reshapedTo4D) {
55 return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
56 }
57 else {
58 return res;
59 }
60}
61export const localResponseNormalization = op({ localResponseNormalization_ });
62//# sourceMappingURL=local_response_normalization.js.map
\No newline at end of file