-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefficient_trainer.py
146 lines (116 loc) · 4.73 KB
/
efficient_trainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from transformers import (BertTokenizer, BertForMaskedLM, Trainer, TrainingArguments,
DataCollatorForLanguageModeling, pipeline)
import torch
import numpy as np
import math
import collections
import urllib
import itertools
from torch.utils.data import DataLoader
import pandas as pd
from dataloader import EuropData,create_train_test_val
from datasets import load_dataset
# from helper_functions import group_texts
def load_vocab(vocab_file):
"""Loads a vocabulary file into a dictionary."""
vocab = collections.OrderedDict()
with open(vocab_file, "r", encoding="utf-8") as reader:
tokens = reader.readlines()
for index, token in enumerate(tokens):
token = token.rstrip('\n')
vocab[token] = index
return vocab
def save_vocab():
# retrieve the bert vocab from aws: Done only once
url = "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt"
vocab_file = urllib.request.urlretrieve(url, "data/vocab.txt")
vocab_path = "data/vocab.txt"
vocab = load_vocab(vocab_path)
return vocab
# ----------------------------------------------------------------------------------------
def group_texts(examples, max_seq_length=128):
# Concatenate all texts.
concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()}
total_length = len(concatenated_examples[list(examples.keys())[0]])
# We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
# customize this part to your needs.
total_length = (total_length // max_seq_length) * max_seq_length
# Split by chunks of max_len.
result = {
k: [t[i : i + max_seq_length] for i in range(0, total_length, max_seq_length)]
for k, t in concatenated_examples.items()
}
return result
def train():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# load tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# load model
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
model.resize_token_embeddings(len(tokenizer))
model.to(device)
datasets = load_dataset("text", data_files={"train": 'data/train_full.txt',
"validation": 'data/test_full.txt'})
def tokenize_function(examples):
return tokenizer(examples["text"])
tokenized_datasets = datasets.map(tokenize_function, batched=True, num_proc=8, remove_columns=["text"])
# print(tokenized_datasets['train'][0]['input_ids'])
lm_datasets = tokenized_datasets.map(
group_texts,
batched=True,
batch_size=1000,
num_proc=8
)
# print(len(lm_datasets['train'][0]['input_ids']))
# print(len(lm_datasets['train'][0]['labels']))
# print(lm_datasets['train'][265]['input_ids'])
# print(lm_datasets['train'][266]['input_ids'])
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15)
training_args = TrainingArguments(
output_dir="test-clm",
overwrite_output_dir=True,
evaluation_strategy="epoch",
learning_rate=2e-5,
weight_decay=0.01,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=lm_datasets["train"],
eval_dataset=lm_datasets["validation"],
data_collator=data_collator
)
trainer.train()
eval_results = trainer.evaluate()
print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}")
trainer.save_model()
def predict(text):
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
pipe = pipeline('fill-mask', model='test-clm', tokenizer=tokenizer)
result = pipe(text)
# print(result)
for d in result:
print(d['token_str'])
print(d['score'])
print("------------------")
return result
def create_train_test_split(path):
data = open(path, encoding='utf-8').read().strip().split('\n')
print(len(data))
total_len = len(data)
train_percent = 0.8
train_data = data[:int(0.8*total_len)]
test_data = data[len(train_data):]
print(len(train_data), len(test_data))
with open('data/train_full.txt', 'w', encoding='utf-8') as f:
for item in train_data:
f.write("%s\n" % item)
with open('data/test_full.txt', 'w', encoding='utf-8') as f:
for item in test_data:
f.write("%s\n" % item)
if __name__ == "__main__":
# create_train_test_split('data/europarl-v7.fr-en.en')
# train()
text = "A product [MASK] is the marketing copy that explains what a product is and why it’s worth purchasing."
# text = "We look to the [MASK] also to ensure that there is matched funding for projects."
predict(text)