-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4156 Co-authored-by: Matthew Soulanille <msoulanille@google.com>
- Loading branch information
1 parent
2d5755c
commit 9206578
Showing
7 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 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 {Tensor} from '../tensor'; | ||
import {DataType, Rank, ShapeMap} from '../types'; | ||
|
||
import {op} from './operation'; | ||
import {randomNormal} from './random_normal'; | ||
|
||
/** | ||
* Creates a `tf.Tensor` with values sampled from a normal distribution. | ||
* | ||
* The generated values will have mean 0 and standard deviation 1. | ||
* | ||
* ```js | ||
* tf.randomStandardNormal([2, 2]).print(); | ||
* ``` | ||
* | ||
* @param shape An array of integers defining the output tensor shape. | ||
* @param dtype The data type of the output. | ||
* @param seed The seed for the random number generator. | ||
* | ||
* @doc {heading: 'Tensors', subheading: 'Random'} | ||
*/ | ||
function randomStandardNormal_<R extends Rank>( | ||
shape: ShapeMap[R], dtype?: 'float32'|'int32', seed?: number): Tensor<R> { | ||
if (dtype != null && (dtype as DataType) === 'bool') { | ||
throw new Error(`Unsupported data type ${dtype}`); | ||
} | ||
return randomNormal(shape, 0, 1, dtype, seed); | ||
} | ||
|
||
export const randomStandardNormal = op({randomStandardNormal_}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
|
||
/** | ||
* @license | ||
* Copyright 2022 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 * as tf from '../index'; | ||
import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; | ||
|
||
import {expectArrayInMeanStdRange, jarqueBeraNormalityTest} from './rand_util'; | ||
|
||
describeWithFlags('randomStandardNormal', ALL_ENVS, () => { | ||
const SEED = 42; | ||
const EPSILON = 0.05; | ||
|
||
it('should return a float32 1D of random standard normal values', | ||
async () => { | ||
const SAMPLES = 10000; | ||
|
||
// Ensure defaults to float32. | ||
let result = tf.randomStandardNormal([SAMPLES], null, SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual([SAMPLES]); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
|
||
result = tf.randomStandardNormal([SAMPLES], 'float32', SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual([SAMPLES]); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a int32 1D of random standard normal values', async () => { | ||
const SAMPLES = 10000; | ||
const result = tf.randomStandardNormal([SAMPLES], 'int32', SEED); | ||
expect(result.dtype).toBe('int32'); | ||
expect(result.shape).toEqual([SAMPLES]); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a float32 2D of random standard normal values', | ||
async () => { | ||
const SAMPLES = 100; | ||
|
||
// Ensure defaults to float32. | ||
let result = tf.randomStandardNormal([SAMPLES, SAMPLES], null, SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual([SAMPLES, SAMPLES]); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
|
||
result = tf.randomStandardNormal([SAMPLES, SAMPLES], 'float32', SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual([SAMPLES, SAMPLES]); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a int32 2D of random standard normal values', async () => { | ||
const SAMPLES = 100; | ||
const result = tf.randomStandardNormal([SAMPLES, SAMPLES], 'int32', SEED); | ||
expect(result.dtype).toBe('int32'); | ||
expect(result.shape).toEqual([SAMPLES, SAMPLES]); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a float32 3D of random standard normal values', | ||
async () => { | ||
const SAMPLES_SHAPE = [20, 20, 20]; | ||
|
||
// Ensure defaults to float32. | ||
let result = tf.randomStandardNormal(SAMPLES_SHAPE, null, SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
|
||
result = tf.randomStandardNormal(SAMPLES_SHAPE, 'float32', SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a int32 3D of random standard normal values', async () => { | ||
const SAMPLES_SHAPE = [20, 20, 20]; | ||
const result = tf.randomStandardNormal(SAMPLES_SHAPE, 'int32', SEED); | ||
expect(result.dtype).toBe('int32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a float32 4D of random standard normal values', | ||
async () => { | ||
const SAMPLES_SHAPE = [10, 10, 10, 10]; | ||
|
||
// Ensure defaults to float32. | ||
let result = tf.randomStandardNormal(SAMPLES_SHAPE, null, SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
|
||
result = tf.randomStandardNormal(SAMPLES_SHAPE, 'float32', SEED); | ||
expect(result.dtype).toBe('float32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a int32 4D of random standard normal values', async () => { | ||
const SAMPLES_SHAPE = [10, 10, 10, 10]; | ||
|
||
const result = tf.randomStandardNormal(SAMPLES_SHAPE, 'int32', SEED); | ||
expect(result.dtype).toBe('int32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
|
||
it('should return a int32 5D of random standard normal values', async () => { | ||
const SAMPLES_SHAPE = [10, 10, 10, 10, 10]; | ||
|
||
const result = tf.randomStandardNormal(SAMPLES_SHAPE, 'int32', SEED); | ||
expect(result.dtype).toBe('int32'); | ||
expect(result.shape).toEqual(SAMPLES_SHAPE); | ||
jarqueBeraNormalityTest(await result.data()); | ||
expectArrayInMeanStdRange(await result.data(), 0, 1, EPSILON); | ||
}); | ||
}); |