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 Hyperopt example for BERT classifier #186

Merged
merged 7 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
10 changes: 10 additions & 0 deletions docs/code/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ Data Loaders
Data Iterators
===============

:hidden:`Batch`
~~~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: texar.torch.data.Batch
:members:

:hidden:`FieldBatch`
~~~~~~~~~~~~~~~~~~~~~~~~
AvinashBukkittu marked this conversation as resolved.
Show resolved Hide resolved
.. autoclass:: texar.torch.data.FieldBatch
:members:

:hidden:`DataIterator`
~~~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: texar.torch.data.DataIterator
Expand Down
2 changes: 1 addition & 1 deletion examples/bert/bert_classifier_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
config_downstream = importlib.import_module(args.config_downstream)
config_downstream = {
k: v for k, v in config_downstream.__dict__.items()
if not k.startswith('__')}
if not k.startswith('__') and k != "hyperparams"}

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

Expand Down
2 changes: 1 addition & 1 deletion examples/bert/bert_classifier_using_executor_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
config_downstream = importlib.import_module(args.config_downstream)
config_downstream = {
k: v for k, v in config_downstream.__dict__.items()
if not k.startswith('__')}
if not k.startswith('__') and k != "hyperparams"}

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

Expand Down
15 changes: 6 additions & 9 deletions examples/bert/bert_with_hypertuning_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@
parser.add_argument(
"--checkpoint", type=str, default=None,
help="Path to a model checkpoint (including bert modules) to restore from.")
parser.add_argument(
"--do-train", action="store_true", help="Whether to run training.")
parser.add_argument(
"--do-eval", action="store_true",
help="Whether to run eval on the dev set.")
parser.add_argument(
"--do-test", action="store_true",
help="Whether to run test on the test set.")
args = parser.parse_args()

config_data = importlib.import_module(args.config_data)
Expand Down Expand Up @@ -104,7 +96,9 @@ def forward(self, # type: ignore
for more details.

Args:
`batch`: :class:`texar.data.Batch`
`batch`: :class:`texar.data.Batch`. (See
https://texar-pytorch.readthedocs.io/en/latest/code/data.html#texar.torch.data.Batch
AvinashBukkittu marked this conversation as resolved.
Show resolved Hide resolved
for more details)
A batch of inputs to be passed through the model

Returns:
Expand Down Expand Up @@ -287,6 +281,9 @@ def objective_func(self, hyperparams: Dict):
print_model_arch=False
)

if args.checkpoint is not None:
executor.load(args.checkpoint)

executor.train()

print(f"Loss on the valid dataset "
Expand Down
30 changes: 27 additions & 3 deletions texar/torch/data/data/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,33 @@ def connect_name(lhs_name, rhs_name):


class Batch:
r"""Wrapper over Python dictionaries representing a batch. This provides a
common interface with :class:`~texar.torch.data.data.dataset_utils.Batch`
that allows accessing via attributes.
r"""Wrapper over Python dictionaries representing a batch. It provides a
dictionary-like interface to access its fields. This class can be used in
the followed way

.. code-block:: python

hparams = {
AvinashBukkittu marked this conversation as resolved.
Show resolved Hide resolved
'dataset': { 'files': 'data.txt', 'vocab_file': 'vocab.txt' },
'batch_size': 1
}

data = MonoTextData(hparams)
iterator = DataIterator(data)
model = BERTEncoder(pretrained_model_name="bert-base-uncased")

for batch in iterator:
# batch is Batch object and contains the following fields
# batch == {
# 'text': [['<BOS>', 'example', 'sequence', '<EOS>']],
# 'text_ids': [[1, 5, 10, 2]],
# 'length': [4]
# }

input_ids = torch.tensor(batch['text_ids'])
input_length = (1 - (input_ids == 0).int()).sum(dim=1)

bert_embeddings, _ = model(input_ids, input_length)
"""

def __init__(self, batch_size: int, batch: Optional[Dict[str, Any]] = None,
Expand Down