-
Notifications
You must be signed in to change notification settings - Fork 2
/
pretraining_hf.py
152 lines (100 loc) · 4.85 KB
/
pretraining_hf.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
145
146
147
148
149
150
151
152
#! /Users/admin/miniconda3/envs/huggingface/bin/python
import torch
import datasets
import evaluate
import numpy as np
from transformers import AdamW, AutoTokenizer, AutoModelForSequenceClassification
from transformers import DataCollatorWithPadding
from transformers import Trainer
from transformers import TrainingArguments
from transformers import Trainer
from datasets import load_dataset
# data preparation
checkpoint = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# model preparation
model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
# inputs
sequences = [
"I have been waiting for a HuggingFace course whole my life",
"I think this movie is amazing"
]
batch = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt")
batch["labels"] = torch.tensor([1, 1])
# optimization
optimizer = AdamW(model.parameters())
# loss function
loss = model(**batch).loss
loss.backward()
optimizer.step()
# downloading dataset from the hub
raw_dataset = load_dataset("glue", "mrpc")
print(raw_dataset)
# raw_dataset returns a dict which contains
# a train, test, and validation tsst
# load_dataset downloads and caches the dataset at ~/.cache/huggingface/datasets.
raw_train_dataset = raw_dataset["train"]
print(raw_train_dataset[0])
# load a a dataset from the hub
def tokenize_function(example):
"""This function takes a dictionary
(like the items of our dataset) and
returns a new dictionary with the keys
input_ids, attention_mask, and token_type_ids."""
return tokenizer(example["sentence1"], example["sentence2"], truncation=True)
# apply the tokenize function on all our dataset at once
tokenized_datasets = raw_dataset.map(tokenize_function, batched=True)
print(tokenized_datasets)
# Dynamic padding
# while defining the tokenize_function didnt use padding=True
# because padding an entire dataset is not computationally
# efficient. Now I will define a method to pad by batch instead
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
print(data_collator)
# getting a few samples
samples = tokenized_datasets["train"][:8]
samples = {k: v for k, v in samples.items() if k not in ["idx", "sentence1", "sentence2"]}
print([len(x) for x in samples["input_ids"]])
# refactored
batch = data_collator(samples)
print({k: v.shape for k, v in batch.items()})
#############################################################################################################
# Training
training_args = TrainingArguments("test-trainer", use_mps_device=True)
# model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
# this is the implementation without the evaluation criteria
"""trainer = Trainer(
model,
training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
tokenizer=tokenizer,
)
trainer.train()"""
################################################################################################################
# Evaluation
# predictions = trainer.predict(tokenized_datasets["validation"])
# print(predictions.predictions.shape, predictions.label_ids.shape)
# preds = np.argmax(predictions.predictions, axis=-1)
# metric = evaluate.load("glue", "mrpc")
# print(metric.compute(predictions=preds, references=predictions.label_ids))
def compute_metrics(eval_preds):
metric = evaluate.load("glue", "mrpc")
logits, labels = eval_preds
predictions = np.argmax(logits, axis=-1)
return metric.compute(predictions=predictions, references=labels)
training_args = TrainingArguments("test-trainer", evaluation_strategy="epoch", use_mps_device=True)
trainer = Trainer(
model,
training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
trainer.train()
# to any curious soul out there, these are the results:
{'eval_loss': 0.733721911907196, 'eval_accuracy': 0.8357843137254902, 'eval_f1': 0.8854700854700854, 'eval_runtime': 3.4403, 'eval_samples_per_second': 118.595, 'eval_steps_per_second': 14.824, 'epoch': 3.0}
{'train_runtime': 254.6969, 'train_samples_per_second': 43.204, 'train_steps_per_second': 5.406, 'train_loss': 0.37109726273502747, 'epoch': 3.0}
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1377/1377 [04:14<00:00, 5.41it/s]