-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
67 lines (61 loc) · 2.07 KB
/
train.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
from transformers import (
AutoModelWithLMHead,
AutoConfig,
Trainer,
AutoTokenizer,
TextDataset,
DataCollatorForLanguageModeling,
TrainingArguments,
GenerateTextArguments,
generate,
)
import random
# Add the import statement for the generate function
from transformers import GenerateTextArguments, generate
train_dataset = TextDataset(
tokenizer=tokenizer, file_path="/content/oKay.txt", block_size=128
)
# validation_dataset = TextDataset(
# tokenizer=tokenizer,
# file_path="/content/oKay2.txt",
# block_size=128,
# )
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
training_args = TrainingArguments(
output_dir="/content/Rewrite5",
num_train_epochs=1.0,
per_device_train_batch_size=2,
# optim="adafactor",
# gradient_accumulation_steps=4,
# gradient_checkpointing=True,
# use_cache=False,
logging_dir="/content/Rewrite5",
save_steps=4000,
)
trainer = Trainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=train_dataset,
)
# Define the callback function
def sample_during_training(trainer, model, tokenizer):
def callback(eval_step, logs):
# Generate and print samples every 1000 steps
if eval_step % 1000 == 0:
prompts = ["The President of the United States is", "The Vice President of the United States is"] # set the prompts to generate samples from
for prompt in prompts:
text = tokenizer.encode(prompt)
myinput, past_key_values = torch.tensor([text]), None
myinput = myinput.to(device)
logits, past_key_values = model(myinput, past_key_values=past_key_values, return_dict=False)
logits = logits[0, -1]
probabilities = torch.nn.functional.softmax(logits)
best_logits, best_indices = logits.topk(4)
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
print(best_words)
return callback
# Add the callback to the trainer
trainer.add_callback(sample_during_training(trainer, model, tokenizer))
# Start training
trainer.train()