Commit ·
f3420c2
1
Parent(s): 0c732eb
added comments and more explicit training loop
Browse files- qatransformer2.py +15 -6
qatransformer2.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
from datasets import load_dataset
|
| 2 |
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, TrainingArguments, Trainer, DefaultDataCollator
|
|
|
|
|
|
|
| 3 |
|
| 4 |
squad = load_dataset("squad", split="train[:5000]")
|
| 5 |
squad = squad.train_test_split(test_size=0.2)
|
| 6 |
|
| 7 |
-
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 9 |
|
| 10 |
def preprocess_function(examples):
|
|
@@ -58,13 +60,16 @@ def preprocess_function(examples):
|
|
| 58 |
inputs["end_positions"] = end_positions
|
| 59 |
return inputs
|
| 60 |
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
eval_dataset = squad["test"].map(preprocess_function, batched=True)
|
| 66 |
|
| 67 |
-
|
| 68 |
training_args = TrainingArguments(
|
| 69 |
output_dir="question-answering",
|
| 70 |
evaluation_strategy="epoch",
|
|
@@ -76,16 +81,20 @@ training_args = TrainingArguments(
|
|
| 76 |
push_to_hub=True,
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
trainer = Trainer(
|
| 80 |
model=model,
|
| 81 |
args=training_args,
|
| 82 |
train_dataset=train_dataset,
|
| 83 |
eval_dataset=eval_dataset,
|
| 84 |
tokenizer=tokenizer,
|
| 85 |
-
data_collator=
|
|
|
|
| 86 |
)
|
| 87 |
|
|
|
|
| 88 |
trainer.train()
|
| 89 |
|
| 90 |
|
|
|
|
| 91 |
#evaluation - todo
|
|
|
|
| 1 |
from datasets import load_dataset
|
| 2 |
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, TrainingArguments, Trainer, DefaultDataCollator
|
| 3 |
+
from transformers.optimization import AdamW
|
| 4 |
+
from transformers.data.data_collator import default_data_collator
|
| 5 |
|
| 6 |
squad = load_dataset("squad", split="train[:5000]")
|
| 7 |
squad = squad.train_test_split(test_size=0.2)
|
| 8 |
|
| 9 |
+
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 11 |
|
| 12 |
def preprocess_function(examples):
|
|
|
|
| 60 |
inputs["end_positions"] = end_positions
|
| 61 |
return inputs
|
| 62 |
|
| 63 |
+
# Define the model
|
| 64 |
+
model = AutoModelForQuestionAnswering.from_pretrained("bert-base-uncased")
|
| 65 |
|
| 66 |
+
# Define the optimization algorithm
|
| 67 |
+
optimizer = AdamW(model.parameters(), lr=2e-5)
|
| 68 |
|
| 69 |
+
# Define the loss function
|
| 70 |
+
loss_fn = default_data_collator
|
|
|
|
| 71 |
|
| 72 |
+
# Define the training arguments
|
| 73 |
training_args = TrainingArguments(
|
| 74 |
output_dir="question-answering",
|
| 75 |
evaluation_strategy="epoch",
|
|
|
|
| 81 |
push_to_hub=True,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Define the trainer
|
| 85 |
trainer = Trainer(
|
| 86 |
model=model,
|
| 87 |
args=training_args,
|
| 88 |
train_dataset=train_dataset,
|
| 89 |
eval_dataset=eval_dataset,
|
| 90 |
tokenizer=tokenizer,
|
| 91 |
+
data_collator=loss_fn,
|
| 92 |
+
optimizer=optimizer,
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Train the model
|
| 96 |
trainer.train()
|
| 97 |
|
| 98 |
|
| 99 |
+
|
| 100 |
#evaluation - todo
|