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