-
Notifications
You must be signed in to change notification settings - Fork 103
added simplified BERT support #791
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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,79 @@ | ||
#!/usr/bin/env python3 | ||
# Creates training data for the BERT network training | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tohle se nepíše do komentáže, ale prostě začneš psát text do |
||
# (noisified + masked gold predictions) using the input corpus | ||
# TODO: add support for other NM vocabularies (aside from t2t) | ||
|
||
import argparse | ||
import os | ||
|
||
import numpy as np | ||
|
||
from neuralmonkey.logging import log as _log | ||
from neuralmonkey.vocabulary import ( | ||
Vocabulary, PAD_TOKEN, UNK_TOKEN, from_wordlist) | ||
|
||
|
||
def log(message: str, color: str = "blue") -> None: | ||
_log(message, color) | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument("--input_file", type=str, default="/dev/stdin") | ||
parser.add_argument("--vocabulary", type=str, required=True) | ||
parser.add_argument("--output_prefix", type=str, default=None) | ||
parser.add_argument("--mask_token", type=str, default=UNK_TOKEN) | ||
parser.add_argument("--coverage", type=float, default=0.15) | ||
parser.add_argument("--mask_prob", type=float, default=0.8) | ||
parser.add_argument("--replace_prob", type=float, default=0.1) | ||
parser.add_argument("--vocab_contains_header", type=bool, default=True) | ||
parser.add_argument("--vocab_contains_frequencies", | ||
type=bool, default=True) | ||
args = parser.parse_args() | ||
|
||
assert (args.coverage <= 1 and args.coverage >= 0) | ||
assert (args.mask_prob <= 1 and args.mask_prob >= 0) | ||
assert (args.replace_prob <= 1 and args.replace_prob >= 0) | ||
|
||
log("Loading vocabulary.") | ||
vocabulary = from_wordlist( | ||
args.vocabulary, | ||
contains_header=args.vocab_contains_header, | ||
contains_frequencies=args.vocab_contains_freqeuencies) | ||
|
||
# Tuple[keep_prob | ||
mask_prob = args.mask_prob | ||
replace_prob = args.replace_prob | ||
keep_prob = 1 - mask_prob - replace_prob | ||
sample_probs = (keep_prob, mask_prob, replace_prob) | ||
|
||
output_prefix = args.output_prefix | ||
if output_prefix is None: | ||
output_prefix = args.input_file | ||
out_f_noise = "{}.noisy".format(output_prefix) | ||
out_f_mask = "{}.mask".format(output_prefix) | ||
|
||
out_noise_h = open(out_f_noise, "w", encoding="utf-8") | ||
out_mask_h = open(out_f_mask, "w", encoding="utf-8") | ||
log("Processing data.") | ||
with open(args.input_file, "r", encoding="utf-8") as input_h: | ||
# TODO: performance optimizations | ||
for line in input_h: | ||
line = line.strip().split(" ") | ||
num_samples = int(args.coverage * len(line)) | ||
sampled_indices = np.random.choice(len(line), num_samples, False) | ||
|
||
output_noisy = list(line) | ||
output_masked = [PAD_TOKEN] * len(line) | ||
for i in sampled_indices: | ||
random_token = np.random.choice(vocabulary.index_to_word[4:]) | ||
new_token = np.random.choice( | ||
[line[i], args.mask_token, random_token], p=sample_probs) | ||
output_noisy[i] = new_token | ||
output_masked[i] = line[i] | ||
out_noise_h.write(str(" ".join(output_noisy)) + "\n") | ||
out_mask_h.write(str(" ".join(output_masked)) + "\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,87 @@ | ||
[main] | ||
name="BERT LM" | ||
output="tests/outputs/bert" | ||
tf_manager=<tf_manager> | ||
|
||
train_dataset=<train_data> | ||
val_dataset=<val_data> | ||
test_datasets=[<val_data>] | ||
|
||
runners=[<runner>] | ||
trainer=<trainer> | ||
evaluation=[("source", "source_masked", evaluators.Accuracy)] | ||
|
||
batch_size=10 | ||
epochs=2 | ||
|
||
validation_period="10s" | ||
logging_period="2s" | ||
overwrite_output_dir=True | ||
|
||
|
||
[batching_scheme] | ||
class=dataset.get_batching_scheme | ||
batch_size=32 | ||
|
||
[tf_manager] | ||
class=tf_manager.TensorFlowManager | ||
num_sessions=1 | ||
num_threads=4 | ||
|
||
|
||
[train_data] | ||
class=dataset.load | ||
batching=<batching_scheme> | ||
# source_masked masks all tokens that weren't ``noisified'' | ||
series=["source_noisy", "source_masked"] | ||
data=["tests/data/bert/train.pcedt.forms.noisy", "tests/data/bert/train.pcedt.forms.mask"] | ||
|
||
[val_data] | ||
class=dataset.load | ||
series=["source_noisy", "source_masked"] | ||
data=["tests/data/bert/val.pcedt.forms", "tests/data/bert/val.pcedt.forms"] | ||
|
||
|
||
[vocabulary] | ||
class=vocabulary.from_wordlist | ||
path="tests/data/factored_decoder_vocab.tsv" | ||
|
||
[sequence] | ||
class=model.sequence.EmbeddedSequence | ||
vocabulary=<vocabulary> | ||
data_id="source_noisy" | ||
embedding_size=6 | ||
scale_embeddings_by_depth=True | ||
|
||
[encoder] | ||
class=encoders.transformer.TransformerEncoder | ||
name="encoder_bert" | ||
input_sequence=<sequence> | ||
ff_hidden_size=10 | ||
depth=2 | ||
n_heads=3 | ||
dropout_keep_prob=0.9 | ||
|
||
[labeler] | ||
class=decoders.sequence_labeler.SequenceLabeler | ||
name="labeler_bert" | ||
encoder=<encoder> | ||
data_id="source_masked" | ||
dropout_keep_prob=0.5 | ||
embeddings_source=<sequence> | ||
|
||
[trainer] | ||
class=trainers.delayed_update_trainer.DelayedUpdateTrainer | ||
batches_per_update=5 | ||
l2_weight=1.0e-8 | ||
clip_norm=1.0 | ||
objectives=[<obj>] | ||
|
||
[obj] | ||
class=trainers.cross_entropy_trainer.CostObjective | ||
decoder=<labeler> | ||
|
||
[runner] | ||
class=runners.LabelRunner | ||
decoder=<labeler> | ||
output_series="source" |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OMG, přesně tyhlety změny mám už od října v nějaký větvi, ale čekal jsem, až Jindra dodělá tf.Dataset a mám s tím nějaké modely, co bych dál rád používal. Máš tohle už v hodně modelech?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Experimenty s BERTem mam zatim on-hold, takze no problem.
Klidne tenhle PR zavri a pouzij tu svoji vetev.