Imvikram99 commited on
Commit
e61ddcf
1 Parent(s): bc8c903

train and use

Browse files
.history/app_20240217162502.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ from trainml import train_and_save_model # Import the training function
5
+
6
+
7
+ # Load the trained model and tokenizer
8
+ model_path = "path/to/save/model"
9
+ tokenizer_path = "path/to/save/tokenizer"
10
+
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
12
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
13
+ model.eval() # Set model to evaluation mode
14
+
15
+ def predict_paraphrase(sentence1, sentence2):
16
+ # Tokenize the input sentences
17
+ inputs = tokenizer(sentence1, sentence2, return_tensors="pt", padding=True, truncation=True)
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+
21
+ # Get probabilities
22
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1).tolist()[0]
23
+
24
+ # Assuming the first class (index 0) is 'not paraphrase' and the second class (index 1) is 'paraphrase'
25
+ return {"Not Paraphrase": probs[0], "Paraphrase": probs[1]}
26
+
27
+ # Create Gradio interface
28
+ iface = gr.Interface(
29
+ fn=predict_paraphrase,
30
+ inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter Sentence 1 Here..."),
31
+ gr.inputs.Textbox(lines=2, placeholder="Enter Sentence 2 Here...")],
32
+ outputs=gr.outputs.Label(num_top_classes=2),
33
+ title="Paraphrase Identification",
34
+ description="This model predicts whether two sentences are paraphrases of each other."
35
+ )
36
+
37
+ iface.launch()
.history/app_20240217162512.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ from trainml import train_and_save_model # Import the training function
5
+ train_and_save_model()
6
+
7
+ # Load the trained model and tokenizer
8
+ model_path = "path/to/save/model"
9
+ tokenizer_path = "path/to/save/tokenizer"
10
+
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
12
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
13
+ model.eval() # Set model to evaluation mode
14
+
15
+ def predict_paraphrase(sentence1, sentence2):
16
+ # Tokenize the input sentences
17
+ inputs = tokenizer(sentence1, sentence2, return_tensors="pt", padding=True, truncation=True)
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+
21
+ # Get probabilities
22
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1).tolist()[0]
23
+
24
+ # Assuming the first class (index 0) is 'not paraphrase' and the second class (index 1) is 'paraphrase'
25
+ return {"Not Paraphrase": probs[0], "Paraphrase": probs[1]}
26
+
27
+ # Create Gradio interface
28
+ iface = gr.Interface(
29
+ fn=predict_paraphrase,
30
+ inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter Sentence 1 Here..."),
31
+ gr.inputs.Textbox(lines=2, placeholder="Enter Sentence 2 Here...")],
32
+ outputs=gr.outputs.Label(num_top_classes=2),
33
+ title="Paraphrase Identification",
34
+ description="This model predicts whether two sentences are paraphrases of each other."
35
+ )
36
+
37
+ iface.launch()
.history/trainml_20240217162405.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 3 # 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")
.history/trainml_20240217162411.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 3 # 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")
.history/trainml_20240217162419.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 3 # 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")
.history/trainml_20240217162441.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 = 3 # This is how many times we'll go through the whole book of puzzles.
49
+ num_training_steps = num_epochs * len(train_dataloader) # This is the total number of puzzles we'll solve.
50
+ lr_scheduler = get_scheduler(
51
+ "linear",
52
+ optimizer=optimizer,
53
+ num_warmup_steps=0,
54
+ num_training_steps=num_training_steps,
55
+ ) # This adjusts how quickly our robot learns over time.
56
+
57
+ # To solve puzzles super fast, we're going to use a rocket!
58
+ accelerator = Accelerator() # This is our rocket that makes everything go faster.
59
+ model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare(
60
+ model, optimizer, train_dataloader, eval_dataloader
61
+ ) # We make sure our robot, our puzzles, and our instructions are all ready for the rocket.
62
+
63
+ # It's time to start solving puzzles!
64
+ progress_bar = tqdm(range(num_training_steps)) # This shows us our progress.
65
+ model.train() # We tell our robot it's time to start learning.
66
+ for epoch in range(num_epochs): # We go through our book of puzzles multiple times to get really good.
67
+ for batch in train_dataloader: # Each time, we take a page of puzzles to solve.
68
+ outputs = model(**batch) # Our robot tries to solve the puzzles.
69
+ loss = outputs.loss # We check how many mistakes it made.
70
+ accelerator.backward(loss) # We give feedback to our robot so it can learn from its mistakes.
71
+ optimizer.step() # We update our robot's puzzle-solving strategy.
72
+ lr_scheduler.step() # We adjust how quickly our robot is learning.
73
+ optimizer.zero_grad() # We reset some settings to make sure our robot is ready for the next page.
74
+ progress_bar.update(1) # We update our progress bar to show how many puzzles we've solved.
75
+
76
+ # After all that practice, it's time to test how good our robot has become at solving puzzles.
77
+ metric = evaluate.load("glue", "mrpc") # This is like the answer key to check our robot's work.
78
+ model.eval() # We tell our robot it's time to show what it's learned.
79
+ for batch in eval_dataloader: # We take a page of puzzles we haven't solved yet.
80
+ with torch.no_grad(): # We make sure we're just testing, not learning anymore.
81
+ outputs = model(**batch) # Our robot solves the puzzles.
82
+ logits = outputs.logits # We look at our robot's answers.
83
+ predictions = torch.argmax(logits, dim=-1) # We decide which answer our robot thinks is right.
84
+ metric.add_batch(predictions=predictions, references=batch["labels"]) # We compare our robot's answers to the correct answers.
85
+
86
+ final_score = metric.compute() # We calculate how well our robot did.
87
+ print(final_score) # We print out the score to see how well our robot solved the puzzles!
88
+
89
+ model.save_pretrained("path/to/save/model")
90
+ tokenizer.save_pretrained("path/to/save/tokenizer")
91
+
92
+ if __name__ == "__main__":
93
+ train_and_save_model()
94
+
.lh/app.py.json CHANGED
@@ -3,7 +3,7 @@
3
  "activeCommit": 0,
