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
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
435aa2d
Add simple example of allennlp
Feb 20, 2020
2982f8d
Implement simple example to use allennlp
Feb 21, 2020
afeb9d3
Add search space used in allentune example
Feb 21, 2020
d08e2a6
Add create_model to separate defining model from objective
Feb 22, 2020
85f8586
Use CPU by default
Feb 22, 2020
cd7d3e8
Remove test_dataset (it is not used)
Feb 22, 2020
6fbea99
Update README
Feb 23, 2020
764bcdb
Merge branch 'allennlp-example' of github.com:himkt/optuna into allen…
Feb 23, 2020
e72dc3d
Update order of import
Feb 26, 2020
782ac6a
Update examples/allennlp_simple.py
himkt Feb 26, 2020
0f9b3f4
Update examples/allennlp_simple.py
himkt Feb 26, 2020
f97cfe4
Update examples/allennlp_simple.py
himkt Feb 26, 2020
07a2437
Update examples/allennlp_simple.py
himkt Feb 26, 2020
fe08bad
Merge branch 'allennlp-example' of github.com:himkt/optuna into allen…
Feb 26, 2020
037d12a
Reduce data size
Feb 26, 2020
0db55bc
Adjust experimental settings for CI
Feb 26, 2020
095358d
Tune num_epoch for computational time
Feb 26, 2020
ec1e8ea
Update setup.py
Feb 26, 2020
fadc5cd
Apply suggestions from code review
himkt Mar 2, 2020
c94080f
Ignore allennlp if Python version is 3.5 or 3.8
Mar 2, 2020
86e6891
Apply suggestions from code review
himkt Mar 2, 2020
0113b9c
Apply suggestions from code review
himkt Mar 2, 2020
0819437
Merge branch 'allennlp-example' of github.com:himkt/optuna into allen…
Mar 2, 2020
d11af78
Remove comment
Mar 3, 2020
e534b92
Use trial.number to create unique serialization_dir
Mar 3, 2020
8f62e22
Move prepare_data inside objective for optuna cli
Mar 12, 2020
4277a9a
Add description for allennlp example
Mar 12, 2020
ef99a13
Merge branch 'master' into allennlp-example
Mar 17, 2020
108d746
Apply black
Mar 17, 2020
1e59f0e
Apply feedback
Mar 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Expand Up @@ -300,7 +300,7 @@ jobs:
<<: *examples
environment:
OMP_NUM_THREADS: 1
IGNORES: chainermn_.*|dask_ml_.*|keras_.*|pytorch_lightning_.*|tensorflow_.*|tfkeras_.*|fastai_.*
IGNORES: chainermn_.*|dask_ml_.*|keras_.*|pytorch_lightning_.*|tensorflow_.*|tfkeras_.*|fastai_.*|allennlp_.*

- run: *examples-mn

Expand All @@ -326,6 +326,6 @@ jobs:
<<: *examples
environment:
OMP_NUM_THREADS: 1
IGNORES: chainermn_.*|pytorch_lightning.*|fastai_.*
IGNORES: chainermn_.*|pytorch_lightning.*|fastai_.*|allennlp_.*

- run: *examples-mn
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -89,6 +89,7 @@ study.optimize(objective, n_trials=100) # Invoke optimization of the objective
* [PyTorch Ignite](./examples/pytorch_ignite_simple.py)
* [PyTorch Lightning](./examples/pytorch_lightning_simple.py)
* [FastAI](./examples/fastai_simple.py)
* [AllenNLP](./examples/allennlp_simple.py)

## Installation

Expand Down
135 changes: 135 additions & 0 deletions examples/allennlp_simple.py
@@ -0,0 +1,135 @@
"""
Optuna example that optimizes a classifier configuration for IMDB movie review dataset.
This script is based on the example of allentune (https://github.com/allenai/allentune).

In this example, we optimize the validation accuracy of sentiment classification using AllenNLP.
Since it is too time-consuming to use the entire dataset, we here use a small subset of it.

We have the following two ways to execute this example:

(1) Execute this code directly.
$ python allennlp_simple.py


(2) Execute through CLI.
$ STUDY_NAME=`optuna create-study --direction maximize --storage sqlite:///example.db`
$ optuna study optimize allennlp_simple.py objective --n-trials=100 --study $STUDY_NAME \
--storage sqlite:///example.db

"""

import os
hvy marked this conversation as resolved.
Show resolved Hide resolved
import shutil

import allennlp
import allennlp.data
import allennlp.models
import allennlp.modules
import torch

import optuna


DEVICE = -1 # If you want to use GPU, use DEVICE = 0.
MAX_DATA_SIZE = 3000

DIR = os.getcwd()
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"


def prepare_data():
glove_indexer = allennlp.data.token_indexers.SingleIdTokenIndexer(lowercase_tokens=True)
tokenizer = allennlp.data.tokenizers.WordTokenizer(
word_splitter=allennlp.data.tokenizers.word_splitter.JustSpacesWordSplitter(),
)

reader = allennlp.data.dataset_readers.TextClassificationJsonReader(
token_indexers={"tokens": glove_indexer}, tokenizer=tokenizer,
)
train_dataset = reader.read(
"https://s3-us-west-2.amazonaws.com/allennlp/datasets/imdb/train.jsonl"
)
train_dataset = train_dataset[:MAX_DATA_SIZE]

valid_dataset = reader.read(
"https://s3-us-west-2.amazonaws.com/allennlp/datasets/imdb/dev.jsonl"
)
valid_dataset = valid_dataset[:MAX_DATA_SIZE]

vocab = allennlp.data.Vocabulary.from_instances(train_dataset)
return train_dataset, valid_dataset, vocab


def create_model(vocab, trial):
embedding = allennlp.modules.Embedding(
embedding_dim=50,
trainable=True,
pretrained_file=GLOVE_FILE_PATH,
num_embeddings=vocab.get_vocab_size("tokens"),
)

embedder = allennlp.modules.text_field_embedders.BasicTextFieldEmbedder({"tokens": embedding})

output_dim = trial.suggest_int("output_dim", 16, 128)
max_filter_size = trial.suggest_int("max_filter_size", 3, 6)
num_filters = trial.suggest_int("num_filters", 16, 128)
encoder = allennlp.modules.seq2vec_encoders.CnnEncoder(
ngram_filter_sizes=range(1, max_filter_size),
num_filters=num_filters,
embedding_dim=50,
output_dim=output_dim,
)

dropout = trial.suggest_uniform("dropout", 0, 0.5)
model = allennlp.models.BasicClassifier(
text_field_embedder=embedder, seq2vec_encoder=encoder, dropout=dropout, vocab=vocab,
)

return model


def objective(trial):
train_dataset, valid_dataset, vocab = prepare_data()
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))).


lr = trial.suggest_loguniform("lr", 1e-1, 1e0)
optimizer = torch.optim.SGD(model.parameters(), lr=lr)

iterator = allennlp.data.iterators.BasicIterator(batch_size=10,)
iterator.index_with(vocab)

serialization_dir = os.path.join(MODEL_DIR, "trial_{}".format(trial.number))
trainer = allennlp.training.Trainer(
model=model,
optimizer=optimizer,
iterator=iterator,
train_dataset=train_dataset,
validation_dataset=valid_dataset,
patience=3,
num_epochs=6,
cuda_device=DEVICE,
serialization_dir=serialization_dir,
)
metrics = trainer.train()
return metrics["best_validation_accuracy"]


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

print("Number of finished trials: ", len(study.trials))
print("Best trial:")
trial = study.best_trial

print(" Value: ", trial.value)
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))

shutil.rmtree(MODEL_DIR)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -67,7 +67,7 @@ def get_extras_require() -> Dict[str, List[str]]:
"torchvision>=0.5.0",
"xgboost",
]
+ (["fastai<2"] if (3, 5) < sys.version_info[:2] < (3, 8) else [])
+ (["allennlp", "fastai<2"] if (3, 5) < sys.version_info[:2] < (3, 8) else [])
+ (
[
"dask[dataframe]",
Expand Down