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

Add AllenNLP integration. #1086

Merged
merged 48 commits into from
Apr 21, 2020
Merged

Add AllenNLP integration. #1086

merged 48 commits into from
Apr 21, 2020

Conversation

himkt
Copy link
Member

@himkt himkt commented Apr 3, 2020

This PR introduces AllenNLP integration to run HPO with AllenNLP configuration files.

Example

def objective(trial):
    trial.suggest_uniform('LEARNING_RATE', 1e-2, 1e-1)
    trial.suggest_uniform('DROPOUT', 0.0, 0.5)
    trial.suggest_int("MAX_FILTER_SIZE", 3, 6)
    trial.suggest_int("NUM_FILTERS", 16, 128)
    trial.suggest_int("NUM_OUTPUT_LAYERS", 1, 3)
    trial.suggest_int("HIDDEN_SIZE", 16, 128)

    serialization_dir = "test"
    executor = AllenNLPExecutor(
        trial,
        "classifier.jsonnet",
        serialization_dir,
        use_poetry=True
    )
    return executor.run()


if __name__ == "__main__":
    study = optuna.create_study(direction="maximize")
    study.optimize(objective, n_trials=50)

#1078

@codecov-io
Copy link

codecov-io commented Apr 7, 2020

Codecov Report

Merging #1086 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1086   +/-   ##
=======================================
  Coverage   90.46%   90.46%           
=======================================
  Files         131      131           
  Lines       11331    11331           
=======================================
  Hits        10251    10251           
  Misses       1080     1080           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 928cefd...928cefd. Read the comment docs.

@himkt himkt marked this pull request as ready for review April 7, 2020 20:14
@himkt
Copy link
Member Author

himkt commented Apr 7, 2020

I think this PR is ready for review.
Could someone please take a look?

@crcrpar crcrpar self-requested a review April 8, 2020 02:23
examples/allennlp/allennlp_jsonnet.py Outdated Show resolved Hide resolved
examples/allennlp/classifier.jsonnet Outdated Show resolved Hide resolved
optuna/integration/allennlp.py Show resolved Hide resolved
optuna/integration/allennlp.py Outdated Show resolved Hide resolved
optuna/integration/allennlp.py Show resolved Hide resolved
optuna/integration/allennlp.py Show resolved Hide resolved
@crcrpar crcrpar added feature Change that does not break compatibility, but affects the public interfaces. optuna.integration Related to the `optuna.integration` submodule. This is automatically labeled by github-actions. labels Apr 9, 2020
@crcrpar crcrpar self-requested a review April 9, 2020 20:54
@toshihikoyanase toshihikoyanase self-assigned this Apr 10, 2020
examples/allennlp/allennlp_jsonnet.py Show resolved Hide resolved
examples/allennlp/allennlp_jsonnet.py Outdated Show resolved Hide resolved
optuna/integration/allennlp.py Outdated Show resolved Hide resolved
@himkt himkt changed the title Add AllenNLP cli integration Add AllenNLP integration Apr 12, 2020
@himkt himkt changed the title Add AllenNLP integration Add AllenNLP integration. Apr 12, 2020
Copy link
Contributor

@crcrpar crcrpar left a comment

Choose a reason for hiding this comment

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

Thanks for adopting the new Allennlp api!

Why does the format of test.jsonnet look different from that of example.jsonnet?

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

I have some small comments.

examples/allennlp/allennlp_jsonnet.py Outdated Show resolved Hide resolved
Comment on lines 25 to 26
"numpy_seed": 42,
"pytorch_seed": 42,
Copy link
Member

Choose a reason for hiding this comment

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

numpy_seed and pytorch_seed seem to be in the same nest-level, but they have different indent-levels.

Copy link
Member Author

@himkt himkt Apr 13, 2020

Choose a reason for hiding this comment

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

Thank you for pointing out, I run jsonnetfmt in d591f53.

"""Train a model using allennlp."""

for package_name in self._include_package:
allennlp.common.util.import_module_and_submodules(package_name)
Copy link
Member

Choose a reason for hiding this comment

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

I think any test cases do not visit this line. Please add test cases.

Copy link
Member Author

@himkt himkt Apr 13, 2020

Choose a reason for hiding this comment

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

I added tests in 6ac976b and found there is a breaking change in this feature.

import_module_and_submodule will be introduced in the next release allennlp,
which is the renamed method of import_modules in v0.9.0.

serialization_dir: str,
metrics: str = "best_validation_accuracy",
*,
include_package: List[str] = []
Copy link
Member

Choose a reason for hiding this comment

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

Please add a description of include_package to the docstring.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in 6ac976b.

@himkt himkt changed the title Add AllenNLP integration. [WIP] Add AllenNLP integration. Apr 13, 2020
@himkt
Copy link
Member Author

himkt commented Apr 13, 2020

@crcrpar Thank you for patient reviews!
I reformatted jsonnet files by jsonnetfmt. (d591f53)

@himkt himkt changed the title [WIP] Add AllenNLP integration. Add AllenNLP integration. Apr 13, 2020
@himkt
Copy link
Member Author

himkt commented Apr 13, 2020

27e4d49

AllenNLPExecutor now parses jsonnet itself and calls train_model instead of train_model_from_file.
In this change, an executor neither calls allennlp cli nor depends on environment variables,
which means that an executor can run in multiple threads/processes.

Copy link
Member Author

@himkt himkt left a comment

Choose a reason for hiding this comment

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

0e14a39

Comments for parsing jsonnet.
(FYI. @crcrpar @toshihikoyanase )

TrackerCallback = object


@experimental("1.4.0")
Copy link
Member Author

Choose a reason for hiding this comment

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

I made AllenNLPExecutor experimental since the major release of AllenNLP will come soon.
There are a lot of updates in their usages and we could face the necessity of changing the interface.
For more information, please see https://github.com/allenai/allennlp/releases/tag/v1.0-prerelease.

@himkt
Copy link
Member Author

himkt commented Apr 15, 2020

@toshihikoyanase
Thank you for your reviewing, I revised my PR including some modification and the changing support level of AllenNLPExecutor (normal -> experimental), please take a look again.

I also would like to express my gratitude to @crcrpar for patient helps.

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

Thank you for your update. It basically looks good to me.
I added small comments about docstring. Please check them.

optuna/integration/allennlp.py Outdated Show resolved Hide resolved

This feature is experimental since AllenNLP major release will come soon.
The interface may change without prior notice to correspond to the update.

Copy link
Member

Choose a reason for hiding this comment

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

How about adding the link to the example as can be seen in TensorFlow's integration?

See the example if you want to add a pruning hook to TensorFlow’s estimator.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the suggestion.
Added in 1533f6a.

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for your great contribution!

@toshihikoyanase toshihikoyanase added this to the v1.4.0 milestone Apr 21, 2020
@toshihikoyanase toshihikoyanase merged commit 4f1b4e3 into optuna:master Apr 21, 2020
@himkt himkt deleted the feature/allennlp-cli-example branch April 21, 2020 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Change that does not break compatibility, but affects the public interfaces. optuna.integration Related to the `optuna.integration` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants