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 example. #949

Merged
merged 30 commits into from Mar 18, 2020
Merged

Add allennlp example. #949

merged 30 commits into from Mar 18, 2020

Conversation

himkt
Copy link
Member

@himkt himkt commented Feb 21, 2020

This PR adds an example using allennlp for defining and training a model.
(edited: this example is based on allentune's official example.)

@codecov-io
Copy link

codecov-io commented Feb 21, 2020

Codecov Report

Merging #949 into master will decrease coverage by 0.03%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #949      +/-   ##
==========================================
- Coverage   90.22%   90.19%   -0.04%     
==========================================
  Files         112      114       +2     
  Lines        9293     9506     +213     
==========================================
+ Hits         8385     8574     +189     
- Misses        908      932      +24
Impacted Files Coverage Δ
setup.py 0% <ø> (ø) ⬆️
optuna/integration/lightgbm_tuner/__init__.py 52.38% <0%> (-7.62%) ⬇️
optuna/trial.py 87.05% <0%> (-0.72%) ⬇️
optuna/storages/rdb/storage.py 96.52% <0%> (-0.36%) ⬇️
...ration_tests/lightgbm_tuner_tests/test_optimize.py 98.09% <0%> (-0.33%) ⬇️
optuna/exceptions.py 100% <0%> (ø) ⬆️
optuna/integration/cma.py 93.95% <0%> (ø) ⬆️
optuna/study.py 92.72% <0%> (ø) ⬆️
optuna/integration/lightgbm.py 89.79% <0%> (ø) ⬆️
... and 10 more

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 f2a9ce0...e534b92. Read the comment docs.

@himkt himkt marked this pull request as ready for review February 22, 2020 08:23
@himkt
Copy link
Member Author

himkt commented Feb 22, 2020

I made some modifications and this PR is ready for review, I think.

Although allennlp users may define and train a model by allennlp-cli (example),
this PR doesn't use allennlp-cli since I didn't get a good idea to smoothly integrate with optuna.
(related: allentune)

@himkt himkt changed the title [WIP] Add allennlp example Add allennlp example Feb 22, 2020
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 great PR!

I have some small comments. In addition, I found this example required hours of computational time without GPUs. Is it possible to reduce the computational time to about ten minutes because we execute all examples in daily CI using VMs without GPUs.

import allennlp.modules
import optuna
import torch
import uuid
Copy link
Member

Choose a reason for hiding this comment

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

Please separate standard library imports and third library imports?
(c.f., #514)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, fixed in e72dc3d.

examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved


if __name__ == '__main__':
optuna.logging.set_verbosity(optuna.logging.WARNING)
Copy link
Member

Choose a reason for hiding this comment

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

Could you tell me why you suppress Optuna's log messages?

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 didn't have some specific intention. 🙇
(I made this PR based on example/chainer_simple.py, which have this setting)
I removed it in 037d12a.

himkt and others added 9 commits February 26, 2020 21:07
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
@himkt
Copy link
Member Author

himkt commented Feb 26, 2020

0db55bc 095358d

I update experimental configurations to save computational time.

$ time poetry run python examples/allennlp_simple.py
...
poetry run python examples/allennlp_simple.py  619.55s user 13.39s system 183% cpu 5:44.74 total

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 reflecting my comments. The current version is much faster than the previous one.
I have some comments, so please check them.

examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved

# Run tuning with small portion of data
# to reduce computational time.
# https://github.com/optuna/optuna/pull/949#pullrequestreview-364110499
Copy link
Member

Choose a reason for hiding this comment

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

IMO, we can remove this link according to the past PRs. Maybe it would be helpful if we share code conventions like this in a design document.

examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved
examples/allennlp_simple.py Outdated Show resolved Hide resolved
patience=3,
num_epochs=6,
cuda_device=DEVICE,
serialization_dir=f'/tmp/xx/{uuid.uuid1()}',
Copy link
Member

Choose a reason for hiding this comment

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

It would be difficult for users to find the model corresponding to the specific trial. This is partly because the directory names are generated based on uuid and partly because the directory is not displayed in log messages. What do you think if we remove it for simplicity? If you remove it, please delete import uuid too.

Suggested change
serialization_dir=f'/tmp/xx/{uuid.uuid1()}',

Copy link
Member Author

@himkt himkt Mar 3, 2020

Choose a reason for hiding this comment

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

Based on pytorch_lightning_simple.py, I revise my PR to use trial.number for creating a model serialization directory. e534b92

setup.py Outdated
@@ -73,6 +73,7 @@ def get_extras_require():
'sphinx_rtd_theme',
],
'example': [
'allennlp',
Copy link
Member

Choose a reason for hiding this comment

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

allennlp does not support Python 3.5. (c.f., https://pypi.org/project/allennlp/). Please exclude it from the installation targets if the Python version is 3.5.
In addition, please exclude examples/allennlp from the CI targets if the job is examples-python35.

himkt and others added 5 commits March 2, 2020 23:33
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
Co-Authored-By: Toshihiko Yanase <toshihiko.yanase@gmail.com>
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 contribution.

[Note]
allennlp will change the arguments of trainer. For example, train_dataset and validation_dataset will be replaced with dataloader.

@toshihikoyanase toshihikoyanase self-assigned this Mar 10, 2020
@hvy hvy added the example label Mar 10, 2020
Copy link
Member

@hvy hvy 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 the PR! I left some comments if you could take a look.

examples/allennlp_simple.py Show resolved Hide resolved
MODEL_DIR = os.path.join(DIR, 'result')

GLOVE_FILE_PATH = (
'https://s3-us-west-2.amazonaws.com/allennlp/datasets/glove/glove.6B.50d.txt.gz'
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I'm not familiar with AllenNLP but is it the case that this AWS URL (and the ones below) is officially documented somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

This example is based on the allentune's official example and this AWS URL is used here.

@himkt
Copy link
Member Author

himkt commented Mar 12, 2020

@hvy Thank you for taking the time, I revised my PR!

model = create_model(vocab, trial)

if DEVICE > -1:
model.cuda(DEVICE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: model.to(torch.device(’cuda:{}’.format(DEVICE))).

Copy link
Member

@hvy hvy left a comment

Choose a reason for hiding this comment

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

Sorry for the delayed review, changes LGTM.

@hvy hvy merged commit 9f73f8c into optuna:master Mar 18, 2020
@hvy hvy added this to the v1.3.0 milestone Mar 18, 2020
@hvy
Copy link
Member

hvy commented Mar 18, 2020

Let me modify the title of this PR to match the format of our release notes.

@hvy hvy changed the title Add allennlp example Add allennlp example. Mar 18, 2020
@himkt himkt deleted the allennlp-example branch March 18, 2020 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants