UNPKG

5.98 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 * as tf from '../index';
18import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
19import { expectValuesInRange } from '../test_util';
20describeWithFlags('randomUniform', ALL_ENVS, () => {
21 it('should return a random 1D float32 array', async () => {
22 const shape = [10];
23 // Ensure defaults to float32 w/o type:
24 let result = tf.randomUniform(shape, 0, 2.5);
25 expect(result.dtype).toBe('float32');
26 expectValuesInRange(await result.data(), 0, 2.5);
27 result = tf.randomUniform(shape, 0, 1.5, 'float32');
28 expect(result.dtype).toBe('float32');
29 expectValuesInRange(await result.data(), 0, 1.5);
30 });
31 it('should return a random 1D int32 array', async () => {
32 const shape = [10];
33 const result = tf.randomUniform(shape, 0, 2, 'int32');
34 expect(result.dtype).toBe('int32');
35 expectValuesInRange(await result.data(), 0, 2);
36 });
37 it('should return a random 1D bool array', async () => {
38 const shape = [10];
39 const result = tf.randomUniform(shape, 0, 1, 'bool');
40 expect(result.dtype).toBe('bool');
41 expectValuesInRange(await result.data(), 0, 1);
42 });
43 it('should return a random 2D float32 array', async () => {
44 const shape = [3, 4];
45 // Ensure defaults to float32 w/o type:
46 let result = tf.randomUniform(shape, 0, 2.5);
47 expect(result.dtype).toBe('float32');
48 expectValuesInRange(await result.data(), 0, 2.5);
49 result = tf.randomUniform(shape, 0, 1.5, 'float32');
50 expect(result.dtype).toBe('float32');
51 expectValuesInRange(await result.data(), 0, 1.5);
52 });
53 it('should return a random 2D int32 array', async () => {
54 const shape = [3, 4];
55 const result = tf.randomUniform(shape, 0, 2, 'int32');
56 expect(result.dtype).toBe('int32');
57 expectValuesInRange(await result.data(), 0, 2);
58 });
59 it('should return a random 2D bool array', async () => {
60 const shape = [3, 4];
61 const result = tf.randomUniform(shape, 0, 1, 'bool');
62 expect(result.dtype).toBe('bool');
63 expectValuesInRange(await result.data(), 0, 1);
64 });
65 it('should return a random 3D float32 array', async () => {
66 const shape = [3, 4, 5];
67 // Ensure defaults to float32 w/o type:
68 let result = tf.randomUniform(shape, 0, 2.5);
69 expect(result.dtype).toBe('float32');
70 expectValuesInRange(await result.data(), 0, 2.5);
71 result = tf.randomUniform(shape, 0, 1.5, 'float32');
72 expect(result.dtype).toBe('float32');
73 expectValuesInRange(await result.data(), 0, 1.5);
74 });
75 it('should return a random 3D int32 array', async () => {
76 const shape = [3, 4, 5];
77 const result = tf.randomUniform(shape, 0, 2, 'int32');
78 expect(result.dtype).toBe('int32');
79 expectValuesInRange(await result.data(), 0, 2);
80 });
81 it('should return a random 3D bool array', async () => {
82 const shape = [3, 4, 5];
83 const result = tf.randomUniform(shape, 0, 1, 'bool');
84 expect(result.dtype).toBe('bool');
85 expectValuesInRange(await result.data(), 0, 1);
86 });
87 it('should return a random 4D float32 array', async () => {
88 const shape = [3, 4, 5, 6];
89 // Ensure defaults to float32 w/o type:
90 let result = tf.randomUniform(shape, 0, 2.5);
91 expect(result.dtype).toBe('float32');
92 expectValuesInRange(await result.data(), 0, 2.5);
93 result = tf.randomUniform(shape, 0, 1.5, 'float32');
94 expect(result.dtype).toBe('float32');
95 expectValuesInRange(await result.data(), 0, 1.5);
96 });
97 it('should return a random 4D int32 array', async () => {
98 const shape = [3, 4, 5, 6];
99 const result = tf.randomUniform(shape, 0, 2, 'int32');
100 expect(result.dtype).toBe('int32');
101 expectValuesInRange(await result.data(), 0, 2);
102 });
103 it('should return a random 4D bool array', async () => {
104 const shape = [3, 4, 5, 6];
105 const result = tf.randomUniform(shape, 0, 1, 'bool');
106 expect(result.dtype).toBe('bool');
107 expectValuesInRange(await result.data(), 0, 1);
108 });
109 it('should return a random 5D float32 array', async () => {
110 const shape = [2, 3, 4, 5, 6];
111 // Ensure defaults to float32 w/o type:
112 let result = tf.randomUniform(shape, 0, 2.5);
113 expect(result.dtype).toBe('float32');
114 expectValuesInRange(await result.data(), 0, 2.5);
115 result = tf.randomUniform(shape, 0, 1.5, 'float32');
116 expect(result.dtype).toBe('float32');
117 expectValuesInRange(await result.data(), 0, 1.5);
118 });
119 it('should return a random 5D int32 array', async () => {
120 const shape = [2, 3, 4, 5, 6];
121 const result = tf.randomUniform(shape, 0, 2, 'int32');
122 expect(result.dtype).toBe('int32');
123 expectValuesInRange(await result.data(), 0, 2);
124 });
125 it('should return a random 5D bool array', async () => {
126 const shape = [2, 3, 4, 5, 6];
127 const result = tf.randomUniform(shape, 0, 1, 'bool');
128 expect(result.dtype).toBe('bool');
129 expectValuesInRange(await result.data(), 0, 1);
130 });
131});
132//# sourceMappingURL=random_uniform_test.js.map
\No newline at end of file