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

Changed all MLContext creation to include a fixed seed #4736

Merged
merged 2 commits into from
Jan 31, 2020
Merged

Changed all MLContext creation to include a fixed seed #4736

merged 2 commits into from
Jan 31, 2020

Conversation

harishsk
Copy link
Contributor

Many tests are creating MLContext without a seed. Since we are seeing a number of random failures, this PR attempts to standardize all MLContext creation to include a fixed seed.

Note to reviewers: If you know if cases where a fixed seed is not desired, please flag those cases.

@harishsk harishsk requested review from a team as code owners January 29, 2020 21:22
Copy link
Contributor

@justinormont justinormont left a comment

Choose a reason for hiding this comment

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

LGTM.

We can likely tighten up the expected ranges on the unit test since the outputs will (hopefully) be more constrained now. It feels odd that I could change these seed values and no unit test would fail. We can tighten the range but not shrink to zero as deterministic results aren't expected since multi-threading causes a race condition for random number generation. I think/hope this will cause the TrainTest/CV splits to be deterministic though.

You could go a step further and feed a fixed seed to each component which takes a seed, as if I recall, they won't listen to the MLContext seed (alternatively correct this).

Certain components can be made (more/fully?) deterministic, like SDCA & LogisticRegression, by setting the number of threads to one.

Btw, 1 is a fine choice; though I prefer 42 (there's no consistency in default seed values for the repo).

@@ -169,7 +169,7 @@ public void SpikeDetection()
[Fact]
public void SsaSpikeDetection()
{
var env = new MLContext();
var env = new MLContext(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussion topic, does any know of a reason to not have a fixed seed for unit tests?

@najeeb-kazmi
Copy link
Member

                new SdcaMaximumEntropyMulticlassTrainer.Options { MaximumNumberOfIterations = 100, Shuffle = true, NumberOfThreads = 1, }));

Shuffle = false for SDCA will give more/full determinism as Justin suggests


Refers to: test/Microsoft.ML.Tests/Scenarios/Api/Estimators/PredictAndMetadata.cs:34 in 4492586. [](commit_id = 4492586, deletion_comment = False)

@najeeb-kazmi
Copy link
Member

najeeb-kazmi commented Jan 30, 2020

                new SdcaMaximumEntropyMulticlassTrainer.Options { MaximumNumberOfIterations = 100, Shuffle = true, NumberOfThreads = 1 }))

Same here, and everywhere else SDCA shows up in tests.


Refers to: test/Microsoft.ML.Tests/Scenarios/Api/Estimators/Extensibility.cs:45 in 4492586. [](commit_id = 4492586, deletion_comment = False)

Copy link
Member

@najeeb-kazmi najeeb-kazmi left a comment

Choose a reason for hiding this comment

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

We also have this, which is used to run a lot of tests in TestPredictors.cs

protected void RunAllTests(
IList<PredictorAndArgs> predictors, IList<TestDataset> datasets,
string[] extraSettings = null, string extraTag = "", bool summary = false,
int digitsOfPrecision = DigitsOfPrecision, NumberParseOption parseOption = NumberParseOption.Default)

extraSettings is used to add things to the maml command line that is executed. Passing "seed=1" here would also set the seed for these.

In fact, there are several other tests that call directly one of RunOneAllTests, Run_TrainTest, or Run_CV, all of which can be passed extraSettings.

@harishsk
Copy link
Contributor Author

Would you recommend changing those to false as well in that case?

@justinormont
Copy link
Contributor

Would you recommend changing those to false as well in that case?

I'm unsure if disabling shuffling with cause the unit tests to be more stable. The shuffling and TrainTest/CV splits (hopefully) listen to seed, and would then be deterministic. Can verify by inspecting the one of the TrainTest splits, or repeatedly dumping the iDataView to a file (expecting with a fixed seed they stable perfectly the same, and changing the seed gets you a different set and ordering of rows).

@harishsk
Copy link
Contributor Author

How about we do this in two passes then? Can we merge these changes in first and then do the shuffle=false in a second pass?

@frank-dong-ms-zz
Copy link
Contributor

Do you plan to do some test whether this will improve stability?

Copy link
Contributor

@frank-dong-ms-zz frank-dong-ms-zz left a comment

Choose a reason for hiding this comment

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

:shipit:

@najeeb-kazmi
Copy link
Member

How about we do this in two passes then? Can we merge these changes in first and then do the shuffle=false in a second pass?

Sounds good to me. Should we also add "seed=1" to the predictor tests in another PR or do you want to take care of this here?

@harishsk
Copy link
Contributor Author

Do you plan to do some test whether this will improve stability?

By all consensus, seeded unit tests are the right thing to do. The tests passed here on first attempt. So I don't think this has made it any worse. I plan to run full tests against these also.

@harishsk harishsk merged commit 24c8274 into dotnet:master Jan 31, 2020
@harishsk harishsk deleted the fixedSeed branch April 21, 2020 23:58
@ghost ghost locked as resolved and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants