Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseRandomLayer Abstract Layer and RandomWidth Preprocessing Layer #7345

Merged
merged 24 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d676ae8
created baseRandomLayer Abstract layer, contains random generator fun…
RWallie Feb 6, 2023
c6a144c
renamed file to base_random_layer
RWallie Feb 6, 2023
a9fd524
created random_width preprocessing layer - extends baseRandomLayer an…
RWallie Feb 6, 2023
253c0e7
tests for randomWidth preproccessing layer
RWallie Feb 6, 2023
3159cc9
added randomWidth preprocessing layer to exports_layers
RWallie Feb 6, 2023
97e9f66
reformatted tests using reshape property
Silvia42 Feb 9, 2023
17ff308
updated BaseRandomLayer to contain an instance of RandomGenerator cla…
Silvia42 Feb 9, 2023
0d7ff04
random generator handles pseudorandomness for autoincrementing seed a…
Silvia42 Feb 9, 2023
9188041
updated randomUniform call with RangomGenerator instance on BaseRando…
Silvia42 Feb 9, 2023
639712f
increment currentSeed before return in random_generator
RWallie Feb 9, 2023
3086655
fixed linting
RWallie Feb 9, 2023
9e79b22
removed reset method from RandomGenerator
RWallie Feb 9, 2023
f9bf89a
unpacked args and chaned conditionals
RWallie Feb 9, 2023
de0a7c6
merged master
Silvia42 Feb 9, 2023
de296dc
fix Safari compatibility
Silvia42 Feb 10, 2023
b1034a5
added test for RandomGenerator Class
Silvia42 Feb 10, 2023
5077b96
Merge branch 'master' into randomLayers
Silvia42 Feb 16, 2023
aba23b7
Change class RandomGenerator to class RandomSeed
Silvia42 Feb 16, 2023
384b758
Merge branch 'master' into randomLayers
Silvia42 Feb 23, 2023
f4b05b2
updated RandomSeed to handle undefined value, removed duplicate state…
Silvia42 Feb 23, 2023
9ae69a4
re-labeled currentSeed as seed
Silvia42 Feb 23, 2023
1d7fc23
Merge branch 'master' into randomLayers
mattsoulanille Mar 14, 2023
b183550
Merge branch 'master' into randomLayers
mattsoulanille Mar 14, 2023
c77e1c3
Merge branch 'master' into randomLayers
mattsoulanille Mar 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions tfjs-layers/src/backend/random_seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

export class RandomSeed {
static className = 'RandomSeed';
currentSeed: number;
constructor(readonly seed: number) {
this.currentSeed = seed;
seed: number | undefined;
constructor(seed: number | undefined) {
this.seed = seed;
}
next() {
++this.currentSeed;
next(): number | undefined {
if (this.seed === undefined) {
return undefined;
}
return this.seed++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to other reviewer: When no seed is specified, the keras implementation does not choose a random seed. It just generates random values. Saving it yields something like this, with the seed set to Null:

{'name': 'random_width_3', 'trainable': True, 'dtype': 'float32', 'factor': (-0.2, 0.3), 'interpolation': 'bilinear', 'seed': None}

This is truly random, and there is no reproducibility here.

On the other hand, when the user sets the seed, it gets saved to the config.

{'name': 'random_width_2', 'trainable': True, 'dtype': 'float32', 'factor': (-0.2, 0.3), 'interpolation': 'bilinear', 'seed': 1}

}
}
14 changes: 10 additions & 4 deletions tfjs-layers/src/backend/random_seed_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
import {RandomSeed} from './random_seed';
import {describeMathCPUAndGPU} from '../utils/test_utils';

const randomSeed = new RandomSeed(42);

describeMathCPUAndGPU('RandomSeed', () => {
it('Checking if RandomSeed class handles pseudo randomness.', () => {
const firstSeed = randomSeed.currentSeed;
const randomSeed = new RandomSeed(42);
const firstSeed = randomSeed.seed;
randomSeed.next();
const secondSeed = randomSeed.currentSeed;
const secondSeed = randomSeed.seed;
expect(firstSeed).not.toEqual(secondSeed);
});
it('Checking if RandomSeed class handles undefined seed.', () => {
const randomSeed = new RandomSeed(undefined);
const firstSeed = randomSeed.seed;
const secondSeed = randomSeed.next();
expect(firstSeed).toEqual(undefined);
expect(secondSeed).toEqual(undefined);
});
});
6 changes: 2 additions & 4 deletions tfjs-layers/src/engine/base_random_layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ export abstract class BaseRandomLayer extends Layer {
/** @nocollapse */
static className = 'BaseRandomLayer';
protected randomGenerator: RandomSeed;
private seed?: number;

constructor(args: BaseRandomLayerArgs) {
super(args);
this.seed = args.seed;
this.randomGenerator = new RandomSeed(this.seed);
this.randomGenerator = new RandomSeed(args.seed);
}

override getConfig(): serialization.ConfigDict {
const config: serialization.ConfigDict = {
'seed': this.seed
'seed': this.randomGenerator.seed
};

const baseConfig = super.getConfig();
Expand Down
4 changes: 1 addition & 3 deletions tfjs-layers/src/layers/preprocessing/random_width.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ export class RandomWidth extends BaseRandomLayer {

this.widthFactor = randomUniform([1],
(1.0 + this.widthLower), (1.0 + this.widthUpper),
'float32', this.randomGenerator.currentSeed
'float32', this.randomGenerator.next()
);

this.randomGenerator.next();

this.adjustedWidth = this.widthFactor.dataSync()[0] * imgWidth;
this.adjustedWidth = Math.round(this.adjustedWidth);

Expand Down