Imvikram99 commited on
Commit
9e8d4cc
1 Parent(s): ef5bafe
.history/trainml_20240218175922.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 = # 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
+
.history/trainml_20240218175924.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 = # 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
+
.history/trainml_20240218175928.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 = "distilbert-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 = # 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
+
.history/trainml_20240218175930.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 = "distilbert-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 = # 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
+
.history/trainml_20240218180254.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 = "distilbert-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 = # 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(fp16=True) # 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
+
.history/trainml_20240218180256.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 = "distilbert-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 = # 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(fp16=True) # 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
+
.history/trainml_20240218180421.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 = "distilbert-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 = # 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(fp16=True) # 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
+
.history/trainml_20240218180507.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 = "distilbert-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(fp16=True) # 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": 10,
7
  "patches": [
8
  {
9
  "date": 1708166375103,
@@ -48,6 +48,22 @@
48
  {
49
  "date": 1708259164291,
50
  "content": "Index: \n===================================================================\n--- \n+++ \n@@ -10,9 +10,9 @@\n from accelerate import Accelerator # This makes everything go super fast, like a rocket!\n \n def train_and_save_model():\n # Now, let's pick up the book we're going to solve today.\n- raw_datasets = load_dataset(\"glue\") # This is a book filled with puzzles about matching sentences.\n+ raw_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.\n checkpoint = \"bert-base-uncased\" # This is a guidebook to help us understand the puzzles' language.\n tokenizer = AutoTokenizer.from_pretrained(checkpoint) # This tool helps us read and understand the language in our book.\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 = 1 # This is how many times we'll go through the whole book of puzzles.\n+ num_epochs = # 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"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
  ],
53
  "date": 1708166375103,
 
3
  "activeCommit": 0,
4
  "commits": [
5
  {
6
+ "activePatchIndex": 14,
7
  "patches": [
8
  {
9
  "date": 1708166375103,
 
48
  {
49
  "date": 1708259164291,
50
  "content": "Index: \n===================================================================\n--- \n+++ \n@@ -10,9 +10,9 @@\n from accelerate import Accelerator # This makes everything go super fast, like a rocket!\n \n def train_and_save_model():\n # Now, let's pick up the book we're going to solve today.\n- raw_datasets = load_dataset(\"glue\") # This is a book filled with puzzles about matching sentences.\n+ raw_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.\n checkpoint = \"bert-base-uncased\" # This is a guidebook to help us understand the puzzles' language.\n tokenizer = AutoTokenizer.from_pretrained(checkpoint) # This tool helps us read and understand the language in our book.\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 = 1 # This is how many times we'll go through the whole book of puzzles.\n+ num_epochs = # 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"
51
+ },
52
+ {
53
+ "date": 1708259362403,
54
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -13,9 +13,9 @@\n # Now, let's pick up the book we're going to solve today.\n raw_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.\n- checkpoint = \"bert-base-uncased\" # This is a guidebook to help us understand the puzzles' language.\n+ checkpoint = \"bert\"-base-uncased # This is a guidebook to help us understand the puzzles' language.\n tokenizer = 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.\n def tokenize_function(example): # This is like reading each sentence carefully and understanding each word.\n"
55
+ },
56
+ {
57
+ "date": 1708259368829,
58
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -13,9 +13,9 @@\n # Now, let's pick up the book we're going to solve today.\n raw_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.\n- checkpoint = \"bert\"-base-uncased # This is a guidebook to help us understand the puzzles' language.\n+ checkpoint = \"distilbert-base-uncased\" # This is a guidebook to help us understand the puzzles' language.\n tokenizer = 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.\n def tokenize_function(example): # This is like reading each sentence carefully and understanding each word.\n"
59
+ },
60
+ {
61
+ "date": 1708259574161,
62
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -54,9 +54,9 @@\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!\n- accelerator = Accelerator() # This is our rocket that makes everything go faster.\n+ accelerator = Accelerator(fp16=True) # This is our rocket that makes everything go faster.\n model, 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"
63
+ },
64
+ {
65
+ "date": 1708259707398,
66
+ "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 = # 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"
67
  }
68
  ],
69
  "date": 1708166375103,
trainml.py CHANGED
@@ -14,7 +14,7 @@ def train_and_save_model():
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.
@@ -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 = # 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",
@@ -55,7 +55,7 @@ def train_and_save_model():
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.
 
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 = "distilbert-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.
 
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",
 
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(fp16=True) # 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.