UNPKG

1.83 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 { buffer } from './buffer';
18import { op } from './operation';
19import { MPRandGauss } from './rand_util';
20/**
21 * Creates a `tf.Tensor` with values sampled from a normal distribution.
22 *
23 * ```js
24 * tf.randomNormal([2, 2]).print();
25 * ```
26 *
27 * @param shape An array of integers defining the output tensor shape.
28 * @param mean The mean of the normal distribution.
29 * @param stdDev The standard deviation of the normal distribution.
30 * @param dtype The data type of the output.
31 * @param seed The seed for the random number generator.
32 *
33 * @doc {heading: 'Tensors', subheading: 'Random'}
34 */
35function randomNormal_(shape, mean = 0, stdDev = 1, dtype, seed) {
36 if (dtype != null && dtype === 'bool') {
37 throw new Error(`Unsupported data type ${dtype}`);
38 }
39 const randGauss = new MPRandGauss(mean, stdDev, dtype, false /* truncated */, seed);
40 const res = buffer(shape, dtype);
41 for (let i = 0; i < res.values.length; i++) {
42 res.values[i] = randGauss.nextValue();
43 }
44 return res.toTensor();
45}
46export const randomNormal = op({ randomNormal_ });
47//# sourceMappingURL=random_normal.js.map
\No newline at end of file