Imvikram99
commited on
Commit
·
a1b3ed0
1
Parent(s):
e61ddcf
train and use
Browse files- .history/trainml_20240217185448.py +94 -0
- .lh/trainml.py.json +5 -1
- trainml.py +1 -1
.history/trainml_20240217185448.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# First, we grab tools from our toolbox. These tools help us with different tasks like reading books (datasets),
|
2 |
+
# learning new languages (tokenization), and solving puzzles (models).
|
3 |
+
from datasets import load_dataset # This tool helps us get our book, where the puzzles are.
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AdamW, get_scheduler # These help us understand and solve puzzles.
|
5 |
+
from transformers import DataCollatorWithPadding # This makes sure all puzzle pieces are the same size.
|
6 |
+
from torch.utils.data import DataLoader # This helps us handle one page of puzzles at a time.
|
7 |
+
import torch # This is like the brain of our operations, helping us think through puzzles.
|
8 |
+
from tqdm.auto import tqdm # This is our progress bar, showing us how far we've come in solving the book.
|
9 |
+
import evaluate # This tells us how well we did in solving puzzles.
|
10 |
+
from accelerate import Accelerator # This makes everything go super fast, like a rocket!
|
11 |
+
|
12 |
+
def train_and_save_model():
|
13 |
+
# Now, let's pick up the book we're going to solve today.
|
14 |
+
raw_datasets = load_dataset("glue", "mrpc") # This is a book filled with puzzles about matching sentences.
|
15 |
+
|
16 |
+
# Before we start solving puzzles, we need to understand the language they're written in.
|
17 |
+
checkpoint = "bert-base-uncased" # This is a guidebook to help us understand the puzzles' language.
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint) # This tool helps us read and understand the language in our book.
|
19 |
+
|
20 |
+
# To solve puzzles, we need to make sure we understand each sentence properly.
|
21 |
+
def tokenize_function(example): # This is like reading each sentence carefully and understanding each word.
|
22 |
+
return tokenizer(example["sentence1"], example["sentence2"], truncation=True)
|
23 |
+
|
24 |
+
# We prepare all puzzles in the book so they're ready to solve.
|
25 |
+
tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) # This is like marking all the important parts of the sentences.
|
26 |
+
|
27 |
+
# Puzzles can be different sizes, but our puzzle solver works best when all puzzles are the same size.
|
28 |
+
data_collator = DataCollatorWithPadding(tokenizer=tokenizer) # This adds extra paper to smaller puzzles to make them all the same size.
|
29 |
+
|
30 |
+
# We're setting up our puzzle pages, making sure we're ready to solve them one by one.
|
31 |
+
tokenized_datasets = tokenized_datasets.remove_columns(["sentence1", "sentence2", "idx"]) # We remove stuff we don't need.
|
32 |
+
tokenized_datasets = tokenized_datasets.rename_column("label", "labels") # We make sure the puzzle answers are labeled correctly.
|
33 |
+
tokenized_datasets.set_format("torch") # We make sure our puzzles are in the right format for our brain to understand.
|
34 |
+
|
35 |
+
# Now, we're ready to start solving puzzles, one page at a time.
|
36 |
+
train_dataloader = DataLoader(
|
37 |
+
tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator
|
38 |
+
) # This is our training puzzles.
|
39 |
+
eval_dataloader = DataLoader(
|
40 |
+
tokenized_datasets["validation"], batch_size=8, collate_fn=data_collator
|
41 |
+
) # These are puzzles we use to check our progress.
|
42 |
+
|
43 |
+
# We need a puzzle solver, which is specially trained to solve these types of puzzles.
|
44 |
+
model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) # This is our puzzle-solving robot.
|
45 |
+
|
46 |
+
# Our robot needs instructions on how to get better at solving puzzles.
|
47 |
+
optimizer = AdamW(model.parameters(), lr=5e-5) # This tells our robot how to improve.
|
48 |
+
num_epochs = 1 # This is how many times we'll go through the whole book of puzzles.
|
49 |
+
num_training_steps = num_epochs * len(train_dataloader) # This is the total number of puzzles we'll solve.
|
50 |
+
lr_scheduler = get_scheduler(
|
51 |
+
"linear",
|
52 |
+
optimizer=optimizer,
|
53 |
+
num_warmup_steps=0,
|
54 |
+
num_training_steps=num_training_steps,
|
55 |
+
) # This adjusts how quickly our robot learns over time.
|
56 |
+
|
57 |
+
# To solve puzzles super fast, we're going to use a rocket!
|
58 |
+
accelerator = Accelerator() # This is our rocket that makes everything go faster.
|
59 |
+
model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare(
|
60 |
+
model, optimizer, train_dataloader, eval_dataloader
|
61 |
+
) # We make sure our robot, our puzzles, and our instructions are all ready for the rocket.
|
62 |
+
|
63 |
+
# It's time to start solving puzzles!
|
64 |
+
progress_bar = tqdm(range(num_training_steps)) # This shows us our progress.
|
65 |
+
model.train() # We tell our robot it's time to start learning.
|
66 |
+
for epoch in range(num_epochs): # We go through our book of puzzles multiple times to get really good.
|
67 |
+
for batch in train_dataloader: # Each time, we take a page of puzzles to solve.
|
68 |
+
outputs = model(**batch) # Our robot tries to solve the puzzles.
|
69 |
+
loss = outputs.loss # We check how many mistakes it made.
|
70 |
+
accelerator.backward(loss) # We give feedback to our robot so it can learn from its mistakes.
|
71 |
+
optimizer.step() # We update our robot's puzzle-solving strategy.
|
72 |
+
lr_scheduler.step() # We adjust how quickly our robot is learning.
|
73 |
+
optimizer.zero_grad() # We reset some settings to make sure our robot is ready for the next page.
|
74 |
+
progress_bar.update(1) # We update our progress bar to show how many puzzles we've solved.
|
75 |
+
|
76 |
+
# After all that practice, it's time to test how good our robot has become at solving puzzles.
|
77 |
+
metric = evaluate.load("glue", "mrpc") # This is like the answer key to check our robot's work.
|
78 |
+
model.eval() # We tell our robot it's time to show what it's learned.
|
79 |
+
for batch in eval_dataloader: # We take a page of puzzles we haven't solved yet.
|
80 |
+
with torch.no_grad(): # We make sure we're just testing, not learning anymore.
|
81 |
+
outputs = model(**batch) # Our robot solves the puzzles.
|
82 |
+
logits = outputs.logits # We look at our robot's answers.
|
83 |
+
predictions = torch.argmax(logits, dim=-1) # We decide which answer our robot thinks is right.
|
84 |
+
metric.add_batch(predictions=predictions, references=batch["labels"]) # We compare our robot's answers to the correct answers.
|
85 |
+
|
86 |
+
final_score = metric.compute() # We calculate how well our robot did.
|
87 |
+
print(final_score) # We print out the score to see how well our robot solved the puzzles!
|
88 |
+
|
89 |
+
model.save_pretrained("path/to/save/model")
|
90 |
+
tokenizer.save_pretrained("path/to/save/tokenizer")
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
train_and_save_model()
|
94 |
+
|
.lh/trainml.py.json
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
"activeCommit": 0,
|
4 |
"commits": [
|
5 |
{
|
6 |
-
"activePatchIndex":
|
7 |
"patches": [
|
8 |
{
|
9 |
"date": 1708166375103,
|
@@ -28,6 +28,10 @@
|
|
28 |
{
|
29 |
"date": 1708167281057,
|
30 |
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -87,4 +87,8 @@\n print(final_score) # We print out the score to see how well our robot solved the puzzles!\n \n model.save_pretrained(\"path/to/save/model\")\n tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n+\n+if __name__ == \"__main__\":\n+ train_and_save_model()\n+\n"
|
|
|
|
|
|
|
|
|
31 |
}
|
32 |
],
|
33 |
"date": 1708166375103,
|
|
|
3 |
"activeCommit": 0,
|
4 |
"commits": [
|
5 |
{
|
6 |
+
"activePatchIndex": 6,
|
7 |
"patches": [
|
8 |
{
|
9 |
"date": 1708166375103,
|
|
|
28 |
{
|
29 |
"date": 1708167281057,
|
30 |
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -87,4 +87,8 @@\n print(final_score) # We print out the score to see how well our robot solved the puzzles!\n \n model.save_pretrained(\"path/to/save/model\")\n tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n+\n+if __name__ == \"__main__\":\n+ train_and_save_model()\n+\n"
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"date": 1708176288213,
|
34 |
+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -44,9 +44,9 @@\n model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) # This is our puzzle-solving robot.\n \n # Our robot needs instructions on how to get better at solving puzzles.\n optimizer = AdamW(model.parameters(), lr=5e-5) # This tells our robot how to improve.\n- num_epochs = 3 # This is how many times we'll go through the whole book of puzzles.\n+ num_epochs = 1 # This is how many times we'll go through the whole book of puzzles.\n num_training_steps = num_epochs * len(train_dataloader) # This is the total number of puzzles we'll solve.\n lr_scheduler = get_scheduler(\n \"linear\",\n optimizer=optimizer,\n"
|
35 |
}
|
36 |
],
|
37 |
"date": 1708166375103,
|
trainml.py
CHANGED
@@ -45,7 +45,7 @@ def train_and_save_model():
|
|
45 |
|
46 |
# Our robot needs instructions on how to get better at solving puzzles.
|
47 |
optimizer = AdamW(model.parameters(), lr=5e-5) # This tells our robot how to improve.
|
48 |
-
num_epochs =
|
49 |
num_training_steps = num_epochs * len(train_dataloader) # This is the total number of puzzles we'll solve.
|
50 |
lr_scheduler = get_scheduler(
|
51 |
"linear",
|
|
|
45 |
|
46 |
# Our robot needs instructions on how to get better at solving puzzles.
|
47 |
optimizer = AdamW(model.parameters(), lr=5e-5) # This tells our robot how to improve.
|
48 |
+
num_epochs = 1 # This is how many times we'll go through the whole book of puzzles.
|
49 |
num_training_steps = num_epochs * len(train_dataloader) # This is the total number of puzzles we'll solve.
|
50 |
lr_scheduler = get_scheduler(
|
51 |
"linear",
|