4
  "commits": [
5
  {
6
- "activePatchIndex": 2,
7
  "patches": [
8
  {
9
  "date": 1708166138917,
@@ -16,6 +16,14 @@
16
  {
17
  "date": 1708166830798,
18
  "content": "Index: \n===================================================================\n--- \n+++ \n@@ -31,5 +31,5 @@\n title=\"Paraphrase Identification\",\n description=\"This model predicts whether two sentences are paraphrases of each other.\"\n )\n \n-iface.launch()\n\\n+iface.launch()\n"
 
 
 
 
 
 
 
 
19
  }
20
  ],
21
  "date": 1708166138917,
 
3
  "activeCommit": 0,
4
  "commits": [
5
  {
6
+ "activePatchIndex": 4,
7
  "patches": [
8
  {
9
  "date": 1708166138917,
 
16
  {
17
  "date": 1708166830798,
18
  "content": "Index: \n===================================================================\n--- \n+++ \n@@ -31,5 +31,5 @@\n title=\"Paraphrase Identification\",\n description=\"This model predicts whether two sentences are paraphrases of each other.\"\n )\n \n-iface.launch()\n\\n+iface.launch()\n"
19
+ },
20
+ {
21
+ "date": 1708167302135,
22
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -1,8 +1,10 @@\n import gradio as gr\n from transformers import AutoTokenizer, AutoModelForSequenceClassification\n import torch\n+from trainml import train_and_save_model # Import the training function\n \n+\n # Load the trained model and tokenizer\n model_path = \"path/to/save/model\"\n tokenizer_path = \"path/to/save/tokenizer\"\n \n"
23
+ },
24
+ {
25
+ "date": 1708167312025,
26
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -1,10 +1,10 @@\n import gradio as gr\n from transformers import AutoTokenizer, AutoModelForSequenceClassification\n import torch\n from trainml import train_and_save_model # Import the training function\n+train_and_save_model()\n \n-\n # Load the trained model and tokenizer\n model_path = \"path/to/save/model\"\n tokenizer_path = \"path/to/save/tokenizer\"\n \n"
27
  }
28
  ],
29
  "date": 1708166138917,
.lh/trainml.py.json CHANGED
@@ -3,7 +3,7 @@
3
  "activeCommit": 0,
4
  "commits": [
5
  {
6
- "activePatchIndex": 1,
7
  "patches": [
8
  {
9
  "date": 1708166375103,
@@ -12,6 +12,22 @@
12
  {
13
  "date": 1708166792627,
14
  "content": "Index: \n===================================================================\n--- \n+++ \n@@ -83,4 +83,7 @@\n metric.add_batch(predictions=predictions, references=batch[\"labels\"]) # We compare our robot's answers to the correct answers.\n \n final_score = metric.compute() # We calculate how well our robot did.\n print(final_score) # We print out the score to see how well our robot solved the puzzles!\n+\n+model.save_pretrained(\"path/to/save/model\")\n+tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n\\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  }
16
  ],
17
  "date": 1708166375103,
 
3
  "activeCommit": 0,
4
  "commits": [
5
  {
6
+ "activePatchIndex": 5,
7
  "patches": [
8
  {
9
  "date": 1708166375103,
 
12
  {
13
  "date": 1708166792627,
14
  "content": "Index: \n===================================================================\n--- \n+++ \n@@ -83,4 +83,7 @@\n metric.add_batch(predictions=predictions, references=batch[\"labels\"]) # We compare our robot's answers to the correct answers.\n \n final_score = metric.compute() # We calculate how well our robot did.\n print(final_score) # We print out the score to see how well our robot solved the puzzles!\n+\n+model.save_pretrained(\"path/to/save/model\")\n+tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n\\n"
15
+ },
16
+ {
17
+ "date": 1708167245700,
18
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -8,8 +8,9 @@\n from tqdm.auto import tqdm # This is our progress bar, showing us how far we've come in solving the book.\n import evaluate # This tells us how well we did in solving puzzles.\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\", \"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"
19
+ },
20
+ {
21
+ "date": 1708167252023,
22
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -86,5 +86,5 @@\n final_score = metric.compute() # We calculate how well our robot did.\n print(final_score) # We print out the score to see how well our robot solved the puzzles!\n \n model.save_pretrained(\"path/to/save/model\")\n-tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n\\n+tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n"
23
+ },
24
+ {
25
+ "date": 1708167259383,
26
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -9,82 +9,82 @@\n import evaluate # This tells us how well we did in solving puzzles.\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\", \"mrpc\") # This is a book filled with puzzles about matching sentences.\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-tokenizer = AutoTokenizer.from_pretrained(checkpoint) # This tool helps us read and understand the language in our book.\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 \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- return tokenizer(example[\"sentence1\"], example[\"sentence2\"], truncation=True)\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+ return tokenizer(example[\"sentence1\"], example[\"sentence2\"], truncation=True)\n \n-# We prepare all puzzles in the book so they're ready to solve.\n-tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) # This is like marking all the important parts of the sentences.\n+ # We prepare all puzzles in the book so they're ready to solve.\n+ tokenized_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.\n-data_collator = DataCollatorWithPadding(tokenizer=tokenizer) # This adds extra paper to smaller puzzles to make them all the same size.\n+ # Puzzles can be different sizes, but our puzzle solver works best when all puzzles are the same size.\n+ data_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.\n-tokenized_datasets = tokenized_datasets.remove_columns([\"sentence1\", \"sentence2\", \"idx\"]) # We remove stuff we don't need.\n-tokenized_datasets = tokenized_datasets.rename_column(\"label\", \"labels\") # We make sure the puzzle answers are labeled correctly.\n-tokenized_datasets.set_format(\"torch\") # We make sure our puzzles are in the right format for our brain to understand.\n+ # We're setting up our puzzle pages, making sure we're ready to solve them one by one.\n+ tokenized_datasets = tokenized_datasets.remove_columns([\"sentence1\", \"sentence2\", \"idx\"]) # We remove stuff we don't need.\n+ tokenized_datasets = tokenized_datasets.rename_column(\"label\", \"labels\") # We make sure the puzzle answers are labeled correctly.\n+ tokenized_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.\n-train_dataloader = DataLoader(\n- tokenized_datasets[\"train\"], shuffle=True, batch_size=8, collate_fn=data_collator\n-) # This is our training puzzles.\n-eval_dataloader = DataLoader(\n- tokenized_datasets[\"validation\"], batch_size=8, collate_fn=data_collator\n-) # These are puzzles we use to check our progress.\n+ # Now, we're ready to start solving puzzles, one page at a time.\n+ train_dataloader = DataLoader(\n+ tokenized_datasets[\"train\"], shuffle=True, batch_size=8, collate_fn=data_collator\n+ ) # This is our training puzzles.\n+ eval_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.\n-model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) # This is our puzzle-solving robot.\n+ # We need a puzzle solver, which is specially trained to solve these types of puzzles.\n+ model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) # This is our puzzle-solving robot.\n \n-# Our robot needs instructions on how to get better at solving puzzles.\n-optimizer = AdamW(model.parameters(), lr=5e-5) # This tells our robot how to improve.\n-num_epochs = 3 # This is how many times we'll go through the whole book of puzzles.\n-num_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- num_warmup_steps=0,\n- num_training_steps=num_training_steps,\n-) # This adjusts how quickly our robot learns over time.\n+ # Our robot needs instructions on how to get better at solving puzzles.\n+ optimizer = AdamW(model.parameters(), lr=5e-5) # This tells our robot how to improve.\n+ num_epochs = 3 # This is how many times we'll go through the whole book of puzzles.\n+ num_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+ 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!\n-accelerator = Accelerator() # 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+ # 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+ 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-# It's time to start solving puzzles!\n-progress_bar = tqdm(range(num_training_steps)) # This shows us our progress.\n-model.train() # We tell our robot it's time to start learning.\n-for 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+ # It's time to start solving puzzles!\n+ progress_bar = tqdm(range(num_training_steps)) # This shows us our progress.\n+ model.train() # We tell our robot it's time to start learning.\n+ for 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.\n-metric = evaluate.load(\"glue\", \"mrpc\") # This is like the answer key to check our robot's work.\n-model.eval() # We tell our robot it's time to show what it's learned.\n-for 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+ # After all that practice, it's time to test how good our robot has become at solving puzzles.\n+ metric = evaluate.load(\"glue\", \"mrpc\") # This is like the answer key to check our robot's work.\n+ model.eval() # We tell our robot it's time to show what it's learned.\n+ for 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 \n-final_score = metric.compute() # We calculate how well our robot did.\n-print(final_score) # We print out the score to see how well our robot solved the puzzles!\n+ final_score = metric.compute() # We calculate how well our robot did.\n+ print(final_score) # We print out the score to see how well our robot solved the puzzles!\n \n-model.save_pretrained(\"path/to/save/model\")\n-tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n+ model.save_pretrained(\"path/to/save/model\")\n+ tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n"
27
+ },
28
+ {
29
+ "date": 1708167281057,
30
+ "content": "Index: \n===================================================================\n--- \n+++ \n@@ -87,4 +87,8 @@\n print(final_score) # We print out the score to see how well our robot solved the puzzles!\n \n model.save_pretrained(\"path/to/save/model\")\n tokenizer.save_pretrained(\"path/to/save/tokenizer\")\n+\n+if __name__ == \"__main__\":\n+ train_and_save_model()\n+\n"
31
  }
32
  ],
33
  "date": 1708166375103,
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
 
 
4
 
5
  # Load the trained model and tokenizer
6
  model_path = "path/to/save/model"
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
+ from trainml import train_and_save_model # Import the training function
5
+ train_and_save_model()
6
 
7
  # Load the trained model and tokenizer
8
  model_path = "path/to/save/model"
trainml.py CHANGED
@@ -9,81 +9,86 @@ from tqdm.auto import tqdm # This is our progress bar, showing us how far we've
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!
87
-
88
- model.save_pretrained("path/to/save/model")
89
- tokenizer.save_pretrained("path/to/save/tokenizer")
 
 
 
 
 
 
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 = 3 # 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
+