-
-
Notifications
You must be signed in to change notification settings - Fork 49
BERT MLM Support #208
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
Open
ArjunParthasarathy
wants to merge
5
commits into
OpenMined:master
Choose a base branch
from
ArjunParthasarathy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
BERT MLM Support #208
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c1940c
Specified encoding for reading txt files
ArjunParthasarathy 1315516
Added support for BERT MLM
ArjunParthasarathy 77af30e
Made changes according to PR
ArjunParthasarathy d1c7e92
Merge remote-tracking branch 'upstream/master'
ArjunParthasarathy 8df9c48
Removed tokenizer_ref property
ArjunParthasarathy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from typing import Dict, List | ||
| from torch import LongTensor | ||
| from transformers import DataCollatorForLanguageModeling | ||
|
|
||
|
|
||
| class BERTIterator: | ||
|
|
||
| def __init__(self, dataset_reader, batch_size: int, sentence_len: int): | ||
| self.dataset_reader = dataset_reader | ||
| self.batch_size = batch_size | ||
| self.sentence_len = sentence_len | ||
|
|
||
| self.data_collator = DataCollatorForLanguageModeling( | ||
| tokenizer=self.dataset_reader.encoder.tokenizer_ref, | ||
| mlm = True, | ||
| mlm_probability = 0.15) | ||
|
|
||
| def load(self, dataset_meta) -> LongTensor: | ||
| self.dataset_reader.read(dataset_meta) | ||
|
|
||
| #In case user wants to display the data | ||
| return self.dataset_reader.encoded_text | ||
ArjunParthasarathy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def __iter__(self): | ||
|
|
||
| self.index = 0 | ||
|
|
||
| return self | ||
|
|
||
| def __next__(self): | ||
|
|
||
| if self.index + self.batch_size > self.num_examples: | ||
| raise StopIteration | ||
ArjunParthasarathy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| batch_examples = [] | ||
|
|
||
| for i in range(self.batch_size): | ||
| example = self._load_example() | ||
| batch_examples.append(example) | ||
|
|
||
| batch = self._collate(batch_examples=batch_examples) | ||
|
|
||
| return batch | ||
|
|
||
ArjunParthasarathy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @property | ||
| def num_examples(self): | ||
| """Returns that number of non-overlapping examples | ||
| in the dataset | ||
| """ | ||
|
|
||
| num_examples = (len(self.dataset_reader.encoded_text) - 1) // self.sentence_len | ||
ArjunParthasarathy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return num_examples | ||
|
|
||
| @property | ||
| def num_batches(self): | ||
| """Returns the total number of batches. The last batch | ||
| is dropped if its size is less than self.batch_size. | ||
| """ | ||
|
|
||
| num_batches = self.num_examples // self.batch_size | ||
|
|
||
| return num_batches | ||
|
|
||
| def _load_example(self) -> LongTensor: | ||
|
|
||
| # LongTensor containing the dataset | ||
| dataset = self.dataset_reader.encoded_text | ||
|
|
||
| #Getting an example - sequence of length 'sentence_len' | ||
| example = dataset.narrow( | ||
| dim=0, start=self.index * self.sentence_len, length=self.sentence_len | ||
| ) | ||
|
|
||
| self.index += 1 | ||
|
|
||
| return example | ||
|
|
||
| def _collate(self, batch_examples: List) -> Dict: | ||
|
|
||
| return self.data_collator(batch_examples) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from typing import Dict, List | ||
| from transformers import BertTokenizer | ||
|
|
||
| class BERTEncoder: | ||
|
|
||
| def __init__(self): | ||
| self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') | ||
|
|
||
| def __call__(self, text:List) -> Dict: | ||
| inputs = self.tokenizer(text) | ||
| return {"token_ids": inputs["input_ids"]} | ||
|
|
||
| @property | ||
| def tokenizer_ref(self): | ||
ArjunParthasarathy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #decorator method so tokenizer can't be modified | ||
| return self.tokenizer | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.