nreimers commited on
Commit
8f180fd
·
1 Parent(s): 12e01c5
README.md ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # Model
2
+ This model is based on [nicoladecao/msmarco-word2vec256000-distilbert-base-uncased](https://huggingface.co/nicoladecao/msmarco-word2vec256000-distilbert-base-uncased) with a 256k sized vocabulary initialized with word2vec.
3
+
4
+ This model has been trained with MLM on the MS MARCO corpus collection for 785k steps. See train_mlm.py for the train script. It was run on 2x V100 GPUs.
config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "nicoladecao/msmarco-word2vec256000-distilbert-base-uncased",
3
+ "activation": "gelu",
4
+ "architectures": [
5
+ "DistilBertForMaskedLM"
6
+ ],
7
+ "attention_dropout": 0.1,
8
+ "dim": 768,
9
+ "dropout": 0.1,
10
+ "hidden_dim": 3072,
11
+ "initializer_range": 0.02,
12
+ "max_position_embeddings": 512,
13
+ "model_type": "distilbert",
14
+ "n_heads": 12,
15
+ "n_layers": 6,
16
+ "pad_token_id": 0,
17
+ "qa_dropout": 0.1,
18
+ "seq_classif_dropout": 0.2,
19
+ "sinusoidal_pos_embds": false,
20
+ "tie_weights_": true,
21
+ "torch_dtype": "float32",
22
+ "transformers_version": "4.11.3",
23
+ "vocab_size": 256000
24
+ }
optimizer.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:97f15c6cccafd5d4089a325975a832e9a1018433837bbc916ea8852479da4d75
3
+ size 1923091089
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f4f0b1d6bcd5243bf53b6642edde75a1fc9cf373621f4ba769c5a2efc6709604
3
+ size 961556128
rng_state.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d60ff5acb082523e24c835896668276215e7ae5eac18a73b8f441b1bc6b3016
3
+ size 15627
scaler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9d025cb0ee33c6293476260c023fa11bf10b08c5018f876fe1c66195959ef7fa
3
+ size 559
scheduler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:10e3aa981717cb28f2f71ae0329b50c74a68e64df4ea605e3e704ddcb43fd901
3
+ size 623
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"do_lower_case": true, "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "tokenize_chinese_chars": true, "strip_accents": null, "model_max_length": 512, "model_input_names": ["input_ids", "attention_mask"], "tokenizer_class": "PreTrainedTokenizerFast", "special_tokens_map_file": "/home/ukp-reimers/.cache/huggingface/transformers/fe09c361189d8238b9e387f10a088e93f70620bfe74b82036baff1fed512a153.dd8bd9bfd3664b530ea4e645105f557769387b3da9f79bdb55ed556bdd80611d", "name_or_path": "nicoladecao/msmarco-word2vec256000-distilbert-base-uncased"}
train_mlm.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This file runs Masked Language Model. You provide a training file. Each line is interpreted as a sentence / paragraph.
3
+ Optionally, you can also provide a dev file.
4
+
5
+ The fine-tuned model is stored in the output/model_name folder.
6
+
7
+ python train_mlm.py model_name data/train_sentences.txt [data/dev_sentences.txt]
8
+ """
9
+
10
+ from transformers import AutoModelForMaskedLM, AutoTokenizer
11
+ from transformers import DataCollatorForLanguageModeling, DataCollatorForWholeWordMask
12
+ from transformers import Trainer, TrainingArguments
13
+ import sys
14
+ import gzip
15
+ from datetime import datetime
16
+ import wandb
17
+
18
+ wandb.init(project="bert-word2vec")
19
+
20
+ model_name = "nicoladecao/msmarco-word2vec256000-distilbert-base-uncased"
21
+ per_device_train_batch_size = 16
22
+ save_steps = 5000
23
+ eval_steps = 1000
24
+ num_train_epochs = 3
25
+ use_fp16 = True #Set to True, if your GPU supports FP16 operations
26
+ max_length = 250 #Max length for a text input
27
+ do_whole_word_mask = True #If set to true, whole words are masked
28
+ mlm_prob = 15 #Probability that a word is replaced by a [MASK] token
29
+
30
+ model = AutoModelForMaskedLM.from_pretrained(model_name)
31
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
32
+
33
+ ## Freeze embedding layer
34
+ model.distilbert.embeddings.requires_grad = False
35
+
36
+ output_dir = "output/{}-{}".format(model_name.replace("/", "_"), datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
37
+ print("Save checkpoints to:", output_dir)
38
+
39
+
40
+ ##### Load our training datasets
41
+
42
+ train_sentences = []
43
+ train_path = 'data/train.txt'
44
+ with gzip.open(train_path, 'rt', encoding='utf8') if train_path.endswith('.gz') else open(train_path, 'r', encoding='utf8') as fIn:
45
+ for line in fIn:
46
+ line = line.strip()
47
+ if len(line) >= 10:
48
+ train_sentences.append(line)
49
+
50
+ print("Train sentences:", len(train_sentences))
51
+
52
+ dev_sentences = []
53
+
54
+ dev_path = 'data/dev.txt'
55
+ with gzip.open(dev_path, 'rt', encoding='utf8') if dev_path.endswith('.gz') else open(dev_path, 'r', encoding='utf8') as fIn:
56
+ for line in fIn:
57
+ line = line.strip()
58
+ if len(line) >= 10:
59
+ dev_sentences.append(line)
60
+
61
+ print("Dev sentences:", len(dev_sentences))
62
+
63
+ #A dataset wrapper, that tokenizes our data on-the-fly
64
+ class TokenizedSentencesDataset:
65
+ def __init__(self, sentences, tokenizer, max_length, cache_tokenization=False):
66
+ self.tokenizer = tokenizer
67
+ self.sentences = sentences
68
+ self.max_length = max_length
69
+ self.cache_tokenization = cache_tokenization
70
+
71
+ def __getitem__(self, item):
72
+ if not self.cache_tokenization:
73
+ return self.tokenizer(self.sentences[item], add_special_tokens=True, truncation=True, max_length=self.max_length, return_special_tokens_mask=True)
74
+
75
+ if isinstance(self.sentences[item], str):
76
+ self.sentences[item] = self.tokenizer(self.sentences[item], add_special_tokens=True, truncation=True, max_length=self.max_length, return_special_tokens_mask=True)
77
+ return self.sentences[item]
78
+
79
+ def __len__(self):
80
+ return len(self.sentences)
81
+
82
+ train_dataset = TokenizedSentencesDataset(train_sentences, tokenizer, max_length)
83
+ dev_dataset = TokenizedSentencesDataset(dev_sentences, tokenizer, max_length, cache_tokenization=True) if len(dev_sentences) > 0 else None
84
+
85
+
86
+ ##### Training arguments
87
+
88
+ if do_whole_word_mask:
89
+ data_collator = DataCollatorForWholeWordMask(tokenizer=tokenizer, mlm=True, mlm_probability=mlm_prob)
90
+ else:
91
+ data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=True, mlm_probability=mlm_prob)
92
+
93
+ training_args = TrainingArguments(
94
+ output_dir=output_dir,
95
+ overwrite_output_dir=True,
96
+ num_train_epochs=num_train_epochs,
97
+ evaluation_strategy="steps" if dev_dataset is not None else "no",
98
+ per_device_train_batch_size=per_device_train_batch_size,
99
+ eval_steps=eval_steps,
100
+ save_steps=save_steps,
101
+ save_total_limit=1,
102
+ prediction_loss_only=True,
103
+ fp16=use_fp16
104
+ )
105
+
106
+ trainer = Trainer(
107
+ model=model,
108
+ args=training_args,
109
+ data_collator=data_collator,
110
+ train_dataset=train_dataset,
111
+ eval_dataset=dev_dataset
112
+ )
113
+
114
+ trainer.train()
115
+
116
+ print("Save model to:", output_dir)
117
+ model.save_pretrained(output_dir)
118
+ tokenizer.save_pretrained(output_dir)
119
+
120
+ print("Training done")
trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5f6c16d5e3fb714d6d4b43f9e3d9077909eaef9723b3c5411687a5655ef2ca4
3
+ size 2991
vocab.txt ADDED
The diff for this file is too large to render. See raw diff