-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_model.py
61 lines (53 loc) · 2.26 KB
/
train_model.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
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
from datasets import load_dataset
import torch
dataset = load_dataset('text', script_version='master', data_files={'train': 'catfacts.txt', 'test': 'catfacts.txt'})
#tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
tokenizer.pad_token = tokenizer.eos_token
def tokenize_function(examples):
return tokenizer(examples["text"], padding='max_length', truncation=True)
tokenized_dataset = dataset.map(tokenize_function, batched=True, num_proc=1, remove_columns=["text"])
block_size = tokenizer.model_max_length
def group_texts(examples):
# 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 // block_size) * block_size
# Split by chunks of max_len.
result = {
k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
for k, t in concatenated_examples.items()
}
result["labels"] = result["input_ids"].copy()
return result
lm_dataset = tokenized_dataset.map(
group_texts,
batched=True,
batch_size=4,
num_proc=1,
)
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
torch.cuda.empty_cache()
device = torch.device("cuda")
model.to(device)
training_args = TrainingArguments(
output_dir='./catfact_model', # output directory
num_train_epochs=100, # total number of training epochs
per_device_train_batch_size=4, # batch size per device during training
per_device_eval_batch_size=12, # batch size for evaluation
warmup_steps=50, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
logging_dir='./logs', # directory for storing logs
logging_steps=10,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=lm_dataset["train"],
eval_dataset=lm_dataset["test"],
)
trainer.train()
trainer.save_model